migration25.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 activity with document version 3.`);
  46. activityModel.find({ documentVersion: 3 }, (err, activities) => {
  47. if (err) reject(err);
  48. else {
  49. async.eachLimit(
  50. activities.map(activity => activity._doc),
  51. 1,
  52. (activity, next) => {
  53. const updateObject = { $set: { documentVersion: 4 } };
  54. if (activity.payload.message) {
  55. activity.payload.message = activity.payload.message.replaceAll(
  56. "<youtubeId>",
  57. "<mediaSource>"
  58. );
  59. activity.payload.message = activity.payload.message.replaceAll(
  60. "</youtubeId>",
  61. "</mediaSource>"
  62. );
  63. }
  64. updateObject.$set.payload = activity.payload;
  65. activityModel.updateOne({ _id: activity._id }, updateObject, next);
  66. },
  67. err => {
  68. this.log("INFO", `Migration 25. Activities found: ${activities.length}.`);
  69. if (err) reject(err);
  70. else resolve();
  71. }
  72. );
  73. }
  74. });
  75. });
  76. await new Promise((resolve, reject) => {
  77. this.log("INFO", `Migration 25. Updating playlist with document version 6.`);
  78. playlistModel.find({ documentVersion: 6 }, (err, documents) => {
  79. if (err) reject(err);
  80. else {
  81. async.eachLimit(
  82. documents.map(document => document._doc),
  83. 1,
  84. (document, next) => {
  85. const updateObject = { $set: { documentVersion: 7 } };
  86. const songs = document.songs.map(song => {
  87. song.mediaSource = `youtube:${song.youtubeId}`;
  88. delete song.youtubeId;
  89. return song;
  90. });
  91. updateObject.$set.songs = songs;
  92. playlistModel.updateOne({ _id: document._id }, updateObject, next);
  93. },
  94. err => {
  95. this.log("INFO", `Migration 25. Playlists found: ${documents.length}.`);
  96. if (err) reject(err);
  97. else resolve();
  98. }
  99. );
  100. }
  101. });
  102. });
  103. await new Promise((resolve, reject) => {
  104. this.log("INFO", `Migration 25. Updating playlist with document version 6.`);
  105. reportModel.find({ documentVersion: 6 }, (err, documents) => {
  106. if (err) reject(err);
  107. else {
  108. async.eachLimit(
  109. documents.map(document => document._doc),
  110. 1,
  111. (document, next) => {
  112. const updateObject = { $set: { documentVersion: 7 } };
  113. document.song.mediaSource = `youtube:${document.song.youtubeId}`;
  114. delete document.song.youtubeId;
  115. updateObject.$set.song = document.song;
  116. reportModel.updateOne({ _id: document._id }, updateObject, next);
  117. },
  118. err => {
  119. this.log("INFO", `Migration 25. Reports found: ${documents.length}.`);
  120. if (err) reject(err);
  121. else resolve();
  122. }
  123. );
  124. }
  125. });
  126. });
  127. await new Promise((resolve, reject) => {
  128. this.log("INFO", `Migration 25. Updating song with document version 9.`);
  129. songModel.find({ documentVersion: 9 }, (err, documents) => {
  130. if (err) reject(err);
  131. else {
  132. async.eachLimit(
  133. documents.map(document => document._doc),
  134. 1,
  135. (document, next) => {
  136. const updateObject = { $set: { documentVersion: 10 } };
  137. updateObject.$set.mediaSource = `youtube:${document.youtubeId}`;
  138. updateObject.$unset = { youtubeId: "" };
  139. songModel.updateOne({ _id: document._id }, updateObject, next);
  140. },
  141. err => {
  142. this.log("INFO", `Migration 25. Songs 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 station with document version 9.`);
  152. stationModel.find({ documentVersion: 9 }, (err, documents) => {
  153. if (err) reject(err);
  154. else {
  155. async.eachLimit(
  156. documents.map(document => document._doc),
  157. 1,
  158. (document, next) => {
  159. const updateObject = { $set: { documentVersion: 10 } };
  160. if (document.currentSong) {
  161. document.currentSong.mediaSource = `youtube:${document.currentSong.youtubeId}`;
  162. delete document.currentSong.youtubeId;
  163. updateObject.$set.currentSong = document.currentSong;
  164. }
  165. const queue = document.queue.map(song => {
  166. song.mediaSource = `youtube:${song.youtubeId}`;
  167. delete song.youtubeId;
  168. return song;
  169. });
  170. updateObject.$set.queue = queue;
  171. stationModel.updateOne({ _id: document._id }, updateObject, next);
  172. },
  173. err => {
  174. this.log("INFO", `Migration 25. Stations found: ${documents.length}.`);
  175. if (err) reject(err);
  176. else resolve();
  177. }
  178. );
  179. }
  180. });
  181. });
  182. await new Promise((resolve, reject) => {
  183. this.log("INFO", `Migration 25. Updating ratings with document version 1.`);
  184. ratingsModel.find({ documentVersion: 1 }, (err, documents) => {
  185. if (err) reject(err);
  186. else {
  187. ratingsModel.collection.dropIndexes(() => {
  188. async.eachLimit(
  189. documents.map(document => document._doc),
  190. 1,
  191. (document, next) => {
  192. const updateObject = { $set: { documentVersion: 2 } };
  193. if (document.youtubeId) {
  194. updateObject.$set.mediaSource = `youtube:${document.youtubeId}`;
  195. updateObject.$unset = { youtubeId: "" };
  196. }
  197. ratingsModel.updateOne({ _id: document._id }, updateObject, next);
  198. },
  199. err => {
  200. this.log("INFO", `Migration 25. Ratings found: ${documents.length}.`);
  201. if (err) reject(err);
  202. else resolve();
  203. }
  204. );
  205. });
  206. }
  207. });
  208. });
  209. await new Promise((resolve, reject) => {
  210. this.log("INFO", `Migration 25. Updating station history with document version 1.`);
  211. stationHistoryModel.find({ documentVersion: 1 }, (err, documents) => {
  212. if (err) reject(err);
  213. else {
  214. async.eachLimit(
  215. documents.map(document => document._doc),
  216. 1,
  217. (document, next) => {
  218. const updateObject = { $set: { documentVersion: 2 } };
  219. document.payload.song.mediaSource = `youtube:${document.payload.song.youtubeId}`;
  220. delete document.payload.song.youtubeId;
  221. updateObject.$set.payload = document.payload;
  222. stationHistoryModel.updateOne({ _id: document._id }, updateObject, next);
  223. },
  224. err => {
  225. this.log("INFO", `Migration 25. Station history found: ${documents.length}.`);
  226. if (err) reject(err);
  227. else resolve();
  228. }
  229. );
  230. }
  231. });
  232. });
  233. }