migration25.js 7.9 KB

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