Browse Source

migrate(Playlists): added migration for changes in how song position in playlists is handled

Signed-off-by: Jonathan <theflametrooper@gmail.com>
Jonathan 3 years ago
parent
commit
53dfeab017
1 changed files with 62 additions and 0 deletions
  1. 62 0
      backend/logic/migration/migrations/migration10.js

+ 62 - 0
backend/logic/migration/migrations/migration10.js

@@ -0,0 +1,62 @@
+import async from "async";
+
+/**
+ * Migration 10
+ *
+ * Migration for changes in how the order of songs in a playlist is handled
+ *
+ * @param {object} MigrationModule - the MigrationModule
+ * @returns {Promise} - returns promise
+ */
+export default async function migrate(MigrationModule) {
+	const playlistModel = await MigrationModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
+
+	return new Promise((resolve, reject) => {
+		async.waterfall(
+			[
+				next => {
+					this.log("INFO", `Migration 10. Finding playlists with document version 3.`);
+					playlistModel.find({ documentVersion: 3 }, (err, playlists) => {
+						if (err) next(err);
+						else {
+							async.eachLimit(
+								playlists.map(playlisti => playlisti._doc),
+								1,
+								(playlisti, next) => {
+									// sort playlists by the position property
+									playlisti.songs.sort((song1, song2) => song1.position - song2.position);
+
+									// delete the position property for each song
+									playlisti.songs.forEach(song => delete song.position);
+
+									// update the database
+									playlistModel.updateOne(
+										{ _id: playlisti._id },
+										{
+											$set: {
+												songs: playlisti.songs,
+												documentVersion: 4
+											}
+										},
+										next
+									);
+								},
+								err => {
+									if (err) next(err);
+									else {
+										this.log("INFO", `Migration 10. Playlists found: ${playlists.length}.`);
+										next();
+									}
+								}
+							);
+						}
+					});
+				}
+			],
+			err => {
+				if (err) reject(new Error(err));
+				else resolve();
+			}
+		);
+	});
+}