Merge pull request #59 from Joe-Dowd/createDBFromSchema

Create DB from schema on start if config option is set
This commit is contained in:
Ajay Ramachandran
2020-03-26 21:58:59 -04:00
committed by GitHub
2 changed files with 14 additions and 3 deletions

View File

@@ -1,4 +1,6 @@
{ {
// Remove all comments from the config when using it!
"port": 80, "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]", "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]", "adminUserID": "[the hashed id of the user who can perform admin actions]",
@@ -8,6 +10,9 @@
"behindProxy": true, "behindProxy": true,
"db": "./databases/sponsorTimes.db", "db": "./databases/sponsorTimes.db",
"privateDB": "./databases/private.db", "privateDB": "./databases/private.db",
"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", "mode": "development",
"readOnly": false "readOnly": false
} }

View File

@@ -21,7 +21,8 @@ YouTubeAPI.authenticate({
var Sqlite3 = require('better-sqlite3'); var Sqlite3 = require('better-sqlite3');
let options = { let options = {
readonly: config.readOnly readonly: config.readOnly,
fileMustExist: !config.createDatabaseIfNotExist
}; };
//load database //load database
@@ -29,6 +30,11 @@ var db = new Sqlite3(config.db, options);
//where the more sensitive data such as IP addresses are stored //where the more sensitive data such as IP addresses are stored
var privateDB = new Sqlite3(config.privateDB, options); var privateDB = new Sqlite3(config.privateDB, options);
if (config.createDatabaseIfNotExist && !config.readOnly) {
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. // Create an HTTP service.
http.createServer(app).listen(config.port); http.createServer(app).listen(config.port);
@@ -1055,4 +1061,4 @@ function getFormattedTime(seconds) {
let formatted = minutes+ ":" + secondsDisplay; let formatted = minutes+ ":" + secondsDisplay;
return formatted; return formatted;
} }