migration4.js 2.6 KB

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