1
mirror of https://github.com/qbittorrent/qBittorrent synced 2024-11-19 12:27:16 +01:00

Correctly check for database needs to be updated

* Correctly check for database needs to be updated
* Create index only if not exists
* Double check whether database needs to be updated

PR #18638.
This commit is contained in:
Vladimir Golovnev 2023-03-02 20:31:38 +03:00 committed by GitHub
parent 37c04fdeed
commit ce9bdaef5c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -272,7 +272,7 @@ BitTorrent::DBResumeDataStorage::DBResumeDataStorage(const Path &dbPath, QObject
else else
{ {
const int dbVersion = (!db.record(DB_TABLE_TORRENTS).contains(DB_COLUMN_DOWNLOAD_PATH.name) ? 1 : currentDBVersion()); const int dbVersion = (!db.record(DB_TABLE_TORRENTS).contains(DB_COLUMN_DOWNLOAD_PATH.name) ? 1 : currentDBVersion());
if (dbVersion != DB_VERSION) if (dbVersion < DB_VERSION)
updateDB(dbVersion); updateDB(dbVersion);
} }
@ -545,24 +545,34 @@ void BitTorrent::DBResumeDataStorage::updateDB(const int fromVersion) const
{ {
if (fromVersion == 1) if (fromVersion == 1)
{ {
const auto alterTableTorrentsQuery = u"ALTER TABLE %1 ADD %2"_qs const auto testQuery = u"SELECT COUNT(%1) FROM %2;"_qs
.arg(quoted(DB_TABLE_TORRENTS), makeColumnDefinition(DB_COLUMN_DOWNLOAD_PATH, "TEXT")); .arg(quoted(DB_COLUMN_DOWNLOAD_PATH.name), quoted(DB_TABLE_TORRENTS));
if (!query.exec(alterTableTorrentsQuery)) if (!query.exec(testQuery))
throw RuntimeError(query.lastError().text()); {
const auto alterTableTorrentsQuery = u"ALTER TABLE %1 ADD %2"_qs
.arg(quoted(DB_TABLE_TORRENTS), makeColumnDefinition(DB_COLUMN_DOWNLOAD_PATH, "TEXT"));
if (!query.exec(alterTableTorrentsQuery))
throw RuntimeError(query.lastError().text());
}
} }
if (fromVersion <= 2) if (fromVersion <= 2)
{ {
const auto alterTableTorrentsQuery = u"ALTER TABLE %1 ADD %2"_qs const auto testQuery = u"SELECT COUNT(%1) FROM %2;"_qs
.arg(quoted(DB_TABLE_TORRENTS), makeColumnDefinition(DB_COLUMN_STOP_CONDITION, "TEXT NOT NULL DEFAULT `None`")); .arg(quoted(DB_COLUMN_STOP_CONDITION.name), quoted(DB_TABLE_TORRENTS));
if (!query.exec(alterTableTorrentsQuery)) if (!query.exec(testQuery))
throw RuntimeError(query.lastError().text()); {
const auto alterTableTorrentsQuery = u"ALTER TABLE %1 ADD %2"_qs
.arg(quoted(DB_TABLE_TORRENTS), makeColumnDefinition(DB_COLUMN_STOP_CONDITION, "TEXT NOT NULL DEFAULT `None`"));
if (!query.exec(alterTableTorrentsQuery))
throw RuntimeError(query.lastError().text());
}
} }
if (fromVersion <= 3) if (fromVersion <= 3)
{ {
const QString torrentsQueuePositionIndexName = u"%1_%2_INDEX"_qs.arg(DB_TABLE_TORRENTS, DB_COLUMN_QUEUE_POSITION.name); const QString torrentsQueuePositionIndexName = u"%1_%2_INDEX"_qs.arg(DB_TABLE_TORRENTS, DB_COLUMN_QUEUE_POSITION.name);
const QString createTorrentsQueuePositionIndexQuery = u"CREATE INDEX %1 ON %2 (%3)"_qs const QString createTorrentsQueuePositionIndexQuery = u"CREATE INDEX IF NOT EXISTS %1 ON %2 (%3)"_qs
.arg(quoted(torrentsQueuePositionIndexName), quoted(DB_TABLE_TORRENTS), quoted(DB_COLUMN_QUEUE_POSITION.name)); .arg(quoted(torrentsQueuePositionIndexName), quoted(DB_TABLE_TORRENTS), quoted(DB_COLUMN_QUEUE_POSITION.name));
if (!query.exec(createTorrentsQueuePositionIndexQuery)) if (!query.exec(createTorrentsQueuePositionIndexQuery))
throw RuntimeError(query.lastError().text()); throw RuntimeError(query.lastError().text());