migration20.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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(
  19. {
  20. documentVersion: 7
  21. },
  22. (err, stations) => {
  23. if (err) next(err);
  24. else {
  25. async.eachLimit(
  26. stations.map(station => station._doc),
  27. 1,
  28. (station, next) => {
  29. stationModel.updateOne(
  30. { _id: station._id },
  31. {
  32. $unset: {
  33. includedPlaylists: "",
  34. excludedPlaylists: "",
  35. playMode: "",
  36. partyMode: "",
  37. locked: ""
  38. },
  39. $set: {
  40. queue: station.queue.map(song => {
  41. if (!song.requestedAt) song.requestedAt = Date.now();
  42. return song;
  43. }),
  44. autofill: {
  45. enabled: !station.partyMode,
  46. playlists: station.includedPlaylists.map(playlist =>
  47. mongoose.Types.ObjectId(playlist)
  48. ),
  49. limit: 30,
  50. mode: station.playMode ? station.playMode : "random"
  51. },
  52. requests: {
  53. enabled: !!station.partyMode,
  54. access:
  55. station.locked || station.type === "official"
  56. ? "owner"
  57. : "user",
  58. limit: 5
  59. },
  60. blacklist: station.excludedPlaylists.map(playlist =>
  61. mongoose.Types.ObjectId(playlist)
  62. ),
  63. documentVersion: 8
  64. }
  65. },
  66. next
  67. );
  68. },
  69. err => {
  70. this.log("INFO", `Migration 20. Stations found: ${stations.length}.`);
  71. next(err);
  72. }
  73. );
  74. }
  75. }
  76. );
  77. }
  78. ],
  79. err => {
  80. if (err) reject(new Error(err));
  81. else resolve();
  82. }
  83. );
  84. });
  85. }