migration5.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. const { _id, songId, title, artists, thumbnail, duration, status } = song;
  61. const trimmedSong = {
  62. _id,
  63. songId,
  64. title,
  65. artists,
  66. thumbnail,
  67. duration,
  68. status
  69. };
  70. async.waterfall(
  71. [
  72. next => {
  73. playlistModel.updateMany(
  74. { "songs._id": song._id, documentVersion: 1 },
  75. { $set: { "songs.$": trimmedSong } },
  76. next
  77. );
  78. },
  79. (res, next) => {
  80. stationModel.updateMany(
  81. { "queue._id": song._id, documentVersion: 3 },
  82. { $set: { "queue.$": trimmedSong } },
  83. next
  84. );
  85. },
  86. (res, next) => {
  87. stationModel.updateMany(
  88. { "currentSong._id": song._id, documentVersion: 3 },
  89. { $set: { currentSong: null } },
  90. next
  91. );
  92. }
  93. ],
  94. err => {
  95. next(err);
  96. }
  97. );
  98. },
  99. err => {
  100. next(err);
  101. }
  102. );
  103. }
  104. });
  105. },
  106. next => {
  107. playlistModel.updateMany({ documentVersion: 1 }, { $set: { documentVersion: 2 } }, (err, res) => {
  108. if (err) next(err);
  109. else {
  110. this.log(
  111. "INFO",
  112. `Migration 5 (playlist). Matched: ${res.n}, modified: ${res.nModified}, ok: ${res.ok}.`
  113. );
  114. next();
  115. }
  116. });
  117. },
  118. next => {
  119. stationModel.updateMany({ documentVersion: 3 }, { $set: { documentVersion: 4 } }, (err, res) => {
  120. if (err) next(err);
  121. else {
  122. this.log(
  123. "INFO",
  124. `Migration 5 (station). Matched: ${res.n}, modified: ${res.nModified}, ok: ${res.ok}.`
  125. );
  126. next();
  127. }
  128. });
  129. }
  130. ],
  131. err => {
  132. if (err) {
  133. reject(new Error(err));
  134. } else {
  135. resolve();
  136. }
  137. }
  138. );
  139. });
  140. }