Commit e0a0fc6f authored by chriadam's avatar chriadam

Add check for database schema version on database open

If some other process is holding open a database connection, the
database may not have been correctly upgraded to the current
schema version.

Previously, this could result in new processes assuming that
the schema was upgraded, and attempting to perform queries which
fail due to the fact that the real database schema might be
old.

This commit ensures that we detect this case, and print an appropriate
warning so that the user can take the appropriate action
(e.g. kill the offending process, or reboot the device).
parent c7bcc058
...@@ -3024,6 +3024,25 @@ bool ContactsDatabase::open(const QString &connectionName, bool nonprivileged, b ...@@ -3024,6 +3024,25 @@ bool ContactsDatabase::open(const QString &connectionName, bool nonprivileged, b
m_database.close(); m_database.close();
return false; return false;
} }
} else if (databasePreexisting && !databaseOwner) {
// check that the version is correct. If not, it is probably because another process
// with an open database connection is preventing upgrade of the database schema.
QSqlQuery versionQuery(m_database);
versionQuery.prepare("PRAGMA user_version");
if (!versionQuery.exec() || !versionQuery.next()) {
QTCONTACTS_SQLITE_WARNING(QString::fromLatin1("Failed to query existing database schema version: %1").arg(versionQuery.lastError().text()));
m_database.close();
return false;
}
int schemaVersion = versionQuery.value(0).toInt();
if (schemaVersion != currentSchemaVersion) {
QTCONTACTS_SQLITE_WARNING(QString::fromLatin1("Existing database schema version is unexpected: %1 != %2. "
"Is a process preventing schema upgrade?")
.arg(schemaVersion).arg(currentSchemaVersion));
m_database.close();
return false;
}
} }
// Attach to the transient store - any process can create it, but only the primary connection of each // Attach to the transient store - any process can create it, but only the primary connection of each
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment