migration20.js 1.4 KB

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