migration3.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import async from "async";
  2. /**
  3. * Migration 3
  4. *
  5. * Clean up station object from playlist2 property (replacing old playlist property with playlist2 property), adding a playMode property and removing genres/blacklisted genres
  6. *
  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. return new Promise((resolve, reject) => {
  13. async.waterfall(
  14. [
  15. next => {
  16. this.log("INFO", `Migration 3. Finding stations with document version 2.`);
  17. stationModel.find({ documentVersion: 2 }, (err, stations) => {
  18. this.log("INFO", `Migration 3. Found ${stations.length} stations with document version 2.`);
  19. next(
  20. null,
  21. stations.map(station => station._doc)
  22. );
  23. });
  24. },
  25. (stations, next) => {
  26. async.eachLimit(
  27. stations,
  28. 1,
  29. (station, next) => {
  30. this.log("INFO", `Migration 3. Updating station ${station._id}.`);
  31. stationModel.updateOne(
  32. { _id: station._id },
  33. {
  34. $set: {
  35. playlist: station.playlist2,
  36. playMode: "random",
  37. documentVersion: 3
  38. },
  39. $unset: {
  40. genres: "",
  41. blacklistedGenres: "",
  42. playlist2: "",
  43. privatePlaylist: ""
  44. }
  45. },
  46. (err, res) => {
  47. if (err) next(err);
  48. else {
  49. this.log(
  50. "INFO",
  51. `Migration 3. Updating station ${station._id} done. Matched: ${res.n}, modified: ${res.nModified}, ok: ${res.ok}.`
  52. );
  53. next();
  54. }
  55. }
  56. );
  57. },
  58. next
  59. );
  60. }
  61. ],
  62. (err, response) => {
  63. if (err) {
  64. reject(new Error(err));
  65. } else {
  66. resolve(response);
  67. }
  68. }
  69. );
  70. });
  71. }