Changed from version table to a key-value config table.

This commit is contained in:
Ajay Ramachandran
2020-05-03 12:01:49 -04:00
parent e303405ee0
commit 8eecffd3d5
3 changed files with 11 additions and 6 deletions

View File

@@ -25,8 +25,9 @@ CREATE TABLE IF NOT EXISTS "categoryVotes" (
"votes" INTEGER NOT NULL default '0' "votes" INTEGER NOT NULL default '0'
); );
CREATE TABLE IF NOT EXISTS "version" ( CREATE TABLE IF NOT EXISTS "config" (
"code" INTEGER NOT NULL default '0' "key" TEXT NOT NULL,
"value" TEXT NOT NULL
); );
CREATE INDEX IF NOT EXISTS sponsorTimes_videoID on sponsorTimes(videoID); CREATE INDEX IF NOT EXISTS sponsorTimes_videoID on sponsorTimes(videoID);

View File

@@ -19,7 +19,7 @@ INSERT INTO sqlb_temp_table_1 SELECT videoID,startTime,endTime,votes,"1",UUID,us
DROP TABLE sponsorTimes; DROP TABLE sponsorTimes;
ALTER TABLE sqlb_temp_table_1 RENAME TO "sponsorTimes"; ALTER TABLE sqlb_temp_table_1 RENAME TO "sponsorTimes";
/* Increase version number */ /* Add version to config */
INSERT INTO version VALUES(1); INSERT INTO config (key, value) VALUES("version", 1);
COMMIT; COMMIT;

View File

@@ -26,12 +26,16 @@ if (config.createDatabaseIfNotExist && !config.readOnly) {
// Upgrade database if required // Upgrade database if required
if (!config.readOnly) { if (!config.readOnly) {
let versionCode = db.prepare("SELECT code FROM version").get() || 0; let versionCodeInfo = db.prepare("SELECT value FROM config WHERE key = ?").get("version");
let versionCode = versionCodeInfo ? versionCodeInfo.value : 0;
console.log(versionCode)
let path = config.schemaFolder + "/_upgrade_" + versionCode + ".sql"; let path = config.schemaFolder + "/_upgrade_" + versionCode + ".sql";
while (fs.existsSync(path)) { while (fs.existsSync(path)) {
db.exec(fs.readFileSync(path).toString()); db.exec(fs.readFileSync(path).toString());
versionCode = db.prepare("SELECT code FROM version").get(); versionCode = db.prepare("SELECT value FROM config WHERE key = ?").get("version").value;
path = config.schemaFolder + "/_upgrade_" + versionCode + ".sql"; path = config.schemaFolder + "/_upgrade_" + versionCode + ".sql";
} }
} }