Skip to content

Commit

Permalink
Fix NULL _id record on db upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
joshirio committed Jan 28, 2020
1 parent 8f6cb5f commit d1d6a91
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
69 changes: 68 additions & 1 deletion components/databasemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,8 @@ bool DatabaseManager::upgradeDatabase(const int oldVersion)
{
//handle database version upgrades
int currentUpgradeVersion = oldVersion;
QSqlQuery query(getDatabase());
QSqlDatabase db = getDatabase();
QSqlQuery query(db);

//upgrade v1 -> v2
if (currentUpgradeVersion == 1) {
Expand Down Expand Up @@ -429,6 +430,72 @@ bool DatabaseManager::upgradeDatabase(const int oldVersion)
}
currentUpgradeVersion = 3;
}
//upgrade v3 -> v4
if (currentUpgradeVersion == 3) {
bool error = false;
QString errorMessage;

//fix inconsistent data sets, see https://github.com/giowck/symphytum/issues/122
//first, check if there are any
//get all collections, including the metadata copy
QStringList collectionsToCheck;
error |= !query.exec("SELECT table_name FROM collections");
while (query.next()) {
QString c = query.value(0).toString();
collectionsToCheck.append(c);
collectionsToCheck.append(c + "_metadata");
}

//check for any _id NULL values
QStringList collectionsToFix;
foreach (QString tableName, collectionsToCheck) {
error |= !query.exec(QString("SELECT _id FROM %1 WHERE _id IS NULL").arg(tableName));
if (query.next()) {
collectionsToFix.append(tableName);
}
}

//correct NULL _id records if any
if (collectionsToFix.size() > 0) {
//start transaction to speed up writes
db.transaction();

foreach (QString tableName, collectionsToFix) {
//get original CREATE TABLE statement
error |= !query.exec(QString("SELECT sql FROM sqlite_master "
"WHERE tbl_name=\"%1\"").arg(tableName));
if (query.next()) {
QString create_sql = query.value(0).toString();
//add PRIMARY KEY constraint
create_sql.replace("_id INT", "_id INTEGER PRIMARY KEY");
//rename table
QString tableNameFixed = tableName + "_fixed";
create_sql.replace(tableName, tableNameFixed);
//create new fixed table
error |= !query.exec(create_sql);
//copy data over to new table
error |= !query.exec(QString("INSERT INTO %1 SELECT * FROM %2")
.arg(tableNameFixed).arg(tableName));
//delete old table
error |= !query.exec(QString("DROP TABLE %1").arg(tableName));
//rename fixed table to original name
error |= !query.exec(QString("ALTER TABLE %1 RENAME TO %2")
.arg(tableNameFixed).arg(tableName));
}
}

//commit transaction
db.commit();
}

if (error) {
QMessageBox::critical(nullptr, QObject::tr("Database Version Upgrade Failed"),
QObject::tr("Error: failed in v3 -> v4"));
return false;
}

currentUpgradeVersion = 4;
}
//add new blocks on new versions here

//upgrade done
Expand Down
2 changes: 1 addition & 1 deletion utils/definitionholder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ QString DefinitionHolder::DOWNLOAD_URL = "https://github.com/giowck/symphytum"
QString DefinitionHolder::HELP_URL = "https://github.com/giowck/symphytum/wiki";
QString DefinitionHolder::DONATE_URL = "https://github.com/giowck/symphytum/blob/master/doc/donate.md";
int DefinitionHolder::SOFTWARE_BUILD = 10;
int DefinitionHolder::DATABASE_VERSION = 3;
int DefinitionHolder::DATABASE_VERSION = 4;
bool DefinitionHolder::APP_STORE = false;
bool DefinitionHolder::APPIMAGE_LINUX = false;
bool DefinitionHolder::WIN_PORTABLE = false;
Expand Down

0 comments on commit d1d6a91

Please sign in to comment.