migration18.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import async from "async";
  2. /**
  3. * Migration 18
  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 18. Finding hidden songs with document version 6.`);
  18. songModel.updateMany(
  19. { documentVersion: 6, status: { $in: ["hidden"] } },
  20. {
  21. $push: { tags: "hidden" },
  22. $set: { documentVersion: 7, verified: false },
  23. $unset: { status: "" }
  24. },
  25. (err, res) => {
  26. if (err) next(err);
  27. else {
  28. this.log(
  29. "INFO",
  30. `Migration 18 (hidden songs). Matched: ${res.matchedCount}, modified: ${res.modifiedCount}, ok: ${res.ok}.`
  31. );
  32. next();
  33. }
  34. }
  35. );
  36. },
  37. next => {
  38. this.log("INFO", `Migration 18. Finding unverified songs with document version 6.`);
  39. songModel.updateMany(
  40. { documentVersion: 6, status: { $in: ["unverified"] } },
  41. { $set: { documentVersion: 7, verified: false }, $unset: { status: "" } },
  42. (err, res) => {
  43. if (err) next(err);
  44. else {
  45. this.log(
  46. "INFO",
  47. `Migration 18 (unverified songs). Matched: ${res.matchedCount}, modified: ${res.modifiedCount}, ok: ${res.ok}.`
  48. );
  49. next();
  50. }
  51. }
  52. );
  53. },
  54. next => {
  55. this.log("INFO", `Migration 18. Finding verified songs with document version 6.`);
  56. songModel.updateMany(
  57. { documentVersion: 6, status: "verified" },
  58. { $set: { documentVersion: 7, verified: true }, $unset: { status: "" } },
  59. (err, res) => {
  60. if (err) next(err);
  61. else {
  62. this.log(
  63. "INFO",
  64. `Migration 18 (verified songs). Matched: ${res.matchedCount}, modified: ${res.modifiedCount}, ok: ${res.ok}.`
  65. );
  66. next();
  67. }
  68. }
  69. );
  70. },
  71. next => {
  72. playlistModel.updateMany({ documentVersion: 5 }, { $set: { documentVersion: 6 } }, (err, res) => {
  73. if (err) next(err);
  74. else {
  75. this.log(
  76. "INFO",
  77. `Migration 18 (playlist). Matched: ${res.matchedCount}, modified: ${res.modifiedCount}, ok: ${res.ok}.`
  78. );
  79. next();
  80. }
  81. });
  82. },
  83. next => {
  84. stationModel.updateMany({ documentVersion: 6 }, { $set: { documentVersion: 7 } }, (err, res) => {
  85. if (err) next(err);
  86. else {
  87. this.log(
  88. "INFO",
  89. `Migration 18 (station). Matched: ${res.matchedCount}, modified: ${res.modifiedCount}, ok: ${res.ok}.`
  90. );
  91. next();
  92. }
  93. });
  94. },
  95. next => {
  96. this.log("INFO", `Migration 18. Updating playlist songs and queue songs.`);
  97. songModel.find({ documentVersion: 7 }, (err, songs) => {
  98. if (err) next(err);
  99. else {
  100. async.eachLimit(
  101. songs.map(song => song._doc),
  102. 1,
  103. (song, next) => {
  104. const {
  105. _id,
  106. youtubeId,
  107. title,
  108. artists,
  109. thumbnail,
  110. duration,
  111. skipDuration,
  112. verified
  113. } = song;
  114. const trimmedSong = {
  115. _id,
  116. youtubeId,
  117. title,
  118. artists,
  119. thumbnail,
  120. duration: +duration,
  121. skipDuration: +skipDuration,
  122. verified
  123. };
  124. async.waterfall(
  125. [
  126. next => {
  127. playlistModel.updateMany(
  128. { "songs._id": song._id, documentVersion: 6 },
  129. { $set: { "songs.$": trimmedSong } },
  130. next
  131. );
  132. },
  133. (res, next) => {
  134. stationModel.updateMany(
  135. { "queue._id": song._id, documentVersion: 7 },
  136. { $set: { "queue.$": trimmedSong } },
  137. next
  138. );
  139. },
  140. (res, next) => {
  141. stationModel.updateMany(
  142. { "currentSong._id": song._id, documentVersion: 7 },
  143. { $set: { currentSong: null } },
  144. next
  145. );
  146. }
  147. ],
  148. err => {
  149. next(err);
  150. }
  151. );
  152. },
  153. err => {
  154. next(err);
  155. }
  156. );
  157. }
  158. });
  159. }
  160. ],
  161. err => {
  162. if (err) {
  163. reject(new Error(err));
  164. } else {
  165. resolve();
  166. }
  167. }
  168. );
  169. });
  170. }