mirror of
https://github.com/YACReader/yacreader
synced 2025-11-16 23:12:53 -05:00
Make sure DB upgrades happen atomically
This commit is contained in:
@ -901,8 +901,11 @@ bool DataBaseManagement::updateToCurrentVersion(const QString &libraryPath)
|
||||
{
|
||||
QSqlDatabase db = loadDatabaseFromFile(libraryDatabasePath);
|
||||
if (db.isValid() && db.isOpen()) {
|
||||
if (pre7) // TODO: execute only if previous version was < 7.0
|
||||
{
|
||||
if (!db.transaction()) {
|
||||
QLOG_ERROR() << "Failed to start transaction for database update";
|
||||
returnValue = false;
|
||||
} else {
|
||||
if (pre7) {
|
||||
// new 7.0 fields
|
||||
QStringList columnDefs;
|
||||
columnDefs << "hasBeenOpened BOOLEAN DEFAULT 0"
|
||||
@ -918,7 +921,6 @@ bool DataBaseManagement::updateToCurrentVersion(const QString &libraryPath)
|
||||
bool successAddingColumns = addColumns("comic_info", columnDefs, db);
|
||||
returnValue = returnValue && successAddingColumns;
|
||||
}
|
||||
// TODO update hasBeenOpened value
|
||||
|
||||
if (pre7_1) {
|
||||
{
|
||||
@ -1068,8 +1070,6 @@ bool DataBaseManagement::updateToCurrentVersion(const QString &libraryPath)
|
||||
pragmaFKOFF.prepare("PRAGMA foreign_keys=OFF");
|
||||
pre9_14_successfulMigration = pre9_14_successfulMigration && pragmaFKOFF.exec();
|
||||
|
||||
db.transaction();
|
||||
|
||||
pre9_14_successfulMigration = pre9_14_successfulMigration && createComicInfoTable(db, "comic_info_migration");
|
||||
|
||||
QSqlQuery copyComicInfoToComicInfoMigration(db);
|
||||
@ -1084,11 +1084,6 @@ bool DataBaseManagement::updateToCurrentVersion(const QString &libraryPath)
|
||||
renameComicInfoMigrationToComicInfo.prepare("ALTER TABLE comic_info_migration RENAME TO comic_info");
|
||||
pre9_14_successfulMigration = pre9_14_successfulMigration && renameComicInfoMigrationToComicInfo.exec();
|
||||
|
||||
if (pre9_14_successfulMigration)
|
||||
db.commit();
|
||||
else
|
||||
db.rollback();
|
||||
|
||||
QSqlQuery pragmaFKON1("PRAGMA foreign_keys=ON", db);
|
||||
|
||||
returnValue = returnValue && pre9_14_successfulMigration;
|
||||
@ -1121,6 +1116,16 @@ bool DataBaseManagement::updateToCurrentVersion(const QString &libraryPath)
|
||||
returnValue = updateVersion.numRowsAffected() > 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (returnValue) {
|
||||
if (!db.commit()) {
|
||||
QLOG_ERROR() << "Failed to commit transaction for database update";
|
||||
returnValue = false;
|
||||
}
|
||||
} else {
|
||||
db.rollback();
|
||||
}
|
||||
}
|
||||
connectionName = db.connectionName();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user