migration8.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import async from "async";
  2. /**
  3. * Migration 8
  4. *
  5. * Migration for replacing songId with youtubeId whereever it is used, and using songId for any song's _id uses
  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. return new Promise((resolve, reject) => {
  17. async.waterfall(
  18. [
  19. next => {
  20. // return next("BLOCKED");
  21. this.log("INFO", `Migration 8. Finding activities with document version 1.`);
  22. activityModel.find({ documentVersion: 1 }, (err, activities) => {
  23. if (err) next(err);
  24. else {
  25. async.eachLimit(
  26. activities.map(activity => activity._doc),
  27. 1,
  28. (activity, next) => {
  29. const { payload } = activity;
  30. if (payload.songId) {
  31. payload.youtubeId = payload.songId;
  32. delete payload.songId;
  33. }
  34. if (payload.message)
  35. payload.message = payload.message
  36. .replaceAll("<songId", "<youtubeId")
  37. .replaceAll("</songId", "</youtubeId");
  38. activityModel.updateOne(
  39. { _id: activity._id },
  40. { $set: { payload, documentVersion: 2 } },
  41. next
  42. );
  43. },
  44. err => {
  45. if (err) next(err);
  46. else {
  47. this.log("INFO", `Migration 8. Activities found: ${activities.length}.`);
  48. next();
  49. }
  50. }
  51. );
  52. }
  53. });
  54. },
  55. next => {
  56. this.log("INFO", `Migration 8. Finding playlists with document version 2.`);
  57. playlistModel.find({ documentVersion: 2 }, (err, playlists) => {
  58. if (err) next(err);
  59. else {
  60. async.eachLimit(
  61. playlists.map(playlist => playlist._doc),
  62. 1,
  63. (playlist, next) => {
  64. const songs = playlist.songs.map(song => {
  65. song.youtubeId = song.songId;
  66. delete song.songId;
  67. return song;
  68. });
  69. playlistModel.updateOne({ _id: playlist._id }, { $set: { songs } }, next);
  70. },
  71. err => {
  72. if (err) next(err);
  73. else {
  74. playlistModel.updateMany(
  75. { documentVersion: 2 },
  76. { $set: { documentVersion: 3 } },
  77. (err, res) => {
  78. if (err) next(err);
  79. else {
  80. this.log(
  81. "INFO",
  82. `Migration 8. Matched: ${res.n}, modified: ${res.nModified}, ok: ${res.ok}.`
  83. );
  84. next();
  85. }
  86. }
  87. );
  88. }
  89. }
  90. );
  91. }
  92. });
  93. },
  94. next => {
  95. this.log("INFO", `Migration 8. Finding reports with document version 1.`);
  96. reportModel.updateMany(
  97. { documentVersion: 1 },
  98. { $set: { documentVersion: 2 }, $rename: { "song.songId": "song.youtubeId" } },
  99. (err, res) => {
  100. if (err) next(err);
  101. else {
  102. this.log(
  103. "INFO",
  104. `Migration 8. Matched: ${res.n}, modified: ${res.nModified}, ok: ${res.ok}.`
  105. );
  106. next();
  107. }
  108. }
  109. );
  110. },
  111. next => {
  112. this.log("INFO", `Migration 8. Dropping indexes.`);
  113. songModel.collection.dropIndexes((err, res) => {
  114. if (err) next(err);
  115. else {
  116. this.log("INFO", `Migration 8. Dropped indexes: ${res}.`);
  117. next();
  118. }
  119. });
  120. },
  121. next => {
  122. this.log("INFO", `Migration 8. Finding spmgs with document version 3.`);
  123. songModel.updateMany(
  124. { documentVersion: 3 },
  125. { $set: { documentVersion: 4 }, $rename: { songId: "youtubeId" } },
  126. (err, res) => {
  127. if (err) next(err);
  128. else {
  129. this.log(
  130. "INFO",
  131. `Migration 8. Matched: ${res.n}, modified: ${res.nModified}, ok: ${res.ok}.`
  132. );
  133. next();
  134. }
  135. }
  136. );
  137. },
  138. next => {
  139. this.log("INFO", `Migration 8. Finding stations with document version 3.`);
  140. stationModel.find({ documentVersion: 4 }, (err, stations) => {
  141. if (err) next(err);
  142. else {
  143. async.eachLimit(
  144. stations.map(station => station._doc),
  145. 1,
  146. (station, next) => {
  147. const queue = station.queue.map(song => {
  148. song.youtubeId = song.songId;
  149. delete song.songId;
  150. return song;
  151. });
  152. stationModel.updateOne({ _id: station._id }, { $set: { queue } }, next);
  153. },
  154. err => {
  155. if (err) next(err);
  156. else {
  157. stationModel.updateMany(
  158. { documentVersion: 4, currentSong: { $ne: null } },
  159. {
  160. $set: { documentVersion: 5 },
  161. $rename: { "currentSong.songId": "currentSong.youtubeId" }
  162. },
  163. (err, res) => {
  164. if (err) next(err);
  165. else {
  166. this.log(
  167. "INFO",
  168. `Migration 8. Matched: ${res.n}, modified: ${res.nModified}, ok: ${res.ok}.`
  169. );
  170. stationModel.updateMany(
  171. { documentVersion: 4, currentSong: null },
  172. {
  173. $set: { documentVersion: 5 }
  174. },
  175. (err, res) => {
  176. if (err) next(err);
  177. else {
  178. this.log(
  179. "INFO",
  180. `Migration 8. Matched: ${res.n}, modified: ${res.nModified}, ok: ${res.ok}.`
  181. );
  182. next();
  183. }
  184. }
  185. );
  186. }
  187. }
  188. );
  189. }
  190. }
  191. );
  192. }
  193. });
  194. }
  195. ],
  196. err => {
  197. if (err) {
  198. reject(new Error(err));
  199. } else {
  200. resolve();
  201. }
  202. }
  203. );
  204. });
  205. }