diff --git a/src/persistence/db/upgrades/dbupgrader.cpp b/src/persistence/db/upgrades/dbupgrader.cpp index 170c186ca..d0db75a35 100644 --- a/src/persistence/db/upgrades/dbupgrader.cpp +++ b/src/persistence/db/upgrades/dbupgrader.cpp @@ -31,23 +31,13 @@ namespace { constexpr int SCHEMA_VERSION = 11; -struct BadEntry +std::vector getInvalidPeers(RawDatabase& db) { - BadEntry(int64_t row_, QString toxId_) - : row{row_} - , toxId{toxId_} - {} - RowId row; - QString toxId; -}; - -std::vector getInvalidPeers(RawDatabase& db) -{ - std::vector badPeerIds; + std::vector badPeerIds; db.execNow( RawDatabase::Query("SELECT id, public_key FROM peers WHERE LENGTH(public_key) != 64", [&](const QVector& row) { - badPeerIds.emplace_back(BadEntry{row[0].toInt(), row[1].toString()}); + badPeerIds.emplace_back(DbUpgrader::BadEntry{row[0].toInt(), row[1].toString()}); })); return badPeerIds; } @@ -147,17 +137,6 @@ void mergeAndDeletePeer(QVector& upgradeQueries, RowId goodP RawDatabase::Query(QStringLiteral("DELETE FROM peers WHERE id = %1").arg(badPeerId.get())); } -void mergeDuplicatePeers(QVector& upgradeQueries, RawDatabase& db, - std::vector badPeers) -{ - for (const auto& badPeer : badPeers) { - const RowId goodPeerId = getValidPeerRow(db, ToxPk{badPeer.toxId.left(64)}); - const auto aliasDuplicates = getDuplicateAliasRows(db, goodPeerId, badPeer.row); - mergeAndDeleteAlias(upgradeQueries, aliasDuplicates.goodAliasRow, aliasDuplicates.badAliasRows); - mergeAndDeletePeer(upgradeQueries, goodPeerId, badPeer.row); - } -} - void addForeignKeyToAlias(QVector& queries) { queries += RawDatabase::Query( @@ -638,3 +617,14 @@ bool DbUpgrader::dbSchema9to10(RawDatabase& db) upgradeQueries += RawDatabase::Query(QStringLiteral("PRAGMA user_version = 10;")); return db.execNow(upgradeQueries); } + +void DbUpgrader::mergeDuplicatePeers(QVector& upgradeQueries, RawDatabase& db, + std::vector badPeers) +{ + for (const auto& badPeer : badPeers) { + const RowId goodPeerId = getValidPeerRow(db, ToxPk{badPeer.toxId.left(64)}); + const auto aliasDuplicates = getDuplicateAliasRows(db, goodPeerId, badPeer.row); + mergeAndDeleteAlias(upgradeQueries, aliasDuplicates.goodAliasRow, aliasDuplicates.badAliasRows); + mergeAndDeletePeer(upgradeQueries, goodPeerId, badPeer.row); + } +} diff --git a/src/persistence/db/upgrades/dbupgrader.h b/src/persistence/db/upgrades/dbupgrader.h index cf28c228f..895678b9b 100644 --- a/src/persistence/db/upgrades/dbupgrader.h +++ b/src/persistence/db/upgrades/dbupgrader.h @@ -21,6 +21,8 @@ #include +#include "src/persistence/db/rawdatabase.h" + class RawDatabase; class IMessageBoxManager; namespace DbUpgrader @@ -40,4 +42,16 @@ namespace DbUpgrader bool dbSchema8to9(RawDatabase& db); bool dbSchema9to10(RawDatabase& db); // 10to11 from DbTo11::dbSchema10to11 + + struct BadEntry + { + BadEntry(int64_t row_, QString toxId_) + : row{row_} + , toxId{toxId_} + {} + RowId row; + QString toxId; + }; + void mergeDuplicatePeers(QVector& upgradeQueries, RawDatabase& db, + std::vector badPeers); }