migration8.js 5.9 KB

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