Skip to content

Commit 7f36dbe

Browse files
committed
Merge remote-tracking branch 'origin/development'
2 parents 7bbee6f + ff6bfce commit 7f36dbe

14 files changed

+400
-321
lines changed

composer.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
}
2020
],
2121
"require": {
22-
"php": "^8.0",
23-
"doctrine/dbal": "4.0.x-dev"
22+
"php": "^8.0"
2423
},
2524
"autoload": {
2625
"psr-4": {

composer.lock

-152
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Commands/DBAuditCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function handle()
3636
}
3737

3838
if ($commandSelect === Constant::CONSTRAIN_COMMAND) {
39-
$this->call('db:constrain');
39+
$this->call('db:constraint');
4040
}
4141
}
4242
}

src/Commands/DBConstrainCommand.php

+84-47
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Vcian\LaravelDBAuditor\Services\AuditService;
1010

1111
use function Termwind\{render};
12+
use function Termwind\{renderUsing};
1213

1314
class DBConstrainCommand extends Command
1415
{
@@ -17,7 +18,7 @@ class DBConstrainCommand extends Command
1718
*
1819
* @var string
1920
*/
20-
protected $signature = 'db:constrain {table?}';
21+
protected $signature = 'db:constraint {table?}';
2122

2223
/**
2324
* The console command description.
@@ -32,64 +33,100 @@ class DBConstrainCommand extends Command
3233
public function handle(AuditService $auditService)
3334
{
3435
try {
35-
$tableName = $this->argument('table');
3636

37-
$results = Constant::ARRAY_DECLARATION;
38-
39-
$referenceTable = null;
40-
$referenceField = null;
37+
$tableName = $this->components->choice(
38+
'Which table whould you like to audit?',
39+
$auditService->getTablesList()
40+
);
4141

42-
$constrains = [
43-
Constant::CONSTRAIN_PRIMARY_KEY,
44-
Constant::CONSTRAIN_INDEX_KEY,
45-
Constant::CONSTRAIN_UNIQUE_KEY,
46-
Constant::CONSTRAIN_FOREIGN_KEY,
47-
Constant::CONSTRAIN_ALL_KEY
42+
$data = [
43+
"table" => $tableName,
44+
"size" => $auditService->getTableSize($tableName),
45+
"fields" => $auditService->getTableFields($tableName),
46+
'field_count' => count($auditService->getTableFields($tableName)),
47+
'constrain' => [
48+
'primary' => $auditService->getConstrainField($tableName, Constant::CONSTRAIN_PRIMARY_KEY),
49+
'unique' => $auditService->getConstrainField($tableName, Constant::CONSTRAIN_UNIQUE_KEY),
50+
'foreign' => $auditService->getConstrainField($tableName, Constant::CONSTRAIN_FOREIGN_KEY),
51+
'index' => $auditService->getConstrainField($tableName, Constant::CONSTRAIN_INDEX_KEY)
52+
]
4853
];
4954

50-
$userInput = $this->choice(
51-
'Please Select',
52-
$constrains
53-
);
55+
render(view('DBAuditor::constraint', ['data' => $data]));
5456

5557
if ($tableName) {
56-
$results = $auditService->checkConstrain($tableName, $userInput);
57-
if (!$results) {
58-
return render('<div class="w-100 px-1 p-1 bg-red-600 text-center">No Table Found</div>');
59-
}
60-
render(view('DBAuditor::constraint', ['tables' => $results]));
61-
62-
$checkUserHasFields = $auditService->getFieldByUserInput($tableName, $userInput);
63-
if ($checkUserHasFields) {
64-
$userSelection = $this->choice('Do you want to add constrain into your table?', ['Yes', 'No']);
65-
if ($userSelection == "Yes") {
66-
if ($userInput === Constant::CONSTRAIN_ALL_KEY) {
67-
$options = [Constant::CONSTRAIN_INDEX_KEY, Constant::CONSTRAIN_UNIQUE_KEY, Constant::CONSTRAIN_FOREIGN_KEY];
68-
if (!$auditService->checkTableHasPrimaryKey($tableName)) {
69-
array_push($options, Constant::CONSTRAIN_PRIMARY_KEY);
58+
59+
$continue = Constant::STATUS_TRUE;
60+
do {
61+
62+
$noConstrainfields = $auditService->getNoConstrainFields($tableName);
63+
$constrainList = $auditService->getConstrainList($tableName, $noConstrainfields);
64+
65+
if ($noConstrainfields) {
66+
67+
$userInput = $this->confirm("Do you want add more constrain?");
68+
69+
if ($userInput) {
70+
71+
$selectConstrain = $this->choice(
72+
'Please select constrain which you want to add',
73+
$constrainList
74+
);
75+
76+
if ($selectConstrain === Constant::CONSTRAIN_PRIMARY_KEY || $selectConstrain === Constant::CONSTRAIN_FOREIGN_KEY) {
77+
$fields = $noConstrainfields['integer'];
78+
} else {
79+
$fields = $noConstrainfields['mix'];
80+
}
81+
82+
$selectField = $this->choice(
83+
'Please select field where you want to add ' . $selectConstrain,
84+
$fields
85+
);
86+
87+
if ($selectConstrain === Constant::CONSTRAIN_FOREIGN_KEY) {
88+
$tableHasValue = $auditService->tableHasValue($tableName);
89+
90+
if ($tableHasValue) {
91+
render('<div class="w-120 px-2 p-1 bg-red-600 text-center"> 😢 Can not apply Foreign Key | Please trancate table 😎 </div>');
92+
} else {
93+
$referenceTable = $this->ask("Please add foreign table name");
94+
$referenceField = $this->ask("Please add foreign table primary key name");
95+
$auditService->addConstrain($tableName, $selectField, $selectConstrain, $referenceTable, $referenceField);
96+
}
7097
}
71-
$userInput = $this->choice("Please select constraints which you want process", $options);
72-
}
73-
$selectField = $this->choice("Please Select Field where you want to apply " . strtolower($userInput) . " key", $checkUserHasFields);
74-
if ($userInput === Constant::CONSTRAIN_FOREIGN_KEY) {
75-
$referenceTable = $this->ask("Please add reference table name");
76-
$referenceField = $this->ask("Please add reference table primary key name");
77-
}
7898

79-
$auditService->addConstrain($tableName, $selectField, $userInput, $referenceTable, $referenceField);
80-
render('<div class="w-100 px-1 p-1 bg-green-600 text-center">Run Successfully</div>');
99+
$auditService->addConstrain($tableName, $selectField, $selectConstrain);
100+
101+
renderUsing($this->output);
102+
103+
render('<div class="w-120 px-2 p-1 bg-green-600 text-center"> 😎 Constrain Add Successfully 😎 </div>');
104+
105+
$data = [
106+
"table" => $tableName,
107+
"size" => $auditService->getTableSize($tableName),
108+
"fields" => $auditService->getTableFields($tableName),
109+
'field_count' => count($auditService->getTableFields($tableName)),
110+
'constrain' => [
111+
'primary' => $auditService->getConstrainField($tableName, Constant::CONSTRAIN_PRIMARY_KEY),
112+
'unique' => $auditService->getConstrainField($tableName, Constant::CONSTRAIN_UNIQUE_KEY),
113+
'foreign' => $auditService->getConstrainField($tableName, Constant::CONSTRAIN_FOREIGN_KEY),
114+
'index' => $auditService->getConstrainField($tableName, Constant::CONSTRAIN_INDEX_KEY)
115+
]
116+
];
117+
118+
render(view('DBAuditor::constraint', ['data' => $data]));
119+
} else {
120+
$continue = Constant::STATUS_FALSE;
121+
}
122+
} else {
123+
$continue = Constant::STATUS_FALSE;
81124
}
82-
}
83-
} else {
84-
$results = $auditService->getList($userInput);
85-
if (!$results) {
86-
return render('<div class="w-100 px-1 p-1 bg-red-600 text-center">No Table Found</div>');
87-
}
88-
render(view('DBAuditor::constraint', ['tables' => $results]));
125+
} while ($continue === Constant::STATUS_TRUE);
89126
}
90127
} catch (Exception $exception) {
91128
Log::error($exception->getMessage());
92-
return render('<div class="w-100 px-1 p-1 bg-red-600 text-center">' . $exception->getMessage() . '</div>');
129+
return $exception->getMessage();
93130
}
94131

95132
return self::SUCCESS;

src/Commands/DBStandardCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function handle(RuleService $ruleService)
4545
$tableStatus = $ruleService->tableRules($tableName);
4646

4747
if (!$tableStatus) {
48-
render('<div class="w-120 px-2 p-1 bg-red-600 text-center"> 😢 No Table Found 😩 </div>');
48+
render('<div class="w-120 py-2 px-2 bg-red-600 pr-0 pl-0 text-center">😢 No Table Found 😩</div>');
4949
} else {
5050
render(view('DBAuditor::fail_standard_table', ['tableStatus' => $tableStatus]));
5151
}

src/Database/migrations/update_table_index.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
*/
1212
public function up(): void
1313
{
14-
Schema::table($table_name, function (Blueprint $table) {
15-
$table->unsignedBigInteger($field_name)->change()->index();
14+
Schema::table($tableName, function (Blueprint $table) {
15+
$table->$dataType($fieldName)->change()->index();
1616
});
1717
}
1818

@@ -21,7 +21,7 @@ public function up(): void
2121
*/
2222
public function down(): void
2323
{
24-
Schema::table($table_name, function (Blueprint $table) {
24+
Schema::table($tableName, function (Blueprint $table) {
2525
//
2626
});
2727
}

src/Database/migrations/update_table_primary.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
public function up(): void
1313
{
1414
Schema::table($table_name, function (Blueprint $table) {
15-
$table->bigIncrements($field_name)->change();
15+
$table->$dataType($field_name)->change()->primary();
1616
});
1717
}
1818

src/Database/migrations/update_table_unique.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
public function up(): void
1313
{
1414
Schema::table($table_name, function (Blueprint $table) {
15-
$table->integer($field_name)->change()->unique();
15+
$table->$dataType($field_name)->change()->unique();
1616
});
1717
}
1818

0 commit comments

Comments
 (0)