diff --git a/src/Traits/Audit.php b/src/Traits/Audit.php index 1955bc7..4405e53 100644 --- a/src/Traits/Audit.php +++ b/src/Traits/Audit.php @@ -85,7 +85,7 @@ public function getConstraintList(string $tableName, array $fields): array if (!empty($fields['mix'])) { $constrainList[] = Constant::CONSTRAINT_INDEX_KEY; - if (empty($this->getUniqueFields($tableName, $fields['mix']))) { + if (!empty($this->getUniqueFields($tableName, $fields['mix']))) { $constrainList[] = Constant::CONSTRAINT_UNIQUE_KEY; } } @@ -108,7 +108,7 @@ public function getConstraintField(string $tableName, string $input): array } if($input === Constant::CONSTRAINT_INDEX_KEY) { - $result = DB::select("SHOW INDEX FROM {$tableName}"); + $result = DB::select("SHOW INDEX FROM {$tableName} where Key_name != 'PRIMARY' and Key_name not like '%unique%'"); } else { $result = DB::select("SHOW KEYS FROM {$tableName} WHERE Key_name LIKE '%" . strtolower($input) . "%'"); } @@ -282,12 +282,19 @@ public function getUniqueFields(string $tableName, array $fields): array $uniqueField = Constant::ARRAY_DECLARATION; try { foreach ($fields as $field) { - $query = "SELECT `" . $field . "`, COUNT(`" . $field . "`) as count FROM " . $tableName . " GROUP BY `" . $field . "` HAVING COUNT(`" . $field . "`) > 1"; - $result = DB::select($query); - if (empty($result)) { - $uniqueField[] = $field; + $getUniqueQuery = "SELECT * FROM INFORMATION_SCHEMA.STATISTICS + WHERE TABLE_SCHEMA = '". $this->getDatabaseName() ."' AND TABLE_NAME = '".$tableName."' AND COLUMN_NAME = '".$field."' AND NON_UNIQUE = 0"; + $resultUniqueQuery = DB::select($getUniqueQuery); + if(!$resultUniqueQuery) { + $query = "SELECT `" . $field . "`, COUNT(`" . $field . "`) as count FROM " . $tableName . " GROUP BY `" . $field . "` HAVING COUNT(`" . $field . "`) > 1"; + $result = DB::select($query); + + if (empty($result)) { + $uniqueField[] = $field; + } } + } } catch (Exception $exception) { Log::error($exception->getMessage()); diff --git a/src/views/auditor/pages/audit.blade.php b/src/views/auditor/pages/audit.blade.php index d2fd635..ba727c4 100644 --- a/src/views/auditor/pages/audit.blade.php +++ b/src/views/auditor/pages/audit.blade.php @@ -437,12 +437,18 @@ function addConstraint() { }), success: function(response) { if (response) { - var newElement = $('key'); + var key; + + if(constraint.toLowerCase() === "primary") { + key = $('key'); + } else { + key = $('key'); + } - $(".add-constraint-" + response + '-' + constraint).replaceWith(newElement); + $(".add-constraint-" + response + '-' + constraint).replaceWith(key); $(".toast-container").css("display", "block"); - $(".toastCustom").replaceWith("

" + constraint + - " Added Successfully

"); + $(".toastCustom").replaceWith("

" + constraint.toLowerCase() + + " key added successfully

"); setTimeout(function() { $(".toast-container").css("display", "none");;