migration20.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. {
  49. $set: {
  50. requests: {
  51. enabled: true,
  52. access: "owner",
  53. limit: 3
  54. }
  55. }
  56. },
  57. (err, res) => {
  58. this.log("INFO", `Migration 20. Stations found: ${res.modifiedCount}.`);
  59. next(err);
  60. }
  61. );
  62. stationModel.find(
  63. { documentVersion: 7, includedPlaylists: { $exists: true }, playMode: { $exists: true } },
  64. (err, stations) => {
  65. if (err) next(err);
  66. else {
  67. async.eachLimit(
  68. stations.map(station => station._doc),
  69. 1,
  70. (station, next) => {
  71. stationModel.updateOne(
  72. { _id: station._id },
  73. {
  74. $unset: { includedPlaylists: "", playMode: "" },
  75. $set: {
  76. autofill: {
  77. enabled: true,
  78. playlists: station.includedPlaylists.map(playlist =>
  79. mongoose.Types.ObjectId(playlist)
  80. ),
  81. limit: 3,
  82. mode: station.playMode
  83. }
  84. }
  85. },
  86. next
  87. );
  88. },
  89. err => {
  90. this.log("INFO", `Migration 20. Stations found: ${stations.length}.`);
  91. next(err);
  92. }
  93. );
  94. }
  95. }
  96. );
  97. }
  98. ],
  99. err => {
  100. if (err) reject(new Error(err));
  101. else resolve();
  102. }
  103. );
  104. });
  105. }