migration3.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. * @param {object} MigrationModule - the MigrationModule
  7. * @returns {Promise} - returns promise
  8. */
  9. export default async function migrate(MigrationModule) {
  10. const stationModel = await MigrationModule.runJob("GET_MODEL", { modelName: "station" }, this);
  11. return new Promise((resolve, reject) => {
  12. async.waterfall(
  13. [
  14. next => {
  15. this.log("INFO", `Migration 3. Finding stations with document version 2.`);
  16. stationModel.find({ documentVersion: 2 }, (err, stations) => {
  17. this.log("INFO", `Migration 3. Found ${stations.length} stations with document version 2.`);
  18. next(
  19. null,
  20. stations.map(station => station._doc)
  21. );
  22. });
  23. },
  24. (stations, next) => {
  25. async.eachLimit(
  26. stations,
  27. 1,
  28. (station, next) => {
  29. this.log("INFO", `Migration 3. Updating station ${station._id}.`);
  30. stationModel.updateOne(
  31. { _id: station._id },
  32. {
  33. $set: {
  34. playlist: station.playlist2,
  35. playMode: "random",
  36. documentVersion: 3
  37. },
  38. $unset: {
  39. genres: "",
  40. blacklistedGenres: "",
  41. playlist2: "",
  42. privatePlaylist: ""
  43. }
  44. },
  45. (err, res) => {
  46. if (err) next(err);
  47. else {
  48. this.log(
  49. "INFO",
  50. `Migration 3. Updating station ${station._id} done. Matched: ${res.n}, modified: ${res.nModified}, ok: ${res.ok}.`
  51. );
  52. next();
  53. }
  54. }
  55. );
  56. },
  57. next
  58. );
  59. }
  60. ],
  61. (err, response) => {
  62. if (err) {
  63. reject(new Error(err));
  64. } else {
  65. resolve(response);
  66. }
  67. }
  68. );
  69. });
  70. }