migration2.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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(
  21. null,
  22. stations.map(station => station._doc)
  23. );
  24. });
  25. },
  26. (stations, next) => {
  27. async.eachLimit(
  28. stations,
  29. 1,
  30. (station, next) => {
  31. this.log("INFO", `Migration 2. Creating station playlist for station ${station._id}.`);
  32. playlistModel.create(
  33. {
  34. isUserModifiable: false,
  35. displayName: `Station - ${station.displayName}`,
  36. songs: [],
  37. createdBy: station.type === "official" ? "Musare" : station.createdBy,
  38. createdFor: `${station._id}`,
  39. createdAt: Date.now(),
  40. type: "station",
  41. documentVersion: 1
  42. },
  43. (err, playlist2) => {
  44. if (err) next(err);
  45. else {
  46. this.log("INFO", `Migration 2. Updating station ${station._id}.`);
  47. stationModel.updateOne(
  48. { _id: station._id },
  49. {
  50. $set: {
  51. playlist2: playlist2._id,
  52. includedPlaylists: [],
  53. excludedPlaylists: [],
  54. playlist: station.type === "official" ? [] : station.playlist,
  55. genres: [],
  56. documentVersion: 2
  57. }
  58. },
  59. (err, res) => {
  60. if (err) next(err);
  61. else {
  62. this.log(
  63. "INFO",
  64. `Migration 2. Updating station ${station._id} done. Matched: ${res.n}, modified: ${res.nModified}, ok: ${res.ok}.`
  65. );
  66. }
  67. }
  68. );
  69. next();
  70. }
  71. }
  72. );
  73. },
  74. next
  75. );
  76. }
  77. ],
  78. (err, response) => {
  79. if (err) {
  80. reject(new Error(err));
  81. } else {
  82. resolve(response);
  83. }
  84. }
  85. );
  86. });
  87. }