mirror of
https://github.com/tnodir/fort
synced 2024-11-15 06:15:15 +00:00
SqliteDb: Simplify migrate(): Refactor re-creating
This commit is contained in:
parent
e47f3b6cda
commit
37c12a4133
76
src/ui/3rdparty/sqlite/sqlitedb.cpp
vendored
76
src/ui/3rdparty/sqlite/sqlitedb.cpp
vendored
@ -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)
|
||||
{
|
||||
|
13
src/ui/3rdparty/sqlite/sqlitedb.h
vendored
13
src/ui/3rdparty/sqlite/sqlitedb.h
vendored
@ -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();
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user