migration14.js 1.5 KB

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