瀏覽代碼

Added migration2 code

Kristian Vos 4 年之前
父節點
當前提交
a255f6c625

+ 1 - 1
backend/logic/db/index.js

@@ -13,7 +13,7 @@ const REQUIRED_DOCUMENT_VERSIONS = {
 	queueSong: 1,
 	report: 1,
 	song: 1,
-	station: 1,
+	station: 2,
 	user: 1
 };
 

+ 85 - 0
backend/logic/migration/migrations/migration2.js

@@ -0,0 +1,85 @@
+import async from "async";
+
+/**
+ * Migration 2
+ *
+ * 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.
+ *
+ * @param {object} MigrationModule - the MigrationModule
+ * @returns {Promise} - returns promise
+ */
+export default async function migrate(MigrationModule) {
+	const stationModel = await MigrationModule.runJob("GET_MODEL", { modelName: "station" }, this);
+	const playlistModel = await MigrationModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
+
+	return new Promise((resolve, reject) => {
+		async.waterfall(
+			[
+				next => {
+					this.log("INFO", `Migration 2. Finding stations with document version 1.`);
+					stationModel.find({ documentVersion: 1 }, (err, stations) => {
+						this.log("INFO", `Migration 2. Found ${stations.length} stations with document version 1.`);
+
+						next(null, stations);
+					});
+				},
+
+				(stations, next) => {
+					async.eachLimit(
+						stations,
+						1,
+						(station, next) => {
+							this.log("INFO", `Migration 2. Creating station playlist for station ${station._id}.`);
+							playlistModel.create(
+								{
+									isUserModifiable: false,
+									displayName: `Station - ${station.displayName}`,
+									songs: [],
+									createdBy: station.type === "official" ? "Musare" : station.createdBy,
+									createdFor: `${station._id}`,
+									createdAt: Date.now(),
+									type: "station",
+									documentVersion: 1
+								},
+
+								(err, playlist2) => {
+									if (err) next(err);
+									else {
+										this.log("INFO", `Migration 2. Updating station ${station._id}.`);
+										stationModel.updateOne(
+											{ _id: station._id },
+											{
+												$set: {
+													playlist2: playlist2._id,
+													includedPlaylists: [],
+													excludedPlaylists: []
+												}
+											},
+											(err, res) => {
+												if (err) next(err);
+												else {
+													this.log(
+														"INFO",
+														`Migration 2. Updating station ${station._id} done. Matched: ${res.n}, modified: ${res.nModified}, ok: ${res.ok}.`
+													);
+												}
+											}
+										);
+									}
+								}
+							);
+						},
+						next
+					);
+				}
+			],
+			(err, response) => {
+				if (err) {
+					reject(new Error(err));
+				} else {
+					resolve(response);
+				}
+			}
+		);
+	});
+}

+ 0 - 44
backend/logic/migration/migrations/migration2WIP.js

@@ -1,44 +0,0 @@
-import async from "async";
-
-/**
- * Migration 2
- *
- * Changes to keep in mind: includedPlaylists, excludedPlaylists, playlist2
- *
- * @param {object} MigrationModule - the MigrationModule
- * @returns {Promise} - returns promise
- */
-export default async function migrate(MigrationModule) {
-	const stationModel = await MigrationModule.runJob("GET_MODEL", { modelName: "station" }, this);
-
-	return new Promise((resolve, reject) => {
-		async.waterfall(
-			[
-				next => {
-					stationModel.updateMany(
-						{ documentVersion: 1 },
-						{ $set: { documentVersion: 2, includedPlaylists: [], excludedPlaylists: [] } },
-						(err, res) => {
-							if (err) next(err);
-							else {
-								this.log(
-									"INFO",
-									`Migration 2 (station). Matched: ${res.n}, modified: ${res.nModified}, ok: ${res.ok}.`
-								);
-
-								next();
-							}
-						}
-					);
-				}
-			],
-			(err, response) => {
-				if (err) {
-					reject(new Error(err));
-				} else {
-					resolve(response);
-				}
-			}
-		);
-	});
-}