SqliteDb::migrate: Add MigrateOptions struct

This commit is contained in:
Nodir Temirkhodjaev 2021-12-11 14:50:43 +03:00
parent 69dd194242
commit 12fc643b49
6 changed files with 50 additions and 33 deletions

View File

@ -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) {

View File

@ -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);

View File

@ -1,7 +1,7 @@
QT += gui network widgets
CONFIG += c++17
CONFIG += c++2a
# Driver integration
include($$PWD/../driver/Driver-include.pri)

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}