migration8.js 5.9 KB

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