migration25.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. import async from "async";
  2. /**
  3. * Migration 25
  4. *
  5. * Migration for changing youtubeId to mediaSource
  6. *
  7. * @param {object} MigrationModule - the MigrationModule
  8. * @returns {Promise} - returns promise
  9. */
  10. export default async function migrate(MigrationModule) {
  11. const activityModel = await MigrationModule.runJob("GET_MODEL", { modelName: "activity" }, this);
  12. const playlistModel = await MigrationModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  13. const reportModel = await MigrationModule.runJob("GET_MODEL", { modelName: "report" }, this);
  14. const songModel = await MigrationModule.runJob("GET_MODEL", { modelName: "song" }, this);
  15. const stationModel = await MigrationModule.runJob("GET_MODEL", { modelName: "station" }, this);
  16. const ratingsModel = await MigrationModule.runJob("GET_MODEL", { modelName: "ratings" }, this);
  17. const stationHistoryModel = await MigrationModule.runJob("GET_MODEL", { modelName: "stationHistory" }, this);
  18. await new Promise((resolve, reject) => {
  19. this.log("INFO", `Migration 25. Updating activity with document version 2.`);
  20. activityModel.find({ documentVersion: 2 }, (err, activities) => {
  21. if (err) reject(err);
  22. else {
  23. async.eachLimit(
  24. activities.map(activity => activity._doc),
  25. 1,
  26. (activity, next) => {
  27. const updateObject = { $set: { documentVersion: 3 } };
  28. if (activity.payload.youtubeId) {
  29. activity.payload.mediaSource = `youtube:${activity.payload.youtubeId}`;
  30. delete activity.payload.youtubeId;
  31. updateObject.$set.payload = activity.payload;
  32. }
  33. activityModel.updateOne({ _id: activity._id }, updateObject, next);
  34. },
  35. err => {
  36. this.log("INFO", `Migration 25. Activities found: ${activities.length}.`);
  37. if (err) reject(err);
  38. else resolve();
  39. }
  40. );
  41. }
  42. });
  43. });
  44. await new Promise((resolve, reject) => {
  45. this.log("INFO", `Migration 25. Updating playlist with document version 6.`);
  46. playlistModel.find({ documentVersion: 6 }, (err, documents) => {
  47. if (err) reject(err);
  48. else {
  49. async.eachLimit(
  50. documents.map(document => document._doc),
  51. 1,
  52. (document, next) => {
  53. const updateObject = { $set: { documentVersion: 7 } };
  54. const songs = document.songs.map(song => {
  55. song.mediaSource = `youtube:${song.youtubeId}`;
  56. delete song.youtubeId;
  57. return song;
  58. });
  59. updateObject.$set.songs = songs;
  60. playlistModel.updateOne({ _id: document._id }, updateObject, next);
  61. },
  62. err => {
  63. this.log("INFO", `Migration 25. Playlists found: ${documents.length}.`);
  64. if (err) reject(err);
  65. else resolve();
  66. }
  67. );
  68. }
  69. });
  70. });
  71. await new Promise((resolve, reject) => {
  72. this.log("INFO", `Migration 25. Updating playlist with document version 6.`);
  73. reportModel.find({ documentVersion: 6 }, (err, documents) => {
  74. if (err) reject(err);
  75. else {
  76. async.eachLimit(
  77. documents.map(document => document._doc),
  78. 1,
  79. (document, next) => {
  80. const updateObject = { $set: { documentVersion: 7 } };
  81. document.song.mediaSource = `youtube:${document.song.youtubeId}`;
  82. delete document.song.youtubeId;
  83. updateObject.$set.song = document.song;
  84. reportModel.updateOne({ _id: document._id }, updateObject, next);
  85. },
  86. err => {
  87. this.log("INFO", `Migration 25. Reports found: ${documents.length}.`);
  88. if (err) reject(err);
  89. else resolve();
  90. }
  91. );
  92. }
  93. });
  94. });
  95. await new Promise((resolve, reject) => {
  96. this.log("INFO", `Migration 25. Updating song with document version 9.`);
  97. songModel.find({ documentVersion: 9 }, (err, documents) => {
  98. if (err) reject(err);
  99. else {
  100. async.eachLimit(
  101. documents.map(document => document._doc),
  102. 1,
  103. (document, next) => {
  104. const updateObject = { $set: { documentVersion: 10 } };
  105. updateObject.$set.mediaSource = `youtube:${document.youtubeId}`;
  106. updateObject.$unset = { youtubeId: "" };
  107. songModel.updateOne({ _id: document._id }, updateObject, next);
  108. },
  109. err => {
  110. this.log("INFO", `Migration 25. Songs found: ${documents.length}.`);
  111. if (err) reject(err);
  112. else resolve();
  113. }
  114. );
  115. }
  116. });
  117. });
  118. await new Promise((resolve, reject) => {
  119. this.log("INFO", `Migration 25. Updating station with document version 9.`);
  120. stationModel.find({ documentVersion: 9 }, (err, documents) => {
  121. if (err) reject(err);
  122. else {
  123. async.eachLimit(
  124. documents.map(document => document._doc),
  125. 1,
  126. (document, next) => {
  127. const updateObject = { $set: { documentVersion: 10 } };
  128. if (document.currentSong) {
  129. document.currentSong.mediaSource = `youtube:${document.currentSong.youtubeId}`;
  130. delete document.currentSong.youtubeId;
  131. updateObject.$set.currentSong = document.currentSong;
  132. }
  133. const queue = document.queue.map(song => {
  134. song.mediaSource = `youtube:${song.youtubeId}`;
  135. delete song.youtubeId;
  136. return song;
  137. });
  138. updateObject.$set.queue = queue;
  139. stationModel.updateOne({ _id: document._id }, updateObject, next);
  140. },
  141. err => {
  142. this.log("INFO", `Migration 25. Stations found: ${documents.length}.`);
  143. if (err) reject(err);
  144. else resolve();
  145. }
  146. );
  147. }
  148. });
  149. });
  150. await new Promise((resolve, reject) => {
  151. this.log("INFO", `Migration 25. Updating ratings with document version 1.`);
  152. ratingsModel.find({ documentVersion: 1 }, (err, documents) => {
  153. if (err) reject(err);
  154. else {
  155. ratingsModel.collection.dropIndexes(() => {
  156. async.eachLimit(
  157. documents.map(document => document._doc),
  158. 1,
  159. (document, next) => {
  160. const updateObject = { $set: { documentVersion: 2 } };
  161. if (document.youtubeId) {
  162. updateObject.$set.mediaSource = `youtube:${document.youtubeId}`;
  163. updateObject.$unset = { youtubeId: "" };
  164. }
  165. ratingsModel.updateOne({ _id: document._id }, updateObject, next);
  166. },
  167. err => {
  168. this.log("INFO", `Migration 25. Ratings found: ${documents.length}.`);
  169. if (err) reject(err);
  170. else resolve();
  171. }
  172. );
  173. });
  174. }
  175. });
  176. });
  177. await new Promise((resolve, reject) => {
  178. this.log("INFO", `Migration 25. Updating station history with document version 1.`);
  179. stationHistoryModel.find({ documentVersion: 1 }, (err, documents) => {
  180. if (err) reject(err);
  181. else {
  182. async.eachLimit(
  183. documents.map(document => document._doc),
  184. 1,
  185. (document, next) => {
  186. const updateObject = { $set: { documentVersion: 2 } };
  187. document.payload.song.mediaSource = `youtube:${document.payload.song.youtubeId}`;
  188. delete document.payload.song.youtubeId;
  189. updateObject.$set.payload = document.payload;
  190. stationHistoryModel.updateOne({ _id: document._id }, updateObject, next);
  191. },
  192. err => {
  193. this.log("INFO", `Migration 25. Station history found: ${documents.length}.`);
  194. if (err) reject(err);
  195. else resolve();
  196. }
  197. );
  198. }
  199. });
  200. });
  201. }