migration2.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import async from "async";
  2. /**
  3. * Migration 2
  4. *
  5. * Updates the document version 1 stations to add the includedPlaylists and excludedPlaylists properties, and to create a station playlist and link that playlist with the playlist2 property.
  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. const playlistModel = await MigrationModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  12. return new Promise((resolve, reject) => {
  13. async.waterfall(
  14. [
  15. next => {
  16. this.log("INFO", `Migration 2. Finding stations with document version 1.`);
  17. stationModel.find({ documentVersion: 1 }, (err, stations) => {
  18. this.log("INFO", `Migration 2. Found ${stations.length} stations with document version 1.`);
  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 2. Creating station playlist for station ${station._id}.`);
  31. playlistModel.create(
  32. {
  33. isUserModifiable: false,
  34. displayName: `Station - ${station.displayName}`,
  35. songs: [],
  36. createdBy: station.type === "official" ? "Musare" : station.createdBy,
  37. createdFor: `${station._id}`,
  38. createdAt: Date.now(),
  39. type: "station",
  40. documentVersion: 1
  41. },
  42. (err, playlist2) => {
  43. if (err) next(err);
  44. else {
  45. this.log("INFO", `Migration 2. Updating station ${station._id}.`);
  46. stationModel.updateOne(
  47. { _id: station._id },
  48. {
  49. $set: {
  50. playlist2: playlist2._id,
  51. includedPlaylists: [],
  52. excludedPlaylists: [],
  53. playlist: station.type === "official" ? [] : station.playlist,
  54. genres: [],
  55. documentVersion: 2
  56. }
  57. },
  58. (err, res) => {
  59. if (err) next(err);
  60. else {
  61. this.log(
  62. "INFO",
  63. `Migration 2. Updating station ${station._id} done. Matched: ${res.n}, modified: ${res.nModified}, ok: ${res.ok}.`
  64. );
  65. }
  66. }
  67. );
  68. next();
  69. }
  70. }
  71. );
  72. },
  73. next
  74. );
  75. }
  76. ],
  77. (err, response) => {
  78. if (err) {
  79. reject(new Error(err));
  80. } else {
  81. resolve(response);
  82. }
  83. }
  84. );
  85. });
  86. }