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. this.log("INFO", `Migration 18. Updating playlist songs and queue songs.`);
  74. songModel.find({ documentVersion: 6 }, (err, songs) => {
  75. if (err) next(err);
  76. else {
  77. async.eachLimit(
  78. songs.map(song => song._doc),
  79. 1,
  80. (song, next) => {
  81. const {
  82. _id,
  83. youtubeId,
  84. title,
  85. artists,
  86. thumbnail,
  87. duration,
  88. skipDuration,
  89. verified
  90. } = song;
  91. const trimmedSong = {
  92. _id,
  93. youtubeId,
  94. title,
  95. artists,
  96. thumbnail,
  97. duration,
  98. skipDuration,
  99. verified
  100. };
  101. async.waterfall(
  102. [
  103. next => {
  104. playlistModel.updateMany(
  105. { "songs._id": song._id, documentVersion: 5 },
  106. { $set: { "songs.$": trimmedSong } },
  107. next
  108. );
  109. },
  110. (res, next) => {
  111. stationModel.updateMany(
  112. { "queue._id": song._id, documentVersion: 6 },
  113. { $set: { "queue.$": trimmedSong } },
  114. next
  115. );
  116. },
  117. (res, next) => {
  118. stationModel.updateMany(
  119. { "currentSong._id": song._id, documentVersion: 6 },
  120. { $set: { currentSong: null } },
  121. next
  122. );
  123. }
  124. ],
  125. err => {
  126. next(err);
  127. }
  128. );
  129. },
  130. err => {
  131. next(err);
  132. }
  133. );
  134. }
  135. });
  136. },
  137. next => {
  138. playlistModel.updateMany({ documentVersion: 5 }, { $set: { documentVersion: 6 } }, (err, res) => {
  139. if (err) next(err);
  140. else {
  141. this.log(
  142. "INFO",
  143. `Migration 18 (playlist). Matched: ${res.matchedCount}, modified: ${res.modifiedCount}, ok: ${res.ok}.`
  144. );
  145. next();
  146. }
  147. });
  148. },
  149. next => {
  150. stationModel.updateMany({ documentVersion: 6 }, { $set: { documentVersion: 7 } }, (err, res) => {
  151. if (err) next(err);
  152. else {
  153. this.log(
  154. "INFO",
  155. `Migration 18 (station). Matched: ${res.matchedCount}, modified: ${res.modifiedCount}, ok: ${res.ok}.`
  156. );
  157. next();
  158. }
  159. });
  160. }
  161. ],
  162. err => {
  163. if (err) {
  164. reject(new Error(err));
  165. } else {
  166. resolve();
  167. }
  168. }
  169. );
  170. });
  171. }