Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.
C++
Fix initialization of the foreign_keys constraint for DB
Dmitry Kazakov8d54205963fb
diff --git a/libs/resources/KisResourceCacheDb.cpp b/libs/resources/KisResourceCacheDb.cppindex 08bedea989..c59c895597 100644--- a/libs/resources/KisResourceCacheDb.cpp+++ b/libs/resources/KisResourceCacheDb.cpp@@ -254,9 +254,9 @@ QSqlError createDatabase(const QString &location)if (newSchemaVersionNumber == QVersionNumber::fromString("0.0.18")&& QVersionNumber::compare(oldSchemaVersionNumber, QVersionNumber::fromString("0.0.14")) >= 0&& QVersionNumber::compare(oldSchemaVersionNumber, QVersionNumber::fromString("0.0.18")) < 0) {-+bool from14to15 = oldSchemaVersionNumber == QVersionNumber::fromString("0.0.14");-+bool from15to16 = oldSchemaVersionNumber == QVersionNumber::fromString("0.0.14")|| oldSchemaVersionNumber == QVersionNumber::fromString("0.0.15");@@ -367,6 +367,11 @@ QSqlError createDatabase(const QString &location).arg(schemaVersion).arg(kritaVersion).arg(QDateTime::fromSecsSinceEpoch(creationDate).toString()));++ /// initialization is completed, transaction is over,+ /// now enable the foreign_keys constraint if necessary+ KisResourceCacheDb::synchronizeForeignKeysState();+return QSqlError();}}@@ -465,6 +470,10 @@ QSqlError createDatabase(const QString &location)transactionLock.commit();+ /// initialization is completed, transaction is over,+ /// now enable the foreign_keys constraint if necessary+ KisResourceCacheDb::synchronizeForeignKeysState();+return QSqlError();}@@ -2105,7 +2114,6 @@ bool KisResourceCacheDb::synchronizeStorage(KisResourceStorageSP storage)}}-QSqlDatabase::database().commit();debugResource << "Synchronizing the storages took" << t.elapsed() << "milliseconds for" << storage->location();@@ -2235,6 +2243,64 @@ void KisResourceCacheDb::performHouseKeepingOnExit()}}+void KisResourceCacheDb::setForeignKeysStateImpl(bool isEnabled)+{+ KisSqlQueryLoader loader("inline://set_foreign_keys_state",+ QString("PRAGMA foreign_keys = %1").arg(isEnabled ? "ON" : "OFF"));+ loader.exec();+}++bool KisResourceCacheDb::getForeignKeysStateImpl()+{+ KisSqlQueryLoader loader("inline://get_foreign_keys_state",+ "PRAGMA foreign_keys");++ loader.exec();++ if (loader.query().first()) {+ return loader.query().value(0).toInt();+ }++ return false;+}++void KisResourceCacheDb::synchronizeForeignKeysState()+{+#ifdef KRITA_STABLE+ bool useForeignKeys = false;+ KisUsageLogger::log("INFO: detected stable build of Krita, foreign_keys constraint will be disabled");+#else+ bool useForeignKeys = true;+ KisUsageLogger::log("INFO: detected unstable build of Krita, foreign_keys constraint will be enabled");+#endif++ if (qEnvironmentVariableIsSet("KRITA_OVERRIDE_USE_FOREIGN_KEYS")) {+ useForeignKeys = qEnvironmentVariableIntValue("KRITA_OVERRIDE_USE_FOREIGN_KEYS") > 0;+ KisUsageLogger::log("INFO: foreign_keys constraint was overridden by KRITA_OVERRIDE_USE_FOREIGN_KEYS: " + QString::number(useForeignKeys));+ }++ try {+ const bool oldForeignKeysState = KisResourceCacheDb::getForeignKeysStateImpl();++ if (oldForeignKeysState != useForeignKeys) {+ KisUsageLogger::log(+ "INFO: switch foreign_keys state: " ++ QString::number(oldForeignKeysState) ++ " -> " ++ QString::number(useForeignKeys));++ KisResourceCacheDb::setForeignKeysStateImpl(useForeignKeys);+ }++ } catch (const KisSqlQueryLoader::SQLException &e) {+ qWarning().noquote() << "ERROR: failed to execute query:" << e.message;+ qWarning().noquote() << " file:" << e.filePath;+ qWarning().noquote() << " statement:" << e.statementIndex;+ qWarning().noquote() << " error:" << e.sqlError.text();+ }++}+bool KisResourceCacheDb::registerResourceType(const QString &resourceType){// Check whether the type already existsdiff --git a/libs/resources/KisResourceCacheDb.h b/libs/resources/KisResourceCacheDb.hindex 7631c7ebf7..3b20a07d40 100644--- a/libs/resources/KisResourceCacheDb.h+++ b/libs/resources/KisResourceCacheDb.h@@ -55,6 +55,22 @@ public:/// perform optimize and vacuum when necessarystatic void performHouseKeepingOnExit();+ /// set the foreign_keys feature state of the database+ /// (the function **may** throw SQL exceptions,+ /// call in a try-block only!)+ static void setForeignKeysStateImpl(bool isEnabled);++ /// get the foreign_keys feature state of the database+ /// (the function **may** throw SQL exceptions,+ /// call in a try-block only!)+ static bool getForeignKeysStateImpl();++ /// Called in the end of the database creation step to enable+ /// or disable the foreign_keys state depending on the release+ /// status of Krita. Currently, only developer's builds of Krita+ /// have foreign_keys constraint enabled.+ static void synchronizeForeignKeysState();+private:friend class KisResourceLocator;diff --git a/libs/resources/tests/ResourceTestHelper.h b/libs/resources/tests/ResourceTestHelper.hindex 336b318cef..9c672f7a9d 100644--- a/libs/resources/tests/ResourceTestHelper.h+++ b/libs/resources/tests/ResourceTestHelper.h@@ -274,19 +274,14 @@ bool recreateDatabaseForATest(KisResourceLocator *locator, const QString &srcLocreturn dbResources;};- auto enableForeignKeys = [](bool isEnabled) {- KisSqlQueryLoader loader("inline://enable_foreign_keys",- QString("PRAGMA foreign_keys = %1").arg(isEnabled ? "on" : "off"));- loader.exec();- };-if (QSqlDatabase::database(QSqlDatabase::defaultConnection, false).isOpen()) {try {KisResourceModelProvider::testingCloseAllQueries();- KisDatabaseTransactionLock transactionLock(QSqlDatabase::database());+ // foreign keys should be disabled outside the transaction's scope!+ KisResourceCacheDb::setForeignKeysStateImpl(false);- enableForeignKeys(false);+ KisDatabaseTransactionLock transactionLock(QSqlDatabase::database());Q_FOREACH (const QString &dbResourceType, QStringList({"table", "index", "trigger", "view"})) {auto resources = listDbResources(dbResourceType);@@ -295,11 +290,12 @@ bool recreateDatabaseForATest(KisResourceLocator *locator, const QString &srcLocdropDbResource(dbResourceType, resource);}}- enableForeignKeys(true);// defuse the lock and save the resultstransactionLock.commit();+ KisResourceCacheDb::setForeignKeysStateImpl(true);+} catch (const KisSqlQueryLoader::SQLException &e) {qWarning().noquote() << "ERROR: failed to execute query:" << e.message;qWarning().noquote() << " file:" << e.filePath;diff --git a/libs/resources/tests/TestResourceLocator.cpp b/libs/resources/tests/TestResourceLocator.cppindex fab2e9cc66..1a51a68c51 100644--- a/libs/resources/tests/TestResourceLocator.cpp+++ b/libs/resources/tests/TestResourceLocator.cpp@@ -47,6 +47,7 @@ void TestResourceLocator::initTestCase()// disable database migration debug messages to avoid bloating the outputconst_cast<QLoggingCategory&>(_30010()).setEnabled(QtDebugMsg, false);const_cast<QLoggingCategory&>(_30010()).setEnabled(QtInfoMsg, false);+ qputenv("KRITA_OVERRIDE_USE_FOREIGN_KEYS", "1");ResourceTestHelper::initTestDb();@@ -69,6 +70,25 @@ void TestResourceLocator::init()QVERIFY(ResourceTestHelper::recreateDatabaseForATest(m_locator, m_srcLocation, m_dstLocation));}+void TestResourceLocator::testForeignKeysAreEnabled()+{+ /// in the TestResourceLocator unittest the foreign keys should be enabled+ /// explicitly by KRITA_OVERRIDE_USE_FOREIGN_KEYS++ try {++ QCOMPARE(KisResourceCacheDb::getForeignKeysStateImpl(), true);++ } catch (const KisSqlQueryLoader::SQLException &e) {+ qWarning().noquote() << "ERROR: failed to execute query:" << e.message;+ qWarning().noquote() << " file:" << e.filePath;+ qWarning().noquote() << " statement:" << e.statementIndex;+ qWarning().noquote() << " error:" << e.sqlError.text();++ QFAIL("SQL Error");+ }+}+void TestResourceLocator::testLocatorInitialization(){QVERIFY(QDir(m_dstLocation).exists());@@ -776,6 +796,8 @@ void TestResourceLocator::testOrphanedMetadataRemoval()QCOMPARE(countMetaDataForResource(5), 1);try {+ KisResourceCacheDb::setForeignKeysStateImpl(false);+KisDatabaseTransactionLock transactionLock(QSqlDatabase::database());/**@@ -783,13 +805,13 @@ void TestResourceLocator::testOrphanedMetadataRemoval()* records but "forgetting" to remove its metadata.*/KisSqlQueryLoader loader("inline://make_resource_orphaned",- "PRAGMA foreign_keys=off;""DELETE FROM resources WHERE id = 5;"- "DELETE FROM versioned_resources WHERE resource_id = 5;"- "PRAGMA foreign_keys=on;");+ "DELETE FROM versioned_resources WHERE resource_id = 5;");loader.exec();transactionLock.commit();++ KisResourceCacheDb::setForeignKeysStateImpl(true);} catch (const KisSqlQueryLoader::SQLException &e) {qWarning().noquote() << "ERROR: failed to execute query:" << e.message;qWarning().noquote() << " file:" << e.filePath;diff --git a/libs/resources/tests/TestResourceLocator.h b/libs/resources/tests/TestResourceLocator.hindex ab295e304b..202891f6e3 100644--- a/libs/resources/tests/TestResourceLocator.h+++ b/libs/resources/tests/TestResourceLocator.h@@ -18,6 +18,9 @@ class TestResourceLocator : public QObjectprivate Q_SLOTS:void initTestCase();void init();++ void testForeignKeysAreEnabled();+void testLocatorInitialization();void testResourceLocationBase();diff --git a/libs/version/kritaversion.h.cmake b/libs/version/kritaversion.h.cmakeindex 2a9cc6f120..b8b5132cca 100644--- a/libs/version/kritaversion.h.cmake+++ b/libs/version/kritaversion.h.cmake@@ -38,20 +38,6 @@*/#define KRITA_STABLE_VERSION_MAJOR @KRITA_STABLE_VERSION_MAJOR@-/**- * @def KRITA_VERSION_MAJOR- * @ingroup KritaMacros- * @brief Major version of Krita, at compile time- *- * Generally it's the same as KRITA_STABLE_VERSION_MAJOR but for unstable x.0- * x is decreased by one, e.g. 3.0 Beta is 2.99.-*/-#if !defined KRITA_STABLE && @KRITA_STABLE_VERSION_MINOR@ == 0-# define KRITA_VERSION_MAJOR (KRITA_STABLE_VERSION_MAJOR - 1)-#else-# define KRITA_VERSION_MAJOR KRITA_STABLE_VERSION_MAJOR-#endif-/*** @def KRITA_STABLE_VERSION_MINOR* @ingroup KritaMacros@@ -60,23 +46,6 @@*/#define KRITA_STABLE_VERSION_MINOR @KRITA_STABLE_VERSION_MINOR@-/**- * @def KRITA_VERSION_MINOR- * @ingroup KritaMacros- * @brief Minor version of Krita, at compile time- *- * Generally it's equal to KRITA_STABLE_VERSION_MINOR for stable releases,- * equal to 99 for x.0 unstable releases (e.g. it's 3.0 Beta has minor version 99),- * and equal to KRITA_STABLE_VERSION_MINOR-1 for unstable releases other than x.0.- */-#ifdef KRITA_STABLE-# define KRITA_VERSION_MINOR KRITA_STABLE_VERSION_MINOR-#elif KRITA_STABLE_VERSION_MINOR == 0-# define KRITA_VERSION_MINOR 99-#else-# define KRITA_VERSION_MINOR (KRITA_STABLE_VERSION_MINOR - 1)-#endif-/*** @def KRITA_VERSION_RELEASE* @ingroup KritaMacros@@ -85,19 +54,6 @@*/#define KRITA_VERSION_RELEASE @KRITA_VERSION_RELEASE@-/**- * @def KRITA_STABLE_VERSION_RELEASE- * @ingroup KritaMacros- * @brief Release version of Krita, at compile time.- *- * Equal to KRITA_VERSION_RELEASE for stable releases and 0 for unstable ones.- */-#ifdef KRITA_STABLE-# define KRITA_STABLE_VERSION_RELEASE 0-#else-# define KRITA_STABLE_VERSION_RELEASE @KRITA_VERSION_RELEASE@-#endif-/*** @def KRITA_ALPHA* @ingroup KritaMacros@@ -144,6 +100,6 @@* the Krita version used at runtime.*/#define KRITA_VERSION \- KRITA_MAKE_VERSION(KRITA_VERSION_MAJOR,KRITA_VERSION_MINOR,KRITA_VERSION_RELEASE)+ KRITA_MAKE_VERSION(KRITA_STABLE_VERSION_MAJOR,KRITA_STABLE_VERSION_MINOR,KRITA_VERSION_RELEASE)#endif // _KRITA_VERSION_H_