migration16.js 1.7 KB

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