From 7acb19756b30d8b269b798b898a68d2f350629e9 Mon Sep 17 00:00:00 2001 From: Joe Dowd Date: Thu, 13 Feb 2020 00:03:09 +0000 Subject: [PATCH 1/5] Create DB form schema on start if config option is set --- config.json.example | 5 ++++- index.js | 7 ++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/config.json.example b/config.json.example index d771410..b90e11e 100644 --- a/config.json.example +++ b/config.json.example @@ -8,6 +8,9 @@ "behindProxy": true, "db": "./databases/sponsorTimes.db", "privateDB": "./databases/private.db", + "createDatabaseIfNotExist": true, //depends on mode='development' //This will run on startup every time - so ensure "create table if not exists" is used in the schema + "dbSchema": "./databases/_sponsorTimes.db.sql", + "privateDBSchema": "./databases/_private.db.sql", "mode": "development", "readOnly": false -} \ No newline at end of file +} diff --git a/index.js b/index.js index e0c993f..73727ba 100644 --- a/index.js +++ b/index.js @@ -29,6 +29,11 @@ var db = new Sqlite3(config.db, options); //where the more sensitive data such as IP addresses are stored var privateDB = new Sqlite3(config.privateDB, options); +if (config.createDatabaseIfNotExist && (config.mode === "development")) { + db.exec(fs.readFileSync(config.dbSchema).toString()); + privateDB.exec(fs.readFileSync(config.privateDBSchema).toString()); +} + // Create an HTTP service. http.createServer(app).listen(config.port); @@ -1049,4 +1054,4 @@ function getFormattedTime(seconds) { let formatted = minutes+ ":" + secondsDisplay; return formatted; -} \ No newline at end of file +} From 1900d1ae52c02bf551653f331230d5948d6e8185 Mon Sep 17 00:00:00 2001 From: Joe Dowd Date: Sat, 15 Feb 2020 20:52:33 +0000 Subject: [PATCH 2/5] Check config.readonly instead of config.mode as condition to creading tables from schema files --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 73727ba..5ecce38 100644 --- a/index.js +++ b/index.js @@ -29,7 +29,7 @@ var db = new Sqlite3(config.db, options); //where the more sensitive data such as IP addresses are stored var privateDB = new Sqlite3(config.privateDB, options); -if (config.createDatabaseIfNotExist && (config.mode === "development")) { +if (config.createDatabaseIfNotExist && !config.readOnly) { db.exec(fs.readFileSync(config.dbSchema).toString()); privateDB.exec(fs.readFileSync(config.privateDBSchema).toString()); } From 97b2d2d56147b434622a3c8a7916e9fa5300b919 Mon Sep 17 00:00:00 2001 From: Joe Dowd Date: Sat, 15 Feb 2020 21:02:56 +0000 Subject: [PATCH 3/5] Updated config comment to match changed condition for running schema script --- config.json.example | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.json.example b/config.json.example index b90e11e..0d76f8b 100644 --- a/config.json.example +++ b/config.json.example @@ -8,7 +8,7 @@ "behindProxy": true, "db": "./databases/sponsorTimes.db", "privateDB": "./databases/private.db", - "createDatabaseIfNotExist": true, //depends on mode='development' //This will run on startup every time - so ensure "create table if not exists" is used in the schema + "createDatabaseIfNotExist": true, //This will run on startup every time (unless readOnly is true) - so ensure "create table if not exists" is used in the schema "dbSchema": "./databases/_sponsorTimes.db.sql", "privateDBSchema": "./databases/_private.db.sql", "mode": "development", From a8cbc4fbdb8c5af8bb792a979bd9001bd94284ba Mon Sep 17 00:00:00 2001 From: Joe Dowd Date: Tue, 10 Mar 2020 02:10:53 +0000 Subject: [PATCH 4/5] Stopped initial DB from being created when missing if config option set to false. Checked for existence of schema files before executing the schema. --- index.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 5ecce38..c867049 100644 --- a/index.js +++ b/index.js @@ -21,7 +21,8 @@ YouTubeAPI.authenticate({ var Sqlite3 = require('better-sqlite3'); let options = { - readonly: config.readOnly + readonly: config.readOnly, + fileMustExist: !config.createDatabaseIfNotExist }; //load database @@ -30,8 +31,8 @@ var db = new Sqlite3(config.db, options); var privateDB = new Sqlite3(config.privateDB, options); if (config.createDatabaseIfNotExist && !config.readOnly) { - db.exec(fs.readFileSync(config.dbSchema).toString()); - privateDB.exec(fs.readFileSync(config.privateDBSchema).toString()); + if (fs.existsSync(config.dbSchema)) db.exec(fs.readFileSync(config.dbSchema).toString()); + if (fs.existsSync(config.privateDBSchema)) privateDB.exec(fs.readFileSync(config.privateDBSchema).toString()); } // Create an HTTP service. From da47098ccd47b008dd87a56313b5950fe260a38a Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Thu, 26 Mar 2020 21:58:21 -0400 Subject: [PATCH 5/5] Added info to config example --- config.json.example | 2 ++ 1 file changed, 2 insertions(+) diff --git a/config.json.example b/config.json.example index 0d76f8b..b8a9d79 100644 --- a/config.json.example +++ b/config.json.example @@ -1,4 +1,6 @@ { + // Remove all comments from the config when using it! + "port": 80, "globalSalt": "[global salt (pepper) that is added to every ip before hashing to make it even harder for someone to decode the ip]", "adminUserID": "[the hashed id of the user who can perform admin actions]",