migration5.js 4.4 KB

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