X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fcore%2Fabstractsqlstorage.cpp;h=10c0f50dc653e044f001d9a9dd28997ad1ea866b;hp=c100d4ef008ac797ab6ec6a573541c9164202b34;hb=158443f71d48215eea8b47b836b61afd77654b78;hpb=3cf5c49e7753be48a994917fe898730add4292b1 diff --git a/src/core/abstractsqlstorage.cpp b/src/core/abstractsqlstorage.cpp index c100d4ef..10c0f50d 100644 --- a/src/core/abstractsqlstorage.cpp +++ b/src/core/abstractsqlstorage.cpp @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2005-2016 by the Quassel Project * + * Copyright (C) 2005-2018 by the Quassel Project * * devel@quassel-irc.org * * * * This program is free software; you can redistribute it and/or modify * @@ -19,9 +19,6 @@ ***************************************************************************/ #include "abstractsqlstorage.h" -#include "quassel.h" - -#include "logger.h" #include #include @@ -29,10 +26,12 @@ #include #include +#include "logmessage.h" +#include "quassel.h" + int AbstractSqlStorage::_nextConnectionId = 0; AbstractSqlStorage::AbstractSqlStorage(QObject *parent) - : Storage(parent), - _schemaVersion(0) + : Storage(parent) { } @@ -43,7 +42,7 @@ AbstractSqlStorage::~AbstractSqlStorage() QHash::iterator conIter; for (conIter = _connectionPool.begin(); conIter != _connectionPool.end(); ++conIter) { QSqlDatabase::removeDatabase(conIter.value()->name()); - disconnect(conIter.value(), 0, this, 0); + disconnect(conIter.value(), nullptr, this, nullptr); } } @@ -116,9 +115,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,14 +138,22 @@ 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())); - if (!upgradeDb()) { + quInfo() << qPrintable(tr("Installed database schema (version %1) is not up to date. Upgrading to " + "version %2... This may take a while for major upgrades." + ).arg(installedSchemaVersion()).arg(schemaVersion())); + emit dbUpgradeInProgress(true); + auto upgradeResult = upgradeDb(); + emit dbUpgradeInProgress(false); + if (!upgradeResult) { qWarning() << qPrintable(tr("Upgrade failed...")); return NotAvailable; } + // Add a message when migration succeeds to avoid confusing folks by implying the schema upgrade failed if + // later functionality does not work. + quInfo() << qPrintable(tr("Installed database schema successfully upgraded to version %1.").arg(schemaVersion())); } - quInfo() << qPrintable(displayName()) << "Storage Backend is ready. Quassel Schema Version:" << installedSchemaVersion(); + quInfo() << qPrintable(displayName()) << "storage backend is ready. Schema version:" << installedSchemaVersion(); return IsReady; } @@ -192,9 +201,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 +248,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; } @@ -360,7 +401,6 @@ AbstractSqlStorage::Connection::~Connection() // AbstractSqlMigrator // ======================================== AbstractSqlMigrator::AbstractSqlMigrator() - : _query(0) { } @@ -376,7 +416,7 @@ void AbstractSqlMigrator::newQuery(const QString &query, QSqlDatabase db) void AbstractSqlMigrator::resetQuery() { delete _query; - _query = 0; + _query = nullptr; } @@ -409,6 +449,8 @@ QString AbstractSqlMigrator::migrationObject(MigrationObject moType) return "IrcServer"; case UserSetting: return "UserSetting"; + case CoreState: + return "CoreState"; }; return QString(); } @@ -445,8 +487,7 @@ void AbstractSqlMigrator::dumpStatus() // AbstractSqlMigrationReader // ======================================== AbstractSqlMigrationReader::AbstractSqlMigrationReader() - : AbstractSqlMigrator(), - _writer(0) + : AbstractSqlMigrator() { } @@ -502,6 +543,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(); @@ -526,7 +571,7 @@ void AbstractSqlMigrationReader::abortMigration(const QString &errorMsg) rollback(); _writer->rollback(); - _writer = 0; + _writer = nullptr; } @@ -537,10 +582,10 @@ bool AbstractSqlMigrationReader::finalizeMigration() commit(); if (!_writer->commit()) { - _writer = 0; + _writer = nullptr; return false; } - _writer = 0; + _writer = nullptr; return true; } @@ -584,3 +629,13 @@ bool AbstractSqlMigrationReader::transferMo(MigrationObject moType, T &mo) qDebug() << "Done."; return true; } + +uint qHash(const SenderData &key) { + return qHash(QString(key.sender + "\n" + key.realname + "\n" + key.avatarurl)); +} + +bool operator==(const SenderData &a, const SenderData &b) { + return a.sender == b.sender && + a.realname == b.realname && + a.avatarurl == b.avatarurl; +}