1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

refactor(history): Expose mergeDuplicatePeers from DbUpgrader

To be used by both dbSchema8to9 and dbTo11.
This commit is contained in:
Anthony Bilinski 2022-03-31 20:31:53 -07:00
parent f29570138f
commit 69d07f077e
No known key found for this signature in database
GPG Key ID: 2AA8E0DA1B31FB3C
2 changed files with 28 additions and 24 deletions

View File

@ -31,23 +31,13 @@
namespace {
constexpr int SCHEMA_VERSION = 11;
struct BadEntry
std::vector<DbUpgrader::BadEntry> getInvalidPeers(RawDatabase& db)
{
BadEntry(int64_t row_, QString toxId_)
: row{row_}
, toxId{toxId_}
{}
RowId row;
QString toxId;
};
std::vector<BadEntry> getInvalidPeers(RawDatabase& db)
{
std::vector<BadEntry> badPeerIds;
std::vector<DbUpgrader::BadEntry> badPeerIds;
db.execNow(
RawDatabase::Query("SELECT id, public_key FROM peers WHERE LENGTH(public_key) != 64",
[&](const QVector<QVariant>& 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<RawDatabase::Query>& upgradeQueries, RowId goodP
RawDatabase::Query(QStringLiteral("DELETE FROM peers WHERE id = %1").arg(badPeerId.get()));
}
void mergeDuplicatePeers(QVector<RawDatabase::Query>& upgradeQueries, RawDatabase& db,
std::vector<BadEntry> 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<RawDatabase::Query>& 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<RawDatabase::Query>& upgradeQueries, RawDatabase& db,
std::vector<BadEntry> 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);
}
}

View File

@ -21,6 +21,8 @@
#include <memory>
#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<RawDatabase::Query>& upgradeQueries, RawDatabase& db,
std::vector<BadEntry> badPeers);
}