mirror of
https://github.com/tnodir/fort
synced 2024-11-15 09:45:44 +00:00
SqliteDb::migrate: Add MigrateOptions struct
This commit is contained in:
parent
69dd194242
commit
12fc643b49
39
src/ui/3rdparty/sqlite/sqlitedb.cpp
vendored
39
src/ui/3rdparty/sqlite/sqlitedb.cpp
vendored
@ -291,32 +291,31 @@ QStringList SqliteDb::columnNames(const QString &tableName, const QString &schem
|
||||
return list;
|
||||
}
|
||||
|
||||
bool SqliteDb::migrate(const QString &sqlDir, const char *sqlPragmas, int version, bool recreate,
|
||||
bool importOldData, SQLITEDB_MIGRATE_FUNC migrateFunc, void *migrateContext)
|
||||
bool SqliteDb::migrate(MigrateOptions &opt)
|
||||
{
|
||||
if (!sqlPragmas) {
|
||||
sqlPragmas = defaultSqlPragmas;
|
||||
if (!opt.sqlPragmas) {
|
||||
opt.sqlPragmas = defaultSqlPragmas;
|
||||
}
|
||||
execute(sqlPragmas);
|
||||
execute(opt.sqlPragmas);
|
||||
|
||||
// Check version
|
||||
int userVersion = this->userVersion();
|
||||
if (userVersion == version)
|
||||
if (userVersion == opt.version)
|
||||
return true;
|
||||
|
||||
if (userVersion > version) {
|
||||
dbWarning() << "Cannot open new DB" << userVersion << "from old application" << version;
|
||||
if (userVersion > opt.version) {
|
||||
dbWarning() << "Cannot open new DB" << userVersion << "from old application" << opt.version;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool isNewDb = (userVersion == 0);
|
||||
if (isNewDb) {
|
||||
recreate = false;
|
||||
opt.recreate = false;
|
||||
}
|
||||
|
||||
// Re-create the DB
|
||||
if (recreate) {
|
||||
if (!clearWithBackup(sqlPragmas))
|
||||
if (opt.recreate) {
|
||||
if (!clearWithBackup(opt.sqlPragmas))
|
||||
return false;
|
||||
|
||||
userVersion = 0;
|
||||
@ -324,26 +323,24 @@ bool SqliteDb::migrate(const QString &sqlDir, const char *sqlPragmas, int versio
|
||||
}
|
||||
|
||||
// Run migration SQL scripts
|
||||
bool success =
|
||||
migrateSqlScripts(sqlDir, version, userVersion, isNewDb, migrateFunc, migrateContext);
|
||||
bool success = migrateSqlScripts(opt, userVersion, isNewDb);
|
||||
|
||||
// Re-create the DB: End
|
||||
if (success && recreate) {
|
||||
success = importBackup(importOldData, migrateFunc, migrateContext);
|
||||
if (success && opt.recreate) {
|
||||
success = importBackup(opt.importOldData, opt.migrateFunc, opt.migrateContext);
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
bool SqliteDb::migrateSqlScripts(const QString &sqlDir, int version, int userVersion, bool isNewDb,
|
||||
SQLITEDB_MIGRATE_FUNC migrateFunc, void *migrateContext)
|
||||
bool SqliteDb::migrateSqlScripts(const MigrateOptions &opt, int userVersion, bool isNewDb)
|
||||
{
|
||||
QDir dir(sqlDir);
|
||||
const QDir dir(opt.sqlDir);
|
||||
bool success = true;
|
||||
|
||||
beginTransaction();
|
||||
|
||||
while (userVersion < version) {
|
||||
while (userVersion < opt.version) {
|
||||
++userVersion;
|
||||
|
||||
const QString filePath = dir.filePath(QString("%1.sql").arg(userVersion));
|
||||
@ -368,8 +365,8 @@ bool SqliteDb::migrateSqlScripts(const QString &sqlDir, int version, int userVer
|
||||
success = execute(data.constData());
|
||||
}
|
||||
|
||||
if (success && migrateFunc) {
|
||||
success = migrateFunc(this, userVersion, isNewDb, migrateContext);
|
||||
if (success && opt.migrateFunc) {
|
||||
success = opt.migrateFunc(this, userVersion, isNewDb, opt.migrateContext);
|
||||
}
|
||||
|
||||
if (success) {
|
||||
|
18
src/ui/3rdparty/sqlite/sqlitedb.h
vendored
18
src/ui/3rdparty/sqlite/sqlitedb.h
vendored
@ -32,6 +32,17 @@ public:
|
||||
OpenDefaultReadWrite = (OpenReadWrite | OpenCreate | OpenNoMutex)
|
||||
};
|
||||
|
||||
struct MigrateOptions
|
||||
{
|
||||
const QString sqlDir;
|
||||
const char *sqlPragmas = nullptr;
|
||||
int version = 0;
|
||||
bool recreate = true;
|
||||
bool importOldData = true;
|
||||
SQLITEDB_MIGRATE_FUNC migrateFunc = nullptr;
|
||||
void *migrateContext = nullptr;
|
||||
};
|
||||
|
||||
explicit SqliteDb(
|
||||
const QString &filePath = QString(), quint32 openFlags = OpenDefaultReadWrite);
|
||||
~SqliteDb();
|
||||
@ -90,15 +101,12 @@ public:
|
||||
QStringList tableNames(const QString &schemaName = QString());
|
||||
QStringList columnNames(const QString &tableName, const QString &schemaName = QString());
|
||||
|
||||
bool migrate(const QString &sqlDir, const char *sqlPragmas, int version, bool recreate = false,
|
||||
bool importOldData = false, SQLITEDB_MIGRATE_FUNC migrateFunc = nullptr,
|
||||
void *migrateContext = nullptr);
|
||||
bool migrate(SqliteDb::MigrateOptions &opt);
|
||||
|
||||
SqliteStmt *stmt(const char *sql);
|
||||
|
||||
private:
|
||||
bool migrateSqlScripts(const QString &sqlDir, int version, int userVersion, bool isNewDb,
|
||||
SQLITEDB_MIGRATE_FUNC migrateFunc, void *migrateContext);
|
||||
bool migrateSqlScripts(const MigrateOptions &opt, int userVersion, bool isNewDb);
|
||||
|
||||
bool clearWithBackup(const char *sqlPragmas);
|
||||
bool importBackup(bool importOldData, SQLITEDB_MIGRATE_FUNC migrateFunc, void *migrateContext);
|
||||
|
@ -1,7 +1,7 @@
|
||||
|
||||
QT += gui network widgets
|
||||
|
||||
CONFIG += c++17
|
||||
CONFIG += c++2a
|
||||
|
||||
# Driver integration
|
||||
include($$PWD/../driver/Driver-include.pri)
|
||||
|
@ -78,8 +78,12 @@ void AppInfoManager::setUp()
|
||||
return;
|
||||
}
|
||||
|
||||
if (!sqliteDb()->migrate(
|
||||
":/appinfo/migrations", nullptr, DATABASE_USER_VERSION, /*recreate=*/true)) {
|
||||
SqliteDb::MigrateOptions opt = { .sqlDir = ":/appinfo/migrations",
|
||||
.version = DATABASE_USER_VERSION,
|
||||
.recreate = true,
|
||||
.importOldData = false };
|
||||
|
||||
if (!sqliteDb()->migrate(opt)) {
|
||||
logCritical() << "Migration error" << sqliteDb()->filePath();
|
||||
return;
|
||||
}
|
||||
|
@ -424,8 +424,12 @@ void ConfManager::setUp()
|
||||
return;
|
||||
}
|
||||
|
||||
if (!sqliteDb()->migrate(":/conf/migrations", nullptr, DATABASE_USER_VERSION, /*recreate=*/true,
|
||||
/*importOldData=*/true, &migrateFunc)) {
|
||||
SqliteDb::MigrateOptions opt = { .sqlDir = ":/conf/migrations",
|
||||
.version = DATABASE_USER_VERSION,
|
||||
.recreate = true,
|
||||
.migrateFunc = &migrateFunc };
|
||||
|
||||
if (!sqliteDb()->migrate(opt)) {
|
||||
logCritical() << "Migration error" << sqliteDb()->filePath();
|
||||
return;
|
||||
}
|
||||
|
@ -91,8 +91,12 @@ void StatManager::setUp()
|
||||
return;
|
||||
}
|
||||
|
||||
if (!sqliteDb()->migrate(":/stat/migrations", nullptr, DATABASE_USER_VERSION, /*recreate=*/true,
|
||||
/*importOldData=*/true, &migrateFunc)) {
|
||||
SqliteDb::MigrateOptions opt = { .sqlDir = ":/stat/migrations",
|
||||
.version = DATABASE_USER_VERSION,
|
||||
.recreate = true,
|
||||
.migrateFunc = &migrateFunc };
|
||||
|
||||
if (!sqliteDb()->migrate(opt)) {
|
||||
logCritical() << "Migration error" << sqliteDb()->filePath();
|
||||
return;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user