From 65775fec4520496eb6599e2cbb802a0194f38bf1 Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Fri, 29 Mar 2024 16:58:08 +0300 Subject: [PATCH] UI: SqliteDb: Simplify migrateSqlScripts() --- src/ui/3rdparty/sqlite/sqlitedb.cpp | 60 +++++++++++++++-------------- src/ui/3rdparty/sqlite/sqlitedb.h | 3 ++ 2 files changed, 35 insertions(+), 28 deletions(-) diff --git a/src/ui/3rdparty/sqlite/sqlitedb.cpp b/src/ui/3rdparty/sqlite/sqlitedb.cpp index 878870bc..8295ef33 100644 --- a/src/ui/3rdparty/sqlite/sqlitedb.cpp +++ b/src/ui/3rdparty/sqlite/sqlitedb.cpp @@ -347,54 +347,58 @@ bool SqliteDb::migrateDb(const MigrateOptions &opt, int userVersion, bool isNewD bool SqliteDb::migrateSqlScripts(const MigrateOptions &opt, int userVersion, bool isNewDb) { - const QDir dir(opt.sqlDir); - bool success = true; + const QDir sqlDir(opt.sqlDir); + bool ok = true; beginWriteTransaction(); while (userVersion < opt.version) { ++userVersion; - const QString filePath = dir.filePath(QString("%1.sql").arg(userVersion)); - beginSavepoint(); - QFile file(filePath); - if (file.exists()) { - if (!file.open(QFile::ReadOnly | QFile::Text)) { - qCWarning(LC) << "Cannot open migration file" << filePath << file.errorString(); - success = false; - break; - } + ok = migrateSqlScript(sqlDir, userVersion); - const QByteArray data = file.readAll(); - if (data.isEmpty()) { - qCWarning(LC) << "Migration file is empty" << filePath; - success = false; - break; - } - - success = execute(data.constData()); + if (ok && opt.migrateFunc) { + ok = opt.migrateFunc(this, userVersion, isNewDb, opt.migrateContext); } - if (success && opt.migrateFunc) { - success = opt.migrateFunc(this, userVersion, isNewDb, opt.migrateContext); - } - - if (success) { - releaseSavepoint(); - } else { - qCCritical(LC) << "Migration error:" << filePath << errorMessage(); + if (!ok) { + qCCritical(LC) << "Migration error to version:" << userVersion << errorMessage(); rollbackSavepoint(); break; } + + releaseSavepoint(); } this->setUserVersion(userVersion); commitTransaction(); - return success; + return ok; +} + +bool SqliteDb::migrateSqlScript(const QDir &sqlDir, int userVersion) +{ + const QString filePath = sqlDir.filePath(QString("%1.sql").arg(userVersion)); + + QFile file(filePath); + if (!file.exists()) + return true; + + if (!file.open(QFile::ReadOnly | QFile::Text)) { + qCWarning(LC) << "Cannot open migration file" << filePath << file.errorString(); + return false; + } + + const QByteArray data = file.readAll(); + if (data.isEmpty()) { + qCWarning(LC) << "Migration file is empty" << filePath; + return false; + } + + return execute(data.constData()); } bool SqliteDb::migrateDbBegin(const MigrateOptions &opt, int &userVersion, bool &isNewDb) diff --git a/src/ui/3rdparty/sqlite/sqlitedb.h b/src/ui/3rdparty/sqlite/sqlitedb.h index 8fddfa27..afbafd46 100644 --- a/src/ui/3rdparty/sqlite/sqlitedb.h +++ b/src/ui/3rdparty/sqlite/sqlitedb.h @@ -8,6 +8,8 @@ #include +QT_FORWARD_DECLARE_CLASS(QDir) + struct sqlite3; class SqliteDb; @@ -126,6 +128,7 @@ private: bool canMigrate(const MigrateOptions &opt) const; bool migrateDb(const MigrateOptions &opt, int userVersion, bool isNewDb); bool migrateSqlScripts(const MigrateOptions &opt, int userVersion, bool isNewDb); + bool migrateSqlScript(const QDir &sqlDir, int userVersion); bool migrateDbBegin(const MigrateOptions &opt, int &userVersion, bool &isNewDb); bool migrateDbEnd(const MigrateOptions &opt);