migration10.js 1.6 KB

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