migration20.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import async from "async";
  2. import mongoose from "mongoose";
  3. /**
  4. * Migration 20
  5. *
  6. * Migration for station overhaul and preventing migration18 from always running
  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. const songModel = await MigrationModule.runJob("GET_MODEL", { modelName: "song" }, 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. next => {
  79. songModel.updateMany({ documentVersion: 7 }, { $set: { documentVersion: 8 } }, (err, res) => {
  80. if (err) next(err);
  81. else {
  82. this.log(
  83. "INFO",
  84. `Migration 20 (songs). Matched: ${res.matchedCount}, modified: ${res.modifiedCount}, ok: ${res.ok}.`
  85. );
  86. next();
  87. }
  88. });
  89. }
  90. ],
  91. err => {
  92. if (err) reject(new Error(err));
  93. else resolve();
  94. }
  95. );
  96. });
  97. }