From d3d1fe6a5c1b3739681860fb1a5bb54a003e3ff9 Mon Sep 17 00:00:00 2001 From: Guillaume Bour Date: Sun, 2 Aug 2020 21:35:25 +0200 Subject: [PATCH] [fix] add new db fields (tag, ...) when creating new database Also create those fields for previous installs in error. --- harbour-nextinpact.pro | 2 +- src/database.cpp | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/harbour-nextinpact.pro b/harbour-nextinpact.pro index 6ed042f..657c233 100644 --- a/harbour-nextinpact.pro +++ b/harbour-nextinpact.pro @@ -17,7 +17,7 @@ DEFINES += APP_VERSION=\\\"$$VERSION\\\" # get build date (timestamp) with: python -c 'import time; print(int(time.time()))' DEFINES += BUILD_DATE='1594726620' -DBVERSION = 4 +DBVERSION = 5 DBNAME = 'nextinpact.db' #DBNAME = 'nextinpact-pre.db' #DBNAME = 'nextinpact-dev.db' diff --git a/src/database.cpp b/src/database.cpp index ad67aef..a287a3c 100644 --- a/src/database.cpp +++ b/src/database.cpp @@ -96,6 +96,10 @@ bool Database::init() "new_comments INTEGER DEFAULT 1," // parent article ('brief' articles only, -1 for no parent) "parent INTEGER DEFAULT -1," + "tag TEXT," + "subtag TEXT," + "star INTEGER," // boolean: 0=false, 1=true + "subscriber INTEGER," "PRIMARY KEY (id, type)" ")"); qDebug() << query.lastError().text(); @@ -198,6 +202,33 @@ bool Database::migrate() { } } + /* forgot to add new v4 fields (tag, subtag, ...) in full create-table query + * So new users that just installed the app after 0.6.8 release (no previously exist db) + * had not those fields + * + * To fix that: + * - incrementing DB VERSION from 4 to 5 + * - creating same fields than for version 4 + * - increase stored version number to 5 even if fields creation failed + */ + if (version < 5) { + QStringList queries = { + "ALTER TABLE articles ADD COLUMN tag TEXT", + "ALTER TABLE articles ADD COLUMN subtag TEXT", + "ALTER TABLE articles ADD COLUMN star INTEGER", // boolean: 0=false, 1=true + "ALTER TABLE articles ADD COLUMN subscriber INTEGER" // boolean. access restricted (full access to subscribers) + //TODO: create index + }; + if (!updater->exec(5, queries)) { + q.prepare("UPDATE config SET value='5' WHERE key='version'"); + if (!q.exec()) { + qDebug() << "upgrade version field failed:" << q.lastError().text(); + return false; + } + q.finish(); + } + } + return true; }