Browse Source

Added document version checking, and option to disable config and document version checking

Kristian Vos 4 years ago
parent
commit
2b32b9fd42

+ 2 - 0
backend/config/template.json

@@ -57,6 +57,8 @@
 		"secure": false,
 		"SIDname": "SID"
 	},
+	"skipConfigVersionCheck": false,
+	"skipDbDocumentsVersionCheck": false,
 	"debug": {
 		"stationIssue": false,
 		"traceUnhandledPromises": false,

+ 7 - 2
backend/index.js

@@ -29,8 +29,13 @@ console.log = (...args) => {
 	if (!blacklisted) oldConsole.log.apply(null, args);
 };
 
-if (!config.has("configVersion") || config.get("configVersion") !== REQUIRED_CONFIG_VERSION) {
-	console.log("CONFIG VERSION IS WRONG. PLEASE UPDATE YOUR CONFIG WITH THE HELP OF THE TEMPLATE FILE AND THE README FILE.");
+if (
+	(!config.has("configVersion") || config.get("configVersion") !== REQUIRED_CONFIG_VERSION) &&
+	!(config.has("skipConfigVersionCheck") && config.get("skipConfigVersionCheck"))
+) {
+	console.log(
+		"CONFIG VERSION IS WRONG. PLEASE UPDATE YOUR CONFIG WITH THE HELP OF THE TEMPLATE FILE AND THE README FILE."
+	);
 	process.exit();
 }
 

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

@@ -1,9 +1,22 @@
 import config from "config";
 import mongoose from "mongoose";
 import bluebird from "bluebird";
+import async from "async";
 
 import CoreClass from "../../core";
 
+const REQUIRED_DOCUMENT_VERSIONS = {
+	activity: 1,
+	news: 1,
+	playlist: 1,
+	punishment: 1,
+	queueSong: 1,
+	report: 1,
+	song: 1,
+	station: 2,
+	user: 1
+};
+
 const regex = {
 	azAZ09_: /^[A-Za-z0-9_]+$/,
 	az09_: /^[a-z0-9_]+$/,
@@ -259,7 +272,16 @@ class _DBModule extends CoreClass {
 							"Invalid description."
 						);
 
-					resolve();
+					if (config.get("skipDbDocumentsVersionCheck")) resolve();
+					else {
+						this.runJob("CHECK_DOCUMENT_VERSIONS", {}, null, -1)
+							.then(() => {
+								resolve();
+							})
+							.catch(err => {
+								reject(err);
+							});
+					}
 				})
 				.catch(err => {
 					this.log("ERROR", err);
@@ -268,6 +290,35 @@ class _DBModule extends CoreClass {
 		});
 	}
 
+	/**
+	 * Checks if all documents have the correct document version
+	 *
+	 * @returns {Promise} - returns promise (reject, resolve)
+	 */
+	CHECK_DOCUMENT_VERSIONS() {
+		return new Promise((resolve, reject) => {
+			async.each(
+				Object.keys(REQUIRED_DOCUMENT_VERSIONS),
+				(modelName, next) => {
+					const model = DBModule.models[modelName];
+					const requiredDocumentVersion = REQUIRED_DOCUMENT_VERSIONS[modelName];
+					model.countDocuments({ documentVersion: { $ne: requiredDocumentVersion } }, (err, count) => {
+						if (err) next(err);
+						else if (count > 0)
+							next(
+								`Collection "${modelName}" has ${count} documents with a wrong document version. Run migration.`
+							);
+						else next();
+					});
+				},
+				err => {
+					if (err) reject(new Error(err));
+					else resolve();
+				}
+			);
+		});
+	}
+
 	/**
 	 * Returns a database model
 	 *

+ 1 - 0
frontend/dist/config/template.json

@@ -17,5 +17,6 @@
 		"siteName": "Musare",
 		"github": "https://github.com/Musare/MusareNode"
 	},
+	"skipConfigVersionCheck": false,
 	"configVersion": 1
 }

+ 5 - 4
frontend/src/main.js

@@ -138,16 +138,17 @@ const router = new VueRouter({
 });
 
 lofig.folder = "../config/default.json";
-lofig.get("configVersion").then(configVersion => {
-	if (configVersion !== REQUIRED_CONFIG_VERSION) {
+lofig.fetchConfig().then(config => {
+	const { configVersion, skipConfigVersionCheck } = config;
+	if (configVersion !== REQUIRED_CONFIG_VERSION && !skipConfigVersionCheck) {
 		// eslint-disable-next-line no-alert
 		alert(
 			"CONFIG VERSION IS WRONG. PLEASE UPDATE YOUR CONFIG WITH THE HELP OF THE TEMPLATE FILE AND THE README FILE."
 		);
 		window.stop();
 	}
-});
-lofig.get("serverDomain").then(serverDomain => {
+
+	const { serverDomain } = config;
 	io.init(serverDomain);
 	io.getSocket(socket => {
 		socket.on("ready", (loggedIn, role, username, userId) => {