SqliteDb: Simplify migrate(): Refactor re-creating

This commit is contained in:
Nodir Temirkhodjaev 2021-12-09 12:35:58 +03:00
parent e47f3b6cda
commit 37c12a4133
5 changed files with 67 additions and 33 deletions

View File

@ -310,23 +310,14 @@ bool SqliteDb::migrate(const QString &sqlDir, const char *sqlPragmas, int versio
}
bool isNewDb = (userVersion == 0);
if (isNewDb) {
recreate = false;
}
// Re-create the DB
QString tempFilePath;
if (recreate && !isNewDb) {
const QString oldEncoding = this->encoding();
close();
tempFilePath = m_filePath + ".temp";
if (!(renameDbFile(m_filePath, tempFilePath) && open())) {
dbWarning() << "Cannot re-create the DB" << m_filePath;
renameDbFile(tempFilePath, m_filePath);
if (recreate) {
if (!clearWithBackup(sqlPragmas))
return false;
}
execute(sqlPragmas);
setEncoding(oldEncoding);
userVersion = 0;
isNewDb = true;
@ -337,16 +328,8 @@ bool SqliteDb::migrate(const QString &sqlDir, const char *sqlPragmas, int versio
migrateSqlScripts(sqlDir, version, userVersion, isNewDb, migrateFunc, migrateContext);
// Re-create the DB: End
if (recreate && !tempFilePath.isEmpty()) {
// Re-import the DB
if (success && importOldData) {
success = importDb(tempFilePath, migrateFunc, migrateContext);
}
// Remove the old DB
if (success) {
removeDbFile(tempFilePath);
}
if (success && recreate) {
success = importBackup(importOldData, migrateFunc, migrateContext);
}
return success;
@ -405,6 +388,51 @@ bool SqliteDb::migrateSqlScripts(const QString &sqlDir, int version, int userVer
return success;
}
bool SqliteDb::clearWithBackup(const char *sqlPragmas)
{
const QString oldEncoding = this->encoding();
close();
const QString tempFilePath = backupFilePath();
if (!(renameDbFile(m_filePath, tempFilePath) && open())) {
dbWarning() << "Cannot re-create the DB" << m_filePath;
renameDbFile(tempFilePath, m_filePath);
return false;
}
execute(sqlPragmas);
setEncoding(oldEncoding);
return true;
}
bool SqliteDb::importBackup(
bool importOldData, SQLITEDB_MIGRATE_FUNC migrateFunc, void *migrateContext)
{
bool success = true;
const QString tempFilePath = backupFilePath();
// Re-import the DB
if (importOldData) {
success = importDb(tempFilePath, migrateFunc, migrateContext);
}
// Remove the old DB
if (success) {
removeDbFile(tempFilePath);
}
return success;
}
QString SqliteDb::backupFilePath() const
{
return m_filePath + ".temp";
}
bool SqliteDb::importDb(
const QString &sourceFilePath, SQLITEDB_MIGRATE_FUNC migrateFunc, void *migrateContext)
{

View File

@ -94,14 +94,19 @@ public:
bool importOldData = false, SQLITEDB_MIGRATE_FUNC migrateFunc = nullptr,
void *migrateContext = nullptr);
bool importDb(
const QString &sourceFilePath, SQLITEDB_MIGRATE_FUNC migrateFunc, void *migrateContext);
SqliteStmt *stmt(const char *sql);
private:
bool migrateSqlScripts(const QString &sqlDir, int version, int userVersion, bool isNewDb,
SQLITEDB_MIGRATE_FUNC migrateFunc = nullptr, void *migrateContext = nullptr);
SQLITEDB_MIGRATE_FUNC migrateFunc, void *migrateContext);
bool clearWithBackup(const char *sqlPragmas);
bool importBackup(bool importOldData, SQLITEDB_MIGRATE_FUNC migrateFunc, void *migrateContext);
QString backupFilePath() const;
bool importDb(
const QString &sourceFilePath, SQLITEDB_MIGRATE_FUNC migrateFunc, void *migrateContext);
void clearStmts();

View File

@ -78,7 +78,8 @@ void AppInfoManager::setUp()
return;
}
if (!sqliteDb()->migrate(":/appinfo/migrations", nullptr, DATABASE_USER_VERSION, true)) {
if (!sqliteDb()->migrate(
":/appinfo/migrations", nullptr, DATABASE_USER_VERSION, /*recreate=*/true)) {
logCritical() << "Migration error" << sqliteDb()->filePath();
return;
}

View File

@ -424,8 +424,8 @@ void ConfManager::setUp()
return;
}
if (!sqliteDb()->migrate(
":/conf/migrations", nullptr, DATABASE_USER_VERSION, true, true, &migrateFunc)) {
if (!sqliteDb()->migrate(":/conf/migrations", nullptr, DATABASE_USER_VERSION, /*recreate=*/true,
/*importOldData=*/true, &migrateFunc)) {
logCritical() << "Migration error" << sqliteDb()->filePath();
return;
}

View File

@ -91,8 +91,8 @@ void StatManager::setUp()
return;
}
if (!sqliteDb()->migrate(
":/stat/migrations", nullptr, DATABASE_USER_VERSION, true, true, &migrateFunc)) {
if (!sqliteDb()->migrate(":/stat/migrations", nullptr, DATABASE_USER_VERSION, /*recreate=*/true,
/*importOldData=*/true, &migrateFunc)) {
logCritical() << "Migration error" << sqliteDb()->filePath();
return;
}