mirror of
https://github.com/qTox/qTox.git
synced 2024-03-22 14:00:36 +08:00
refactor(history): apply db schema upgrade one version at a time
This allows upgrade steps to query the db at the last version and run C++ code on the results, then do a single transaction to make the upgrade, instead of all actions of each upgrade step being required to be part of the overall upgrade transaction.
This commit is contained in:
parent
996b17b1d1
commit
dacfcdadac
|
@ -28,8 +28,9 @@
|
||||||
namespace {
|
namespace {
|
||||||
static constexpr int SCHEMA_VERSION = 1;
|
static constexpr int SCHEMA_VERSION = 1;
|
||||||
|
|
||||||
void generateCurrentSchema(QVector<RawDatabase::Query>& queries)
|
bool createCurrentSchema(std::shared_ptr<RawDatabase> db)
|
||||||
{
|
{
|
||||||
|
QVector<RawDatabase::Query> queries;
|
||||||
queries += RawDatabase::Query(QStringLiteral(
|
queries += RawDatabase::Query(QStringLiteral(
|
||||||
"CREATE TABLE peers (id INTEGER PRIMARY KEY, "
|
"CREATE TABLE peers (id INTEGER PRIMARY KEY, "
|
||||||
"public_key TEXT NOT NULL UNIQUE);"
|
"public_key TEXT NOT NULL UNIQUE);"
|
||||||
|
@ -61,6 +62,8 @@ void generateCurrentSchema(QVector<RawDatabase::Query>& queries)
|
||||||
"direction INTEGER NOT NULL, "
|
"direction INTEGER NOT NULL, "
|
||||||
"file_state INTEGER NOT NULL);"
|
"file_state INTEGER NOT NULL);"
|
||||||
"CREATE TABLE faux_offline_pending (id INTEGER PRIMARY KEY);"));
|
"CREATE TABLE faux_offline_pending (id INTEGER PRIMARY KEY);"));
|
||||||
|
queries += RawDatabase::Query(QStringLiteral("PRAGMA user_version = %1;").arg(SCHEMA_VERSION));
|
||||||
|
return db->execNow(queries);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isNewDb(std::shared_ptr<RawDatabase> db)
|
bool isNewDb(std::shared_ptr<RawDatabase> db)
|
||||||
|
@ -76,8 +79,9 @@ bool isNewDb(std::shared_ptr<RawDatabase> db)
|
||||||
return newDb;
|
return newDb;
|
||||||
}
|
}
|
||||||
|
|
||||||
void dbSchema0to1(std::shared_ptr<RawDatabase> db, QVector<RawDatabase::Query>& queries)
|
bool dbSchema0to1(std::shared_ptr<RawDatabase> db)
|
||||||
{
|
{
|
||||||
|
QVector<RawDatabase::Query> queries;
|
||||||
queries +=
|
queries +=
|
||||||
RawDatabase::Query(QStringLiteral(
|
RawDatabase::Query(QStringLiteral(
|
||||||
"CREATE TABLE file_transfers "
|
"CREATE TABLE file_transfers "
|
||||||
|
@ -92,6 +96,8 @@ void dbSchema0to1(std::shared_ptr<RawDatabase> db, QVector<RawDatabase::Query>&
|
||||||
"file_state INTEGER NOT NULL);"));
|
"file_state INTEGER NOT NULL);"));
|
||||||
queries +=
|
queries +=
|
||||||
RawDatabase::Query(QStringLiteral("ALTER TABLE history ADD file_id INTEGER;"));
|
RawDatabase::Query(QStringLiteral("ALTER TABLE history ADD file_id INTEGER;"));
|
||||||
|
queries += RawDatabase::Query(QStringLiteral("PRAGMA user_version = 1;"));
|
||||||
|
return db->execNow(queries);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -128,15 +134,14 @@ void dbSchemaUpgrade(std::shared_ptr<RawDatabase> db)
|
||||||
// Note: 0 is a special version that is actually two versions.
|
// Note: 0 is a special version that is actually two versions.
|
||||||
// possibility 1) it is a newly created database and it neesds the current schema to be created.
|
// possibility 1) it is a newly created database and it neesds the current schema to be created.
|
||||||
// possibility 2) it is a old existing database, before version 1 and before we saved schema version,
|
// possibility 2) it is a old existing database, before version 1 and before we saved schema version,
|
||||||
// and need to be updated.
|
// and needs to be updated.
|
||||||
if (isNewDb(db)) {
|
if (isNewDb(db)) {
|
||||||
generateCurrentSchema(queries);
|
createCurrentSchema(db);
|
||||||
queries += RawDatabase::Query(QStringLiteral("PRAGMA user_version = %1;").arg(SCHEMA_VERSION));
|
|
||||||
db->execLater(queries);
|
|
||||||
qDebug() << "Database created at schema version" << SCHEMA_VERSION;
|
qDebug() << "Database created at schema version" << SCHEMA_VERSION;
|
||||||
break; // new db is the only case where we don't incrementally upgrade through each version
|
break; // new db is the only case where we don't incrementally upgrade through each version
|
||||||
} else {
|
} else {
|
||||||
dbSchema0to1(db, queries);
|
dbSchema0to1(db);
|
||||||
|
qDebug() << "Database upgraded incrementally to schema version 1";
|
||||||
}
|
}
|
||||||
// fallthrough
|
// fallthrough
|
||||||
// case 1:
|
// case 1:
|
||||||
|
@ -144,9 +149,7 @@ void dbSchemaUpgrade(std::shared_ptr<RawDatabase> db)
|
||||||
// //fallthrough
|
// //fallthrough
|
||||||
// etc.
|
// etc.
|
||||||
default:
|
default:
|
||||||
queries += RawDatabase::Query(QStringLiteral("PRAGMA user_version = %1;").arg(SCHEMA_VERSION));
|
qInfo() << "Database upgrade finished (databaseSchemaVersion" << databaseSchemaVersion
|
||||||
db->execLater(queries);
|
|
||||||
qDebug() << "Database upgrade finished (databaseSchemaVersion" << databaseSchemaVersion
|
|
||||||
<< "->" << SCHEMA_VERSION << ")";
|
<< "->" << SCHEMA_VERSION << ")";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -114,8 +114,7 @@ void TestDbSchema::testCreation()
|
||||||
{
|
{
|
||||||
QVector<RawDatabase::Query> queries;
|
QVector<RawDatabase::Query> queries;
|
||||||
auto db = std::shared_ptr<RawDatabase>{new RawDatabase{"testCreation.db", {}, {}}};
|
auto db = std::shared_ptr<RawDatabase>{new RawDatabase{"testCreation.db", {}, {}}};
|
||||||
generateCurrentSchema(queries);
|
QVERIFY(createCurrentSchema(db));
|
||||||
QVERIFY(db->execNow(queries));
|
|
||||||
verifyDb(db, schema1);
|
verifyDb(db, schema1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -124,9 +123,7 @@ void TestDbSchema::testIsNewDb()
|
||||||
auto db = std::shared_ptr<RawDatabase>{new RawDatabase{"testIsNewDbTrue.db", {}, {}}};
|
auto db = std::shared_ptr<RawDatabase>{new RawDatabase{"testIsNewDbTrue.db", {}, {}}};
|
||||||
QVERIFY(isNewDb(db) == true);
|
QVERIFY(isNewDb(db) == true);
|
||||||
db = std::shared_ptr<RawDatabase>{new RawDatabase{"testIsNewDbFalse.db", {}, {}}};
|
db = std::shared_ptr<RawDatabase>{new RawDatabase{"testIsNewDbFalse.db", {}, {}}};
|
||||||
QVector<RawDatabase::Query> queries;
|
|
||||||
createSchemaAtVersion(db, schema0);
|
createSchemaAtVersion(db, schema0);
|
||||||
QVERIFY(db->execNow(queries));
|
|
||||||
QVERIFY(isNewDb(db) == false);
|
QVERIFY(isNewDb(db) == false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -134,9 +131,7 @@ void TestDbSchema::test0to1()
|
||||||
{
|
{
|
||||||
auto db = std::shared_ptr<RawDatabase>{new RawDatabase{"test0to1.db", {}, {}}};
|
auto db = std::shared_ptr<RawDatabase>{new RawDatabase{"test0to1.db", {}, {}}};
|
||||||
createSchemaAtVersion(db, schema0);
|
createSchemaAtVersion(db, schema0);
|
||||||
QVector<RawDatabase::Query> queries;
|
QVERIFY(dbSchema0to1(db));
|
||||||
dbSchema0to1(db, queries);
|
|
||||||
QVERIFY(db->execNow(queries));
|
|
||||||
verifyDb(db, schema1);
|
verifyDb(db, schema1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user