migration20.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. stationModel.updateMany(
  47. { documentVersion: 7 },
  48. { $set: { "requests.enabled": true } },
  49. (err, res) => {
  50. this.log("INFO", `Migration 20. Stations found: ${res.modifiedCount}.`);
  51. next(err);
  52. }
  53. );
  54. }
  55. ],
  56. err => {
  57. if (err) reject(new Error(err));
  58. else resolve();
  59. }
  60. );
  61. });
  62. }