migration10.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import async from "async";
  2. /**
  3. * Migration 10
  4. *
  5. * Migration for changes in how the order of songs in a playlist is handled
  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 10. Finding playlists with document version 3.`);
  17. playlistModel.find({ documentVersion: 3 }, (err, playlists) => {
  18. if (err) next(err);
  19. else {
  20. async.eachLimit(
  21. playlists.map(playlisti => playlisti._doc),
  22. 1,
  23. (playlisti, next) => {
  24. // sort playlists by the position property
  25. playlisti.songs.sort((song1, song2) => song1.position - song2.position);
  26. // delete the position property for each song
  27. playlisti.songs.forEach(song => delete song.position);
  28. // update the database
  29. playlistModel.updateOne(
  30. { _id: playlisti._id },
  31. {
  32. $set: {
  33. songs: playlisti.songs,
  34. documentVersion: 4
  35. }
  36. },
  37. next
  38. );
  39. },
  40. err => {
  41. if (err) next(err);
  42. else {
  43. this.log("INFO", `Migration 10. Playlists found: ${playlists.length}.`);
  44. next();
  45. }
  46. }
  47. );
  48. }
  49. });
  50. }
  51. ],
  52. err => {
  53. if (err) reject(new Error(err));
  54. else resolve();
  55. }
  56. );
  57. });
  58. }