Skip to content

Commit

Permalink
Update Unique and Index Constraint Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
vc-urvin committed Jun 28, 2023
1 parent 624c907 commit f8d3249
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
19 changes: 13 additions & 6 deletions src/Traits/Audit.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand All @@ -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) . "%'");
}
Expand Down Expand Up @@ -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());
Expand Down
14 changes: 10 additions & 4 deletions src/views/auditor/pages/audit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -437,12 +437,18 @@ function addConstraint() {
}),
success: function(response) {
if (response) {
var newElement = $('<img src="auditor/icon/gray-key.svg" alt="key" class="m-auto" />');
var key;
if(constraint.toLowerCase() === "primary") {
key = $('<img src="auditor/icon/green-key.svg" alt="key" class="m-auto" />');
} else {
key = $('<img src="auditor/icon/gray-key.svg" alt="key" class="m-auto" />');
}
$(".add-constraint-" + response + '-' + constraint).replaceWith(newElement);
$(".add-constraint-" + response + '-' + constraint).replaceWith(key);
$(".toast-container").css("display", "block");
$(".toastCustom").replaceWith("<p class='toastCustom'>" + constraint +
" Added Successfully</p>");
$(".toastCustom").replaceWith("<p class='toastCustom'>" + constraint.toLowerCase() +
" key added successfully</p>");
setTimeout(function() {
$(".toast-container").css("display", "none");;
Expand Down

0 comments on commit f8d3249

Please sign in to comment.