Skip to content

Commit

Permalink
Add check for database schema version on database open
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
Chris Adams committed Apr 15, 2019
1 parent c7bcc05 commit e0a0fc6
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/engine/contactsdatabase.cpp
Expand Up @@ -3024,6 +3024,25 @@ bool ContactsDatabase::open(const QString &connectionName, bool nonprivileged, b
m_database.close();
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
Expand Down

0 comments on commit e0a0fc6

Please sign in to comment.