X-Git-Url: https://git.quassel-irc.org/?a=blobdiff_plain;f=src%2Fcore%2Fabstractsqlstorage.cpp;h=fce1bb1d17fc69df980aae3faa897972dbcc9420;hb=a49af6149b21e0aaf717fc827c55a2c43a2fd778;hp=7e436fce41a12ed1121693d2c43b79c100e05296;hpb=56f686f70bbb212d83803be88adec6fdd225ea8e;p=quassel.git diff --git a/src/core/abstractsqlstorage.cpp b/src/core/abstractsqlstorage.cpp index 7e436fce..fce1bb1d 100644 --- a/src/core/abstractsqlstorage.cpp +++ b/src/core/abstractsqlstorage.cpp @@ -250,16 +250,47 @@ bool AbstractSqlStorage::upgradeDb() QSqlDatabase db = logDb(); + // TODO: For databases that support it (e.g. almost only PostgreSQL), wrap upgrades in a + // transaction. This will need careful testing of potential additional space requirements and + // any database modifications that might not be allowed in a transaction. + for (int ver = installedSchemaVersion() + 1; ver <= schemaVersion(); ver++) { foreach(QString queryString, upgradeQueries(ver)) { QSqlQuery query = db.exec(queryString); if (!watchQuery(query)) { - qCritical() << "Unable to upgrade Logging Backend!"; + // Individual upgrade query failed, bail out + qCritical() << "Unable to upgrade Logging Backend! Upgrade query in schema version" + << ver << "failed."; return false; } } + + // Update the schema version for each intermediate step. This ensures that any interrupted + // upgrades have a greater chance of resuming correctly after core restart. + // + // Almost all databases make single queries atomic (fully works or fully fails, no partial), + // and with many of the longest migrations being a single query, this makes upgrade + // interruptions much more likely to leave the database in a valid intermediate schema + // version. + if (!updateSchemaVersion(ver)) { + // Updating the schema version failed, bail out + qCritical() << "Unable to upgrade Logging Backend! Setting schema version" + << ver << "failed."; + return false; + } } - return updateSchemaVersion(schemaVersion()); + + // Update the schema version for the final step. Split this out to offer more informative + // logging (though setting schema version really should not fail). + if (!updateSchemaVersion(schemaVersion())) { + // Updating the final schema version failed, bail out + qCritical() << "Unable to upgrade Logging Backend! Setting final schema version" + << schemaVersion() << "failed."; + return false; + } + + // If we made it here, everything seems to have worked! + return true; }