mirror of
https://github.com/tnodir/fort
synced 2024-11-15 08:56:22 +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);
|
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)
|
||||||
{
|
{
|
||||||
|
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,
|
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();
|
||||||
|
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user