migration20.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. *
  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. const songModel = await MigrationModule.runJob("GET_MODEL", { modelName: "song" }, this);
  14. return new Promise((resolve, reject) => {
  15. async.waterfall(
  16. [
  17. next => {
  18. this.log("INFO", `Migration 20. Finding stations with document version 7.`);
  19. stationModel.find(
  20. {
  21. documentVersion: 7
  22. },
  23. (err, stations) => {
  24. if (err) next(err);
  25. else {
  26. async.eachLimit(
  27. stations.map(station => station._doc),
  28. 1,
  29. (station, next) => {
  30. stationModel.updateOne(
  31. { _id: station._id },
  32. {
  33. $unset: {
  34. includedPlaylists: "",
  35. excludedPlaylists: "",
  36. playMode: "",
  37. partyMode: "",
  38. locked: ""
  39. },
  40. $set: {
  41. queue: station.queue.map(song => {
  42. if (!song.requestedAt) song.requestedAt = Date.now();
  43. return song;
  44. }),
  45. autofill: {
  46. enabled: !station.partyMode,
  47. playlists: station.includedPlaylists.map(playlist =>
  48. mongoose.Types.ObjectId(playlist)
  49. ),
  50. limit: 30,
  51. mode: station.playMode ? station.playMode : "random"
  52. },
  53. requests: {
  54. enabled: !!station.partyMode,
  55. access:
  56. station.locked || station.type === "official"
  57. ? "owner"
  58. : "user",
  59. limit: 5
  60. },
  61. blacklist: station.excludedPlaylists.map(playlist =>
  62. mongoose.Types.ObjectId(playlist)
  63. ),
  64. documentVersion: 8
  65. }
  66. },
  67. next
  68. );
  69. },
  70. err => {
  71. this.log("INFO", `Migration 20. Stations found: ${stations.length}.`);
  72. next(err);
  73. }
  74. );
  75. }
  76. }
  77. );
  78. },
  79. next => {
  80. songModel.updateMany({ documentVersion: 7 }, { $set: { documentVersion: 8 } }, (err, res) => {
  81. if (err) next(err);
  82. else {
  83. this.log(
  84. "INFO",
  85. `Migration 20 (songs). Matched: ${res.matchedCount}, modified: ${res.modifiedCount}, ok: ${res.ok}.`
  86. );
  87. next();
  88. }
  89. });
  90. }
  91. ],
  92. err => {
  93. if (err) reject(new Error(err));
  94. else resolve();
  95. }
  96. );
  97. });
  98. }