migration5.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import async from "async";
  2. /**
  3. * Migration 5
  4. *
  5. * Migration for song status property.
  6. * @param {object} MigrationModule - the MigrationModule
  7. * @returns {Promise} - returns promise
  8. */
  9. export default async function migrate(MigrationModule) {
  10. const songModel = await MigrationModule.runJob("GET_MODEL", { modelName: "song" }, this);
  11. const playlistModel = await MigrationModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  12. const stationModel = await MigrationModule.runJob("GET_MODEL", { modelName: "station" }, this);
  13. return new Promise((resolve, reject) => {
  14. async.waterfall(
  15. [
  16. next => {
  17. this.log("INFO", `Migration 5. Finding unverified songs with document version 2.`);
  18. songModel.updateMany(
  19. { documentVersion: 2, verified: false },
  20. { $set: { documentVersion: 3, status: "unverified" }, $unset: { verified: "" } },
  21. (err, res) => {
  22. if (err) next(err);
  23. else {
  24. this.log(
  25. "INFO",
  26. `Migration 5 (unverified songs). Matched: ${res.n}, modified: ${res.nModified}, ok: ${res.ok}.`
  27. );
  28. next();
  29. }
  30. }
  31. );
  32. },
  33. next => {
  34. this.log("INFO", `Migration 5. Finding verified songs with document version 2.`);
  35. songModel.updateMany(
  36. { documentVersion: 2, verified: true },
  37. { $set: { documentVersion: 3, status: "verified" }, $unset: { verified: "" } },
  38. (err, res) => {
  39. if (err) next(err);
  40. else {
  41. this.log(
  42. "INFO",
  43. `Migration 5 (verified songs). Matched: ${res.n}, modified: ${res.nModified}, ok: ${res.ok}.`
  44. );
  45. next();
  46. }
  47. }
  48. );
  49. },
  50. next => {
  51. this.log("INFO", `Migration 5. Updating playlist songs and queue songs.`);
  52. songModel.find({ documentVersion: 3 }, (err, songs) => {
  53. if (err) next(err);
  54. else {
  55. async.eachLimit(
  56. songs.map(song => song._doc),
  57. 1,
  58. (song, next) => {
  59. const { _id, songId, title, artists, thumbnail, duration, status } = song;
  60. const trimmedSong = {
  61. _id,
  62. songId,
  63. title,
  64. artists,
  65. thumbnail,
  66. duration,
  67. status
  68. };
  69. async.waterfall(
  70. [
  71. next => {
  72. playlistModel.updateMany(
  73. { "songs._id": song._id, documentVersion: 1 },
  74. { $set: { "songs.$": trimmedSong } },
  75. next
  76. );
  77. },
  78. (res, next) => {
  79. stationModel.updateMany(
  80. { "queue._id": song._id, documentVersion: 3 },
  81. { $set: { "queue.$": trimmedSong } },
  82. next
  83. );
  84. },
  85. (res, next) => {
  86. stationModel.updateMany(
  87. { "currentSong._id": song._id, documentVersion: 3 },
  88. { $set: { currentSong: null } },
  89. next
  90. );
  91. }
  92. ],
  93. err => {
  94. next(err);
  95. }
  96. );
  97. },
  98. err => {
  99. next(err);
  100. }
  101. );
  102. }
  103. });
  104. },
  105. next => {
  106. playlistModel.updateMany({ documentVersion: 1 }, { $set: { documentVersion: 2 } }, (err, res) => {
  107. if (err) next(err);
  108. else {
  109. this.log(
  110. "INFO",
  111. `Migration 5 (playlist). Matched: ${res.n}, modified: ${res.nModified}, ok: ${res.ok}.`
  112. );
  113. next();
  114. }
  115. });
  116. },
  117. next => {
  118. stationModel.updateMany({ documentVersion: 3 }, { $set: { documentVersion: 4 } }, (err, res) => {
  119. if (err) next(err);
  120. else {
  121. this.log(
  122. "INFO",
  123. `Migration 5 (station). Matched: ${res.n}, modified: ${res.nModified}, ok: ${res.ok}.`
  124. );
  125. next();
  126. }
  127. });
  128. }
  129. ],
  130. err => {
  131. if (err) {
  132. reject(new Error(err));
  133. } else {
  134. resolve();
  135. }
  136. }
  137. );
  138. });
  139. }