migration18.js 4.6 KB

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