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
commit 60dfec3375
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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,
"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]",
@ -8,6 +10,9 @@
"behindProxy": true,
"db": "./databases/sponsorTimes.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",
"readOnly": false
}
}

View file

@ -21,7 +21,8 @@ YouTubeAPI.authenticate({
var Sqlite3 = require('better-sqlite3');
let options = {
readonly: config.readOnly
readonly: config.readOnly,
fileMustExist: !config.createDatabaseIfNotExist
};
//load database
@ -29,6 +30,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.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.
http.createServer(app).listen(config.port);
@ -1055,4 +1061,4 @@ function getFormattedTime(seconds) {
let formatted = minutes+ ":" + secondsDisplay;
return formatted;
}
}