X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fcore%2Fsqlitestorage.cpp;h=893bd77496df4355f97f1d522333326a9e5b35af;hp=909c8177e29b4fff11d15cbce6fc3a040463db6e;hb=de04705745cf92be498a1273fdc4abf84d2649c9;hpb=cc6e7c08709c4e761e2fd9c2e322751015497003 diff --git a/src/core/sqlitestorage.cpp b/src/core/sqlitestorage.cpp index 909c8177..893bd774 100644 --- a/src/core/sqlitestorage.cpp +++ b/src/core/sqlitestorage.cpp @@ -75,21 +75,39 @@ int SqliteStorage::installedSchemaVersion() return AbstractSqlStorage::installedSchemaVersion(); } -bool SqliteStorage::updateSchemaVersion(int newVersion) +bool SqliteStorage::updateSchemaVersion(int newVersion, bool clearUpgradeStep) { // only used when there is a singlethread (during startup) // so we don't need locking here - QSqlQuery query(logDb()); + + QSqlDatabase db = logDb(); + + // Atomically update the schema version and clear the upgrade step, if specified + // Note: This will need reworked if "updateSchemaVersion" is ever called within a transaction. + db.transaction(); + + QSqlQuery query(db); query.prepare("UPDATE coreinfo SET value = :version WHERE key = 'schemaversion'"); query.bindValue(":version", newVersion); - query.exec(); + safeExec(query); - bool success = true; - if (query.lastError().isValid()) { - qCritical() << "SqliteStorage::updateSchemaVersion(int): Updating schema version failed!"; - success = false; + if (!watchQuery(query)) { + qCritical() << "SqliteStorage::updateSchemaVersion(int, bool): Updating schema version failed!"; + db.rollback(); + return false; } - return success; + + if (clearUpgradeStep) { + // Try clearing the upgrade step if requested + if (!setSchemaVersionUpgradeStep("")) { + db.rollback(); + return false; + } + } + + // Successful, commit and return true + db.commit(); + return true; } bool SqliteStorage::setupSchemaVersion(int version) @@ -109,6 +127,52 @@ bool SqliteStorage::setupSchemaVersion(int version) return success; } +QString SqliteStorage::schemaVersionUpgradeStep() +{ + // Only used when there is a singlethread (during startup), so we don't need locking here + QSqlQuery query(logDb()); + query.prepare("SELECT value FROM coreinfo WHERE key = 'schemaupgradestep'"); + safeExec(query); + watchQuery(query); + if (query.first()) + return query.value(0).toString(); + + // Fall back to the default value + return AbstractSqlStorage::schemaVersionUpgradeStep(); +} + +bool SqliteStorage::setSchemaVersionUpgradeStep(QString upgradeQuery) +{ + // Only used when there is a singlethread (during startup), so we don't need locking here + + // Intentionally do not wrap in a transaction so other functions can include multiple operations + QSqlQuery query(logDb()); + query.prepare("UPDATE coreinfo SET value = :upgradestep WHERE key = 'schemaupgradestep'"); + query.bindValue(":upgradestep", upgradeQuery); + safeExec(query); + + // Don't wrap with watchQuery to avoid an alarming message in the log when the key is missing + // Make sure that the query didn't fail, and that some non-zero number of rows were affected + bool success = !query.lastError().isValid() && query.numRowsAffected() != 0; + + if (!success) { + // The key might not exist (Quassel 0.13.0 and older). Try inserting it... + query = QSqlQuery(logDb()); + query.prepare("INSERT INTO coreinfo (key, value) VALUES ('schemaupgradestep', :upgradestep)"); + query.bindValue(":upgradestep", upgradeQuery); + safeExec(query); + + if (!watchQuery(query)) { + qCritical() << Q_FUNC_INFO << "Setting schema upgrade step failed!"; + success = false; + } + else { + success = true; + } + } + return success; +} + UserId SqliteStorage::addUser(const QString& user, const QString& password, const QString& authenticator) { QSqlDatabase db = logDb(); @@ -128,7 +192,7 @@ UserId SqliteStorage::addUser(const QString& user, const QString& password, cons lockForWrite(); safeExec(query); if (query.lastError().isValid() - && query.lastError().number() == 19) { // user already exists - sadly 19 seems to be the general constraint violation error... + && query.lastError().nativeErrorCode() == "19") { // user already exists - sadly 19 seems to be the general constraint violation error... db.rollback(); } else { @@ -1345,7 +1409,7 @@ bool SqliteStorage::renameBuffer(const UserId& user, const BufferId& bufferId, c error = query.lastError().isValid(); // unexepcted error occured (19 == constraint violation) - if (error && query.lastError().number() != 19) { + if (error && query.lastError().nativeErrorCode() != "19") { watchQuery(query); } else { @@ -1726,7 +1790,7 @@ bool SqliteStorage::logMessage(Message& msg) if (logMessageQuery.lastError().isValid()) { // constraint violation - must be NOT NULL constraint - probably the sender is missing... - if (logMessageQuery.lastError().number() == 19) { + if (logMessageQuery.lastError().nativeErrorCode() == "19") { QSqlQuery addSenderQuery(db); addSenderQuery.prepare(queryString("insert_sender")); addSenderQuery.bindValue(":sender", msg.sender()); @@ -2155,15 +2219,13 @@ bool SqliteStorage::safeExec(QSqlQuery& query, int retryCount) if (!query.lastError().isValid()) return true; - switch (query.lastError().number()) { - case 5: // SQLITE_BUSY 5 /* The database file is locked */ - // fallthrough - case 6: // SQLITE_LOCKED 6 /* A table in the database is locked */ + QString nativeErrorCode = query.lastError().nativeErrorCode(); + + // SQLITE_BUSY 5 /* The database file is locked */ + // SQLITE_LOCKED 6 /* A table in the database is locked */ + if (nativeErrorCode == "5" || nativeErrorCode == "6") { if (retryCount < _maxRetryCount) return safeExec(query, retryCount + 1); - break; - default: - ; } return false; }