migration4.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import async from "async";
  2. /**
  3. * Migration 4
  4. *
  5. * Migration for song merging. Merges queueSongs into songs database, and adds verified property to all songs.
  6. *
  7. * @param {object} MigrationModule - the MigrationModule
  8. * @returns {Promise} - returns promise
  9. */
  10. export default async function migrate(MigrationModule) {
  11. const queueSongModel = await MigrationModule.runJob("GET_MODEL", { modelName: "queueSong" }, this);
  12. const songModel = await MigrationModule.runJob("GET_MODEL", { modelName: "song" }, this);
  13. return new Promise((resolve, reject) => {
  14. async.waterfall(
  15. [
  16. next => {
  17. this.log("INFO", `Migration 4. Finding songs with document version 1.`);
  18. songModel.updateMany(
  19. { documentVersion: 1 },
  20. { $set: { documentVersion: 2, verified: true } },
  21. (err, res) => {
  22. if (err) next(err);
  23. else {
  24. this.log(
  25. "INFO",
  26. `Migration 4 (song). Matched: ${res.n}, modified: ${res.nModified}, ok: ${res.ok}.`
  27. );
  28. next();
  29. }
  30. }
  31. );
  32. },
  33. next => {
  34. this.log("INFO", `Migration 4. Finding queue songs.`);
  35. queueSongModel.find({}, next);
  36. },
  37. (queueSongs, next) => {
  38. this.log("INFO", `Migration 4. Found ${queueSongs.length} queue songs.`);
  39. async.eachLimit(
  40. queueSongs,
  41. 1,
  42. (_queueSong, next) => {
  43. const queueSong = JSON.parse(JSON.stringify(_queueSong));
  44. songModel.findOne({ songId: queueSong.songId }, (err, song) => {
  45. if (err) next(err);
  46. else if (song) {
  47. this.log(
  48. "INFO",
  49. `Migration 4. Skipping creating song for queue song ${queueSong.songId} (${queueSong._id}) since it already exists.`
  50. );
  51. next(null, song);
  52. } else {
  53. this.log(
  54. "INFO",
  55. `Migration 4. Creating song for queue song ${queueSong.songId} (${queueSong._id}).`
  56. );
  57. queueSong.verified = false;
  58. queueSong.documentVersion = 2;
  59. delete queueSong._id;
  60. songModel.create(queueSong, next);
  61. }
  62. });
  63. },
  64. err => {
  65. if (err) next(err);
  66. else {
  67. this.log("INFO", `Migration 4. Deleting queue songs.`);
  68. queueSongModel.deleteMany({}, (err, res) => {
  69. if (err) next(err);
  70. else {
  71. this.log(
  72. "INFO",
  73. `Migration 4 (queueSong). Matched: ${res.n}, deleted: ${res.deletedCount}, ok: ${res.ok}.`
  74. );
  75. next();
  76. }
  77. });
  78. }
  79. }
  80. );
  81. }
  82. ],
  83. err => {
  84. if (err) {
  85. reject(new Error(err));
  86. } else {
  87. resolve();
  88. }
  89. }
  90. );
  91. });
  92. }