migration14.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import async from "async";
  2. /**
  3. * Migration 14
  4. *
  5. * Migration for removing some data from stations
  6. *
  7. * @param {object} MigrationModule - the MigrationModule
  8. * @returns {Promise} - returns promise
  9. */
  10. export default async function migrate(MigrationModule) {
  11. const stationModel = await MigrationModule.runJob("GET_MODEL", { modelName: "station" }, this);
  12. return new Promise((resolve, reject) => {
  13. async.waterfall(
  14. [
  15. next => {
  16. this.log("INFO", `Migration 14. Finding stations with document version 5.`);
  17. stationModel.find({ documentVersion: 5 }, (err, stations) => {
  18. if (err) next(err);
  19. else {
  20. async.eachLimit(
  21. stations.map(station => station._doc),
  22. 1,
  23. (station, next) => {
  24. const { queue, currentSong } = station;
  25. if (currentSong && currentSong.likes) {
  26. delete currentSong.likes;
  27. delete currentSong.dislikes;
  28. }
  29. queue.forEach(song => {
  30. delete song.likes;
  31. delete song.dislikes;
  32. });
  33. stationModel.updateOne(
  34. { _id: station._id },
  35. {
  36. $set: {
  37. documentVersion: 6,
  38. queue,
  39. currentSong
  40. }
  41. },
  42. next
  43. );
  44. },
  45. err => {
  46. this.log("INFO", `Migration 14. Stations found: ${stations.length}.`);
  47. next(err);
  48. }
  49. );
  50. }
  51. });
  52. }
  53. ],
  54. err => {
  55. if (err) reject(new Error(err));
  56. else resolve();
  57. }
  58. );
  59. });
  60. }