123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- import async from "async";
- /**
- * Migration 8
- *
- * Migration for replacing songId with youtubeId whereever it is used, and using songId for any song's _id uses
- *
- * @param {object} MigrationModule - the MigrationModule
- * @returns {Promise} - returns promise
- */
- export default async function migrate(MigrationModule) {
- const activityModel = await MigrationModule.runJob("GET_MODEL", { modelName: "activity" }, this);
- const playlistModel = await MigrationModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
- const reportModel = await MigrationModule.runJob("GET_MODEL", { modelName: "report" }, this);
- const songModel = await MigrationModule.runJob("GET_MODEL", { modelName: "song" }, this);
- const stationModel = await MigrationModule.runJob("GET_MODEL", { modelName: "station" }, this);
- return new Promise((resolve, reject) => {
- async.waterfall(
- [
- next => {
- // return next("BLOCKED");
- this.log("INFO", `Migration 8. Finding activities with document version 1.`);
- activityModel.find({ documentVersion: 1 }, (err, activities) => {
- if (err) next(err);
- else {
- async.eachLimit(
- activities.map(activity => activity._doc),
- 1,
- (activity, next) => {
- const { payload } = activity;
- if (payload.songId) {
- payload.youtubeId = payload.songId;
- delete payload.songId;
- }
- if (payload.message)
- payload.message = payload.message
- .replaceAll("<songId", "<youtubeId")
- .replaceAll("</songId", "</youtubeId");
- activityModel.updateOne(
- { _id: activity._id },
- { $set: { payload, documentVersion: 2 } },
- next
- );
- },
- err => {
- if (err) next(err);
- else {
- this.log("INFO", `Migration 8. Activities found: ${activities.length}.`);
- next();
- }
- }
- );
- }
- });
- },
- next => {
- this.log("INFO", `Migration 8. Finding playlists with document version 2.`);
- playlistModel.find({ documentVersion: 2 }, (err, playlists) => {
- if (err) next(err);
- else {
- async.eachLimit(
- playlists.map(playlist => playlist._doc),
- 1,
- (playlist, next) => {
- const songs = playlist.songs.map(song => {
- song.youtubeId = song.songId;
- delete song.songId;
- return song;
- });
- playlistModel.updateOne({ _id: playlist._id }, { $set: { songs } }, next);
- },
- err => {
- if (err) next(err);
- else {
- playlistModel.updateMany(
- { documentVersion: 2 },
- { $set: { documentVersion: 3 } },
- (err, res) => {
- if (err) next(err);
- else {
- this.log(
- "INFO",
- `Migration 8. Matched: ${res.n}, modified: ${res.nModified}, ok: ${res.ok}.`
- );
- next();
- }
- }
- );
- }
- }
- );
- }
- });
- },
- next => {
- this.log("INFO", `Migration 8. Finding reports with document version 1.`);
- reportModel.updateMany(
- { documentVersion: 1 },
- { $set: { documentVersion: 2 }, $rename: { "song.songId": "song.youtubeId" } },
- (err, res) => {
- if (err) next(err);
- else {
- this.log(
- "INFO",
- `Migration 8. Matched: ${res.n}, modified: ${res.nModified}, ok: ${res.ok}.`
- );
- next();
- }
- }
- );
- },
- next => {
- this.log("INFO", `Migration 8. Dropping indexes.`);
- songModel.collection.dropIndexes((err, res) => {
- if (err) next(err);
- else {
- this.log("INFO", `Migration 8. Dropped indexes: ${res}.`);
- next();
- }
- });
- },
- next => {
- this.log("INFO", `Migration 8. Finding spmgs with document version 3.`);
- songModel.updateMany(
- { documentVersion: 3 },
- { $set: { documentVersion: 4 }, $rename: { songId: "youtubeId" } },
- (err, res) => {
- if (err) next(err);
- else {
- this.log(
- "INFO",
- `Migration 8. Matched: ${res.n}, modified: ${res.nModified}, ok: ${res.ok}.`
- );
- next();
- }
- }
- );
- },
- next => {
- this.log("INFO", `Migration 8. Finding stations with document version 3.`);
- stationModel.find({ documentVersion: 4 }, (err, stations) => {
- if (err) next(err);
- else {
- async.eachLimit(
- stations.map(station => station._doc),
- 1,
- (station, next) => {
- const queue = station.queue.map(song => {
- song.youtubeId = song.songId;
- delete song.songId;
- return song;
- });
- stationModel.updateOne({ _id: station._id }, { $set: { queue } }, next);
- },
- err => {
- if (err) next(err);
- else {
- stationModel.updateMany(
- { documentVersion: 4, currentSong: { $ne: null } },
- {
- $set: { documentVersion: 5 },
- $rename: { "currentSong.songId": "currentSong.youtubeId" }
- },
- (err, res) => {
- if (err) next(err);
- else {
- this.log(
- "INFO",
- `Migration 8. Matched: ${res.n}, modified: ${res.nModified}, ok: ${res.ok}.`
- );
- stationModel.updateMany(
- { documentVersion: 4, currentSong: null },
- {
- $set: { documentVersion: 5 }
- },
- (err, res) => {
- if (err) next(err);
- else {
- this.log(
- "INFO",
- `Migration 8. Matched: ${res.n}, modified: ${res.nModified}, ok: ${res.ok}.`
- );
- next();
- }
- }
- );
- }
- }
- );
- }
- }
- );
- }
- });
- }
- ],
- err => {
- if (err) {
- reject(new Error(err));
- } else {
- resolve();
- }
- }
- );
- });
- }
|