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;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SqliteDb::migrate(const QString &sqlDir, const char *sqlPragmas, int version, bool recreate,
|
bool SqliteDb::migrate(MigrateOptions &opt)
|
||||||
bool importOldData, SQLITEDB_MIGRATE_FUNC migrateFunc, void *migrateContext)
|
|
||||||
{
|
{
|
||||||
if (!sqlPragmas) {
|
if (!opt.sqlPragmas) {
|
||||||
sqlPragmas = defaultSqlPragmas;
|
opt.sqlPragmas = defaultSqlPragmas;
|
||||||
}
|
}
|
||||||
execute(sqlPragmas);
|
execute(opt.sqlPragmas);
|
||||||
|
|
||||||
// Check version
|
// Check version
|
||||||
int userVersion = this->userVersion();
|
int userVersion = this->userVersion();
|
||||||
if (userVersion == version)
|
if (userVersion == opt.version)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
if (userVersion > version) {
|
if (userVersion > opt.version) {
|
||||||
dbWarning() << "Cannot open new DB" << userVersion << "from old application" << version;
|
dbWarning() << "Cannot open new DB" << userVersion << "from old application" << opt.version;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isNewDb = (userVersion == 0);
|
bool isNewDb = (userVersion == 0);
|
||||||
if (isNewDb) {
|
if (isNewDb) {
|
||||||
recreate = false;
|
opt.recreate = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Re-create the DB
|
// Re-create the DB
|
||||||
if (recreate) {
|
if (opt.recreate) {
|
||||||
if (!clearWithBackup(sqlPragmas))
|
if (!clearWithBackup(opt.sqlPragmas))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
userVersion = 0;
|
userVersion = 0;
|
||||||
@ -324,26 +323,24 @@ bool SqliteDb::migrate(const QString &sqlDir, const char *sqlPragmas, int versio
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Run migration SQL scripts
|
// Run migration SQL scripts
|
||||||
bool success =
|
bool success = migrateSqlScripts(opt, userVersion, isNewDb);
|
||||||
migrateSqlScripts(sqlDir, version, userVersion, isNewDb, migrateFunc, migrateContext);
|
|
||||||
|
|
||||||
// Re-create the DB: End
|
// Re-create the DB: End
|
||||||
if (success && recreate) {
|
if (success && opt.recreate) {
|
||||||
success = importBackup(importOldData, migrateFunc, migrateContext);
|
success = importBackup(opt.importOldData, opt.migrateFunc, opt.migrateContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SqliteDb::migrateSqlScripts(const QString &sqlDir, int version, int userVersion, bool isNewDb,
|
bool SqliteDb::migrateSqlScripts(const MigrateOptions &opt, int userVersion, bool isNewDb)
|
||||||
SQLITEDB_MIGRATE_FUNC migrateFunc, void *migrateContext)
|
|
||||||
{
|
{
|
||||||
QDir dir(sqlDir);
|
const QDir dir(opt.sqlDir);
|
||||||
bool success = true;
|
bool success = true;
|
||||||
|
|
||||||
beginTransaction();
|
beginTransaction();
|
||||||
|
|
||||||
while (userVersion < version) {
|
while (userVersion < opt.version) {
|
||||||
++userVersion;
|
++userVersion;
|
||||||
|
|
||||||
const QString filePath = dir.filePath(QString("%1.sql").arg(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());
|
success = execute(data.constData());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (success && migrateFunc) {
|
if (success && opt.migrateFunc) {
|
||||||
success = migrateFunc(this, userVersion, isNewDb, migrateContext);
|
success = opt.migrateFunc(this, userVersion, isNewDb, opt.migrateContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (success) {
|
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)
|
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(
|
explicit SqliteDb(
|
||||||
const QString &filePath = QString(), quint32 openFlags = OpenDefaultReadWrite);
|
const QString &filePath = QString(), quint32 openFlags = OpenDefaultReadWrite);
|
||||||
~SqliteDb();
|
~SqliteDb();
|
||||||
@ -90,15 +101,12 @@ public:
|
|||||||
QStringList tableNames(const QString &schemaName = QString());
|
QStringList tableNames(const QString &schemaName = QString());
|
||||||
QStringList columnNames(const QString &tableName, 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 migrate(SqliteDb::MigrateOptions &opt);
|
||||||
bool importOldData = false, SQLITEDB_MIGRATE_FUNC migrateFunc = nullptr,
|
|
||||||
void *migrateContext = nullptr);
|
|
||||||
|
|
||||||
SqliteStmt *stmt(const char *sql);
|
SqliteStmt *stmt(const char *sql);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool migrateSqlScripts(const QString &sqlDir, int version, int userVersion, bool isNewDb,
|
bool migrateSqlScripts(const MigrateOptions &opt, int userVersion, bool isNewDb);
|
||||||
SQLITEDB_MIGRATE_FUNC migrateFunc, void *migrateContext);
|
|
||||||
|
|
||||||
bool clearWithBackup(const char *sqlPragmas);
|
bool clearWithBackup(const char *sqlPragmas);
|
||||||
bool importBackup(bool importOldData, SQLITEDB_MIGRATE_FUNC migrateFunc, void *migrateContext);
|
bool importBackup(bool importOldData, SQLITEDB_MIGRATE_FUNC migrateFunc, void *migrateContext);
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
|
|
||||||
QT += gui network widgets
|
QT += gui network widgets
|
||||||
|
|
||||||
CONFIG += c++17
|
CONFIG += c++2a
|
||||||
|
|
||||||
# Driver integration
|
# Driver integration
|
||||||
include($$PWD/../driver/Driver-include.pri)
|
include($$PWD/../driver/Driver-include.pri)
|
||||||
|
@ -78,8 +78,12 @@ void AppInfoManager::setUp()
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!sqliteDb()->migrate(
|
SqliteDb::MigrateOptions opt = { .sqlDir = ":/appinfo/migrations",
|
||||||
":/appinfo/migrations", nullptr, DATABASE_USER_VERSION, /*recreate=*/true)) {
|
.version = DATABASE_USER_VERSION,
|
||||||
|
.recreate = true,
|
||||||
|
.importOldData = false };
|
||||||
|
|
||||||
|
if (!sqliteDb()->migrate(opt)) {
|
||||||
logCritical() << "Migration error" << sqliteDb()->filePath();
|
logCritical() << "Migration error" << sqliteDb()->filePath();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -424,8 +424,12 @@ void ConfManager::setUp()
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!sqliteDb()->migrate(":/conf/migrations", nullptr, DATABASE_USER_VERSION, /*recreate=*/true,
|
SqliteDb::MigrateOptions opt = { .sqlDir = ":/conf/migrations",
|
||||||
/*importOldData=*/true, &migrateFunc)) {
|
.version = DATABASE_USER_VERSION,
|
||||||
|
.recreate = true,
|
||||||
|
.migrateFunc = &migrateFunc };
|
||||||
|
|
||||||
|
if (!sqliteDb()->migrate(opt)) {
|
||||||
logCritical() << "Migration error" << sqliteDb()->filePath();
|
logCritical() << "Migration error" << sqliteDb()->filePath();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -91,8 +91,12 @@ void StatManager::setUp()
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!sqliteDb()->migrate(":/stat/migrations", nullptr, DATABASE_USER_VERSION, /*recreate=*/true,
|
SqliteDb::MigrateOptions opt = { .sqlDir = ":/stat/migrations",
|
||||||
/*importOldData=*/true, &migrateFunc)) {
|
.version = DATABASE_USER_VERSION,
|
||||||
|
.recreate = true,
|
||||||
|
.migrateFunc = &migrateFunc };
|
||||||
|
|
||||||
|
if (!sqliteDb()->migrate(opt)) {
|
||||||
logCritical() << "Migration error" << sqliteDb()->filePath();
|
logCritical() << "Migration error" << sqliteDb()->filePath();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user