migration2.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. *
  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. const playlistModel = await MigrationModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  13. return new Promise((resolve, reject) => {
  14. async.waterfall(
  15. [
  16. next => {
  17. this.log("INFO", `Migration 2. Finding stations with document version 1.`);
  18. stationModel.find({ documentVersion: 1 }, (err, stations) => {
  19. this.log("INFO", `Migration 2. Found ${stations.length} stations with document version 1.`);
  20. next(null, stations);
  21. });
  22. },
  23. (stations, next) => {
  24. async.eachLimit(
  25. stations,
  26. 1,
  27. (station, next) => {
  28. this.log("INFO", `Migration 2. Creating station playlist for station ${station._id}.`);
  29. playlistModel.create(
  30. {
  31. isUserModifiable: false,
  32. displayName: `Station - ${station.displayName}`,
  33. songs: [],
  34. createdBy: station.type === "official" ? "Musare" : station.createdBy,
  35. createdFor: `${station._id}`,
  36. createdAt: Date.now(),
  37. type: "station",
  38. documentVersion: 1
  39. },
  40. (err, playlist2) => {
  41. if (err) next(err);
  42. else {
  43. this.log("INFO", `Migration 2. Updating station ${station._id}.`);
  44. stationModel.updateOne(
  45. { _id: station._id },
  46. {
  47. $set: {
  48. playlist2: playlist2._id,
  49. includedPlaylists: [],
  50. excludedPlaylists: [],
  51. documentVersion: 2
  52. }
  53. },
  54. (err, res) => {
  55. if (err) next(err);
  56. else {
  57. this.log(
  58. "INFO",
  59. `Migration 2. Updating station ${station._id} done. Matched: ${res.n}, modified: ${res.nModified}, ok: ${res.ok}.`
  60. );
  61. }
  62. }
  63. );
  64. }
  65. }
  66. );
  67. },
  68. next
  69. );
  70. }
  71. ],
  72. (err, response) => {
  73. if (err) {
  74. reject(new Error(err));
  75. } else {
  76. resolve(response);
  77. }
  78. }
  79. );
  80. });
  81. }