Browse Source

Added migration4 for song merging

Kristian Vos 4 years ago
parent
commit
5480cfe629
2 changed files with 100 additions and 0 deletions
  1. 1 0
      backend/logic/migration/index.js
  2. 99 0
      backend/logic/migration/migrations/migration4.js

+ 1 - 0
backend/logic/migration/index.js

@@ -92,6 +92,7 @@ class _MigrationModule extends CoreClass {
 						},
 						err => {
 							if (err) console.log("Migration error", err);
+							else console.log("Migration completed");
 						}
 					);
 

+ 99 - 0
backend/logic/migration/migrations/migration4.js

@@ -0,0 +1,99 @@
+import async from "async";
+
+/**
+ * Migration 4
+ *
+ * Migration for song merging. Merges queueSongs into songs database, and adds verified property to all songs.
+ *
+ * @param {object} MigrationModule - the MigrationModule
+ * @returns {Promise} - returns promise
+ */
+export default async function migrate(MigrationModule) {
+	const queueSongModel = await MigrationModule.runJob("GET_MODEL", { modelName: "queueSong" }, this);
+	const songModel = await MigrationModule.runJob("GET_MODEL", { modelName: "song" }, this);
+
+	return new Promise((resolve, reject) => {
+		async.waterfall(
+			[
+				next => {
+					this.log("INFO", `Migration 4. Finding songs with document version 1.`);
+					songModel.updateMany(
+						{ documentVersion: 1 },
+						{ $set: { documentVersion: 2, verified: true } },
+						(err, res) => {
+							if (err) next(err);
+							else {
+								this.log(
+									"INFO",
+									`Migration 4 (song). Matched: ${res.n}, modified: ${res.nModified}, ok: ${res.ok}.`
+								);
+
+								next();
+							}
+						}
+					);
+				},
+
+				next => {
+					this.log("INFO", `Migration 4. Finding queue songs.`);
+					queueSongModel.find({}, next);
+				},
+
+				(queueSongs, next) => {
+					this.log("INFO", `Migration 4. Found ${queueSongs.length} queue songs.`);
+					async.eachLimit(
+						queueSongs,
+						1,
+						(_queueSong, next) => {
+							const queueSong = JSON.parse(JSON.stringify(_queueSong));
+
+							songModel.findOne({ songId: queueSong.songId }, (err, song) => {
+								if (err) next(err);
+								else if (song) {
+									this.log(
+										"INFO",
+										`Migration 4. Skipping creating song for queue song ${queueSong.songId} (${queueSong._id}) since it already exists.`
+									);
+									next(null, song);
+								} else {
+									this.log(
+										"INFO",
+										`Migration 4. Creating song for queue song ${queueSong.songId} (${queueSong._id}).`
+									);
+									queueSong.verified = false;
+									queueSong.documentVersion = 2;
+									delete queueSong._id;
+									songModel.create(queueSong, next);
+								}
+							});
+						},
+						err => {
+							if (err) next(err);
+							else {
+								this.log("INFO", `Migration 4. Deleting queue songs.`);
+								queueSongModel.deleteMany({}, (err, res) => {
+									if (err) next(err);
+									else {
+										this.log(
+											"INFO",
+											`Migration 4 (queueSong). Matched: ${res.n}, deleted: ${res.deletedCount}, ok: ${res.ok}.`
+										);
+
+										next();
+									}
+								});
+							}
+						}
+					);
+				}
+			],
+			err => {
+				if (err) {
+					reject(new Error(err));
+				} else {
+					resolve();
+				}
+			}
+		);
+	});
+}