X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fcore%2Fabstractsqlstorage.cpp;h=fce1bb1d17fc69df980aae3faa897972dbcc9420;hp=4ecf623bc592f728059c77bbf35f7cb41dc2e3ff;hb=f10304a35af0a7a4f8b812e467e69287d358ce7c;hpb=20f446a492d8e681156423f0dc3637db78c45bae diff --git a/src/core/abstractsqlstorage.cpp b/src/core/abstractsqlstorage.cpp index 4ecf623b..fce1bb1d 100644 --- a/src/core/abstractsqlstorage.cpp +++ b/src/core/abstractsqlstorage.cpp @@ -116,9 +116,11 @@ void AbstractSqlStorage::dbConnect(QSqlDatabase &db) } -Storage::State AbstractSqlStorage::init(const QVariantMap &settings) +Storage::State AbstractSqlStorage::init(const QVariantMap &settings, + const QProcessEnvironment &environment, + bool loadFromEnvironment) { - setConnectionProperties(settings); + setConnectionProperties(settings, environment, loadFromEnvironment); _debug = Quassel::isOptionSet("debug"); @@ -137,11 +139,20 @@ Storage::State AbstractSqlStorage::init(const QVariantMap &settings) } if (installedSchemaVersion() < schemaVersion()) { - qWarning() << qPrintable(tr("Installed Schema (version %1) is not up to date. Upgrading to version %2...").arg(installedSchemaVersion()).arg(schemaVersion())); + qWarning() << qPrintable(tr("Installed Schema (version %1) is not up to date. Upgrading to " + "version %2... This may take a while for major upgrades." + ).arg(installedSchemaVersion()).arg(schemaVersion())); + // TODO: The monolithic client won't show this message unless one looks into the debug log. + // This should be made more friendly, e.g. a popup message in the GUI. if (!upgradeDb()) { qWarning() << qPrintable(tr("Upgrade failed...")); return NotAvailable; } + // Warning messages are also sent to the console, while Info messages aren't. Add a message + // when migration succeeds to avoid confusing folks by implying the schema upgrade failed if + // later functionality does not work. + qWarning() << qPrintable(tr("Installed Schema successfully upgraded to version %1." + ).arg(schemaVersion())); } quInfo() << qPrintable(displayName()) << "storage backend is ready. Schema version:" << installedSchemaVersion(); @@ -192,9 +203,10 @@ QStringList AbstractSqlStorage::setupQueries() } -bool AbstractSqlStorage::setup(const QVariantMap &settings) +bool AbstractSqlStorage::setup(const QVariantMap &settings, const QProcessEnvironment &environment, + bool loadFromEnvironment) { - setConnectionProperties(settings); + setConnectionProperties(settings, environment, loadFromEnvironment); QSqlDatabase db = logDb(); if (!db.isOpen()) { qCritical() << "Unable to setup Logging Backend!"; @@ -238,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; } @@ -409,6 +452,8 @@ QString AbstractSqlMigrator::migrationObject(MigrationObject moType) return "IrcServer"; case UserSetting: return "UserSetting"; + case CoreState: + return "CoreState"; }; return QString(); } @@ -502,6 +547,10 @@ bool AbstractSqlMigrationReader::migrateTo(AbstractSqlMigrationWriter *writer) if (!transferMo(UserSetting, userSettingMo)) return false; + CoreStateMO coreStateMO; + if (!transferMo(CoreState, coreStateMO)) + return false; + if (!_writer->postProcess()) abortMigration(); return finalizeMigration();