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); bool isNewDb = (userVersion == 0);
if (isNewDb) {
recreate = false;
}
// Re-create the DB // Re-create the DB
QString tempFilePath; if (recreate) {
if (recreate && !isNewDb) { if (!clearWithBackup(sqlPragmas))
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);
return false; return false;
}
execute(sqlPragmas);
setEncoding(oldEncoding);
userVersion = 0; userVersion = 0;
isNewDb = true; isNewDb = true;
@ -337,16 +328,8 @@ bool SqliteDb::migrate(const QString &sqlDir, const char *sqlPragmas, int versio
migrateSqlScripts(sqlDir, version, userVersion, isNewDb, migrateFunc, migrateContext); migrateSqlScripts(sqlDir, version, userVersion, isNewDb, migrateFunc, migrateContext);
// Re-create the DB: End // Re-create the DB: End
if (recreate && !tempFilePath.isEmpty()) { if (success && recreate) {
// Re-import the DB success = importBackup(importOldData, migrateFunc, migrateContext);
if (success && importOldData) {
success = importDb(tempFilePath, migrateFunc, migrateContext);
}
// Remove the old DB
if (success) {
removeDbFile(tempFilePath);
}
} }
return success; return success;
@ -405,6 +388,51 @@ bool SqliteDb::migrateSqlScripts(const QString &sqlDir, int version, int userVer
return success; 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( bool SqliteDb::importDb(
const QString &sourceFilePath, SQLITEDB_MIGRATE_FUNC migrateFunc, void *migrateContext) const QString &sourceFilePath, SQLITEDB_MIGRATE_FUNC migrateFunc, void *migrateContext)
{ {

View File

@ -94,14 +94,19 @@ public:
bool importOldData = false, SQLITEDB_MIGRATE_FUNC migrateFunc = nullptr, bool importOldData = false, SQLITEDB_MIGRATE_FUNC migrateFunc = nullptr,
void *migrateContext = nullptr); void *migrateContext = nullptr);
bool importDb(
const QString &sourceFilePath, SQLITEDB_MIGRATE_FUNC migrateFunc, void *migrateContext);
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 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(); void clearStmts();

View File

@ -78,7 +78,8 @@ void AppInfoManager::setUp()
return; 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(); logCritical() << "Migration error" << sqliteDb()->filePath();
return; return;
} }

View File

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

View File

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