core: Allow clean shutdown of the core
[quassel.git] / src / core / abstractsqlstorage.cpp
index 9fe95b9..149aabb 100644 (file)
@@ -19,9 +19,6 @@
  ***************************************************************************/
 
 #include "abstractsqlstorage.h"
-#include "quassel.h"
-
-#include "logger.h"
 
 #include <QMutexLocker>
 #include <QSqlDriver>
@@ -29,6 +26,9 @@
 #include <QSqlField>
 #include <QSqlQuery>
 
+#include "logmessage.h"
+#include "quassel.h"
+
 int AbstractSqlStorage::_nextConnectionId = 0;
 AbstractSqlStorage::AbstractSqlStorage(QObject *parent)
     : Storage(parent),
@@ -139,11 +139,19 @@ 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. Schema version:" << installedSchemaVersion();
@@ -241,16 +249,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;
+        }
+    }
+
+    // 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;
     }
-    return updateSchemaVersion(schemaVersion());
+
+    // If we made it here, everything seems to have worked!
+    return true;
 }