UI: SqliteDb: Simplify migrate()

This commit is contained in:
Nodir Temirkhodjaev 2023-05-10 13:45:58 +03:00
parent 96cd249c6f
commit 424e68ae14
2 changed files with 21 additions and 4 deletions

View File

@ -309,6 +309,21 @@ bool SqliteDb::migrate(MigrateOptions &opt)
if (userVersion == opt.version) if (userVersion == opt.version)
return true; return true;
// Check migration options
if (!canMigrate(opt, userVersion))
return false;
// Migrate the DB
bool isNewDb = (userVersion == 0);
if (isNewDb) {
opt.recreate = false;
}
return migrateDb(opt, userVersion, isNewDb);
}
bool SqliteDb::canMigrate(const MigrateOptions &opt, int userVersion) const
{
if (userVersion > opt.version) { if (userVersion > opt.version) {
qCWarning(LC) << "Cannot open new DB" << userVersion << "from old application" qCWarning(LC) << "Cannot open new DB" << userVersion << "from old application"
<< opt.version; << opt.version;
@ -320,11 +335,11 @@ bool SqliteDb::migrate(MigrateOptions &opt)
return false; return false;
} }
bool isNewDb = (userVersion == 0); return true;
if (isNewDb) { }
opt.recreate = false;
}
bool SqliteDb::migrateDb(const MigrateOptions &opt, int userVersion, bool isNewDb)
{
// Re-create the DB // Re-create the DB
if (opt.recreate) { if (opt.recreate) {
if (!clearWithBackup(opt.sqlPragmas)) if (!clearWithBackup(opt.sqlPragmas))

View File

@ -110,6 +110,8 @@ public:
SqliteStmt *stmt(const char *sql); SqliteStmt *stmt(const char *sql);
private: private:
bool canMigrate(const MigrateOptions &opt, int userVersion) const;
bool migrateDb(const MigrateOptions &opt, int userVersion, bool isNewDb);
bool migrateSqlScripts(const MigrateOptions &opt, int userVersion, bool isNewDb); bool migrateSqlScripts(const MigrateOptions &opt, int userVersion, bool isNewDb);
bool clearWithBackup(const char *sqlPragmas); bool clearWithBackup(const char *sqlPragmas);