migration16.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import async from "async";
  2. /**
  3. * Migration 16
  4. *
  5. * Migration for playlists to remove isUserModifiable
  6. * @param {object} MigrationModule - the MigrationModule
  7. * @returns {Promise} - returns promise
  8. */
  9. export default async function migrate(MigrationModule) {
  10. const playlistModel = await MigrationModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  11. return new Promise((resolve, reject) => {
  12. async.waterfall(
  13. [
  14. next => {
  15. this.log("INFO", `Migration 16. Finding playlists with document version 4.`);
  16. playlistModel.find({ documentVersion: 4 }, (err, playlists) => {
  17. if (err) next(err);
  18. else {
  19. async.eachLimit(
  20. playlists.map(playlisti => playlisti._doc),
  21. 1,
  22. (playlisti, next) => {
  23. // set liked/disliked playlist to new type
  24. if (playlisti.type === "user" && playlisti.displayName === "Liked Songs")
  25. playlisti.type = "user-liked";
  26. else if (playlisti.type === "user" && playlisti.displayName === "Disliked Songs")
  27. playlisti.type = "user-disliked";
  28. // update the database
  29. playlistModel.updateOne(
  30. { _id: playlisti._id },
  31. {
  32. $unset: {
  33. isUserModifiable: ""
  34. },
  35. $set: {
  36. type: playlisti.type,
  37. documentVersion: 5
  38. }
  39. },
  40. next
  41. );
  42. },
  43. err => {
  44. if (err) next(err);
  45. else {
  46. this.log("INFO", `Migration 16. Playlists found: ${playlists.length}.`);
  47. next();
  48. }
  49. }
  50. );
  51. }
  52. });
  53. }
  54. ],
  55. err => {
  56. if (err) reject(new Error(err));
  57. else resolve();
  58. }
  59. );
  60. });
  61. }