From a754f32b8a298dbe7f80c3da4fb0f0bdd15aaeaf Mon Sep 17 00:00:00 2001
From: Jan Wennrich <jan.wennrich@pcsg.de>
Date: Tue, 17 Mar 2020 15:26:52 +0100
Subject: [PATCH] refactor: simplifies logic of dropping no longer needed
 primary keys

---
 src/QUI/Bricks/Events.php | 19 +++++--------------
 1 file changed, 5 insertions(+), 14 deletions(-)

diff --git a/src/QUI/Bricks/Events.php b/src/QUI/Bricks/Events.php
index 9e9425f..8ec50d1 100644
--- a/src/QUI/Bricks/Events.php
+++ b/src/QUI/Bricks/Events.php
@@ -225,8 +225,6 @@ public static function onPackageSetup(QUI\Package\Package $Package)
         // unique bricks cache patch
         $projects = QUI::getProjectManager()->getProjectList();
 
-        $databaseName = QUI::getDataBase()->getAttribute('dbname');
-
         foreach ($projects as $Project) {
             $projectCacheTable = QUI::getDBProjectTableName(
                 Manager::TABLE_CACHE,
@@ -234,18 +232,11 @@ public static function onPackageSetup(QUI\Package\Package $Package)
             );
 
             try {
-                $primaryExists = QUI::getDataBase()->fetchSQL("
-                    SELECT COUNT(constraint_type) AS primary_key_count FROM information_schema.table_constraints
-                    WHERE constraint_type = 'PRIMARY KEY' 
-                    AND table_name = '{$projectCacheTable}'
-                    AND TABLE_SCHEMA = '{$databaseName}'
-                ");
-
-                $isPrimaryKeyExisting = \boolval($primaryExists[0]['primary_key_count']);
-
-                // Primary key should only be dropped if it exists
-                if ($isPrimaryKeyExisting) {
-                    QUI::getDataBase()->fetchSQL("ALTER TABLE `{$projectCacheTable}` DROP PRIMARY KEY;");
+                // Only drop composite primary key if it exists
+                if (QUI::getDataBase()->table()->issetPrimaryKey($projectCacheTable, 'id')
+                    && QUI::getDataBase()->table()->issetPrimaryKey($projectCacheTable, 'area')) {
+                    // Primary key no longer exists and should be removed
+                    QUI::getDataBase()->getPDO()->exec("ALTER TABLE `{$projectCacheTable}` DROP PRIMARY KEY;");
                 }
             } catch (QUI\Exception $Exception) {
                 QUI\System\Log::addInfo($Exception->getMessage());
-- 
GitLab