migration3.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. // next();
  58. },
  59. next
  60. );
  61. }
  62. ],
  63. (err, response) => {
  64. if (err) {
  65. reject(new Error(err));
  66. } else {
  67. resolve(response);
  68. }
  69. }
  70. );
  71. });
  72. }