Skip to content

Commit eba89ae

Browse files
authored
Merge pull request vcian#38 from vc-urvin/main
Implement the database track command
2 parents a5822c9 + ced846e commit eba89ae

File tree

8 files changed

+342
-57
lines changed

8 files changed

+342
-57
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,14 @@ If you want to check standalone feature then you can execute below artisan comma
6767

6868
<p align="center"><img src="https://raw.githubusercontent.com/vcian/art/main/laravel-db-auditor/db-standard-table-report-2.png" width="100%" alt="Logo Laravel DB Auditor"></p>
6969

70-
70+
---
71+
> #### **php artisan db:track**
72+
>
73+
> This command give you the track of the database files. Like when it's created with how many field in which table or whom created. this type of information show in the result.
74+
>
75+
>
76+
> You can also filter with --table=, --action=, --status=.
77+
>
7178
7279
**Note:**
7380

src/Commands/DBAuditCommand.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use Illuminate\Console\Command;
66
use Vcian\LaravelDBAuditor\Constants\Constant;
77

8+
use function Laravel\Prompts\select;
9+
810
class DBAuditCommand extends Command
911
{
1012
/**
@@ -19,15 +21,23 @@ class DBAuditCommand extends Command
1921
*
2022
* @var string
2123
*/
22-
protected $description = 'Database Audit : Check Standard and Constraint';
24+
protected $description = 'Database Audit : Check Standard and Constraint with track';
2325

2426
/**
2527
* Execute the console command.
26-
* @return void
2728
*/
2829
public function handle(): void
2930
{
30-
$commandSelect = $this->choice('Please Select feature which would you like to do', [Constant::STANDARD_COMMAND, Constant::CONSTRAINT_COMMAND, Constant::SUMMARY_COMMAND]);
31+
$commandSelect = select(
32+
label: 'Please Select feature which would you like to do',
33+
options: [
34+
Constant::STANDARD_COMMAND,
35+
Constant::CONSTRAINT_COMMAND,
36+
Constant::SUMMARY_COMMAND,
37+
Constant::TRACK_COMMAND,
38+
],
39+
default: Constant::SUMMARY_COMMAND
40+
);
3141

3242
if ($commandSelect === Constant::STANDARD_COMMAND) {
3343
$this->call('db:standard');
@@ -40,5 +50,9 @@ public function handle(): void
4050
if ($commandSelect === Constant::SUMMARY_COMMAND) {
4151
$this->call('db:summary');
4252
}
53+
54+
if ($commandSelect === Constant::TRACK_COMMAND) {
55+
$this->call('db:track');
56+
}
4357
}
4458
}

src/Commands/DBConstraintCommand.php

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
use Illuminate\Support\Facades\Log;
88
use Vcian\LaravelDBAuditor\Constants\Constant;
99
use Vcian\LaravelDBAuditor\Traits\Audit;
10-
use function Termwind\{render};
10+
11+
use function Laravel\Prompts\confirm;
12+
use function Laravel\Prompts\select;
1113
use function Termwind\{renderUsing};
14+
use function Termwind\{render};
1215

1316
class DBConstraintCommand extends Command
1417
{
@@ -38,10 +41,11 @@ class DBConstraintCommand extends Command
3841
public function handle(): int|string
3942
{
4043
try {
41-
42-
$tableName = $this->components->choice(
43-
__('Lang::messages.constraint.question.table_selection'),
44-
$this->getTableList()
44+
45+
$tableName = select(
46+
label: __('Lang::messages.constraint.question.table_selection'),
47+
options: $this->getTableList(),
48+
default: $this->getTableList()[0]
4549
);
4650

4751
$this->displayTable($tableName);
@@ -56,13 +60,14 @@ public function handle(): int|string
5660
if (empty($noConstraintFields)) {
5761
$continue = Constant::STATUS_FALSE;
5862
} else {
59-
if ($this->confirm(__('Lang::messages.constraint.question.continue'))) {
63+
if (confirm(label: __('Lang::messages.constraint.question.continue'))) {
6064

6165
$this->skip = Constant::STATUS_FALSE;
6266
$constraintList = $this->getConstraintList($tableName, $noConstraintFields);
63-
$selectConstrain = $this->choice(
64-
__('Lang::messages.constraint.question.constraint_selection'),
65-
$constraintList
67+
$selectConstrain = select(
68+
label: __('Lang::messages.constraint.question.constraint_selection'),
69+
options: $constraintList,
70+
default: $constraintList[0]
6671
);
6772

6873
$this->selectedConstraint($selectConstrain, $noConstraintFields, $tableName);
@@ -81,31 +86,28 @@ public function handle(): int|string
8186

8287
/**
8388
* Display selected table
84-
* @param string $tableName
85-
* @return void
8689
*/
8790
public function displayTable(string $tableName): void
8891
{
8992

9093
$data = [
91-
"table" => $tableName,
92-
"size" => $this->getTableSize($tableName),
93-
"fields" => $this->getFieldsDetails($tableName),
94+
'table' => $tableName,
95+
'size' => $this->getTableSize($tableName),
96+
'fields' => $this->getFieldsDetails($tableName),
9497
'field_count' => count($this->getFieldsDetails($tableName)),
9598
'constrain' => [
9699
'primary' => $this->getConstraintField($tableName, Constant::CONSTRAINT_PRIMARY_KEY),
97100
'unique' => $this->getConstraintField($tableName, Constant::CONSTRAINT_UNIQUE_KEY),
98101
'foreign' => $this->getConstraintField($tableName, Constant::CONSTRAINT_FOREIGN_KEY),
99-
'index' => $this->getConstraintField($tableName, Constant::CONSTRAINT_INDEX_KEY)
100-
]
102+
'index' => $this->getConstraintField($tableName, Constant::CONSTRAINT_INDEX_KEY),
103+
],
101104
];
102105

103106
render(view('DBAuditor::constraint', ['data' => $data]));
104107
}
105108

106109
/**
107110
* Display error messages
108-
* @param string $message
109111
*/
110112
public function errorMessage(string $message): void
111113
{
@@ -115,7 +117,6 @@ public function errorMessage(string $message): void
115117

116118
/**
117119
* Display success messages
118-
* @param string $message
119120
*/
120121
public function successMessage(string $message): void
121122
{
@@ -124,9 +125,6 @@ public function successMessage(string $message): void
124125

125126
/**
126127
* Get Foreign Key Constrain
127-
* @param string $tableName
128-
* @param string $selectField
129-
* @return void
130128
*/
131129
public function foreignKeyConstraint(string $tableName, string $selectField): void
132130
{
@@ -145,7 +143,7 @@ public function foreignKeyConstraint(string $tableName, string $selectField): vo
145143
do {
146144
$referenceField = $this->anticipate(__('Lang::messages.constraint.question.foreign_field'), $fields);
147145

148-
if (!$referenceField || !$this->checkFieldExistOrNot($referenceTable, $referenceField)) {
146+
if (! $referenceField || ! $this->checkFieldExistOrNot($referenceTable, $referenceField)) {
149147
$this->errorMessage(__('Lang::messages.constraint.error_message.field_not_found'));
150148
} else {
151149
$foreignContinue = Constant::STATUS_TRUE;
@@ -163,17 +161,17 @@ public function foreignKeyConstraint(string $tableName, string $selectField): vo
163161
$this->errorMessage(__('Lang::messages.constraint.error_message.foreign_selected_table_match', ['foreign' => $referenceTable, 'selected' => $tableName]));
164162
}
165163

166-
if (!$this->skip) {
164+
if (! $this->skip) {
167165
if ($referenceFieldType['data_type'] !== $selectedFieldType['data_type']) {
168166

169167
render('
170168
<div class="mt-1">
171169
<div class="flex space-x-1">
172-
<span class="font-bold text-green">' . $selectedFieldType['data_type'] . '</span>
173-
<i class="text-blue">' . $selectField . '</i>
170+
<span class="font-bold text-green">'.$selectedFieldType['data_type'].'</span>
171+
<i class="text-blue">'.$selectField.'</i>
174172
<span class="flex-1 content-repeat-[.] text-gray"></span>
175-
<i class="text-blue">' . $referenceField . '</i>
176-
<span class="font-bold text-green">' . $referenceFieldType['data_type'] . '</span>
173+
<i class="text-blue">'.$referenceField.'</i>
174+
<span class="font-bold text-green">'.$referenceFieldType['data_type'].'</span>
177175
</div>
178176
</div>
179177
');
@@ -184,12 +182,6 @@ public function foreignKeyConstraint(string $tableName, string $selectField): vo
184182
}
185183
}
186184

187-
/**
188-
* @param string $selectConstrain
189-
* @param array $noConstraintFields
190-
* @param string $tableName
191-
* @return void
192-
*/
193185
public function selectedConstraint(string $selectConstrain, array $noConstraintFields, string $tableName): void
194186
{
195187

@@ -201,7 +193,7 @@ public function selectedConstraint(string $selectConstrain, array $noConstraintF
201193
}
202194
}
203195

204-
if (!$this->skip) {
196+
if (! $this->skip) {
205197
if ($selectConstrain === Constant::CONSTRAINT_PRIMARY_KEY || $selectConstrain === Constant::CONSTRAINT_FOREIGN_KEY) {
206198
$fields = $noConstraintFields['integer'];
207199
} else {
@@ -215,9 +207,9 @@ public function selectedConstraint(string $selectConstrain, array $noConstraintF
215207
}
216208
}
217209

218-
if (!$this->skip) {
210+
if (! $this->skip) {
219211
$selectField = $this->choice(
220-
__('Lang::messages.constraint.question.field_selection') . ' ' . strtolower($selectConstrain) . ' key',
212+
__('Lang::messages.constraint.question.field_selection').' '.strtolower($selectConstrain).' key',
221213
$fields
222214
);
223215

@@ -229,7 +221,7 @@ public function selectedConstraint(string $selectConstrain, array $noConstraintF
229221
}
230222
}
231223

232-
if (!$this->skip) {
224+
if (! $this->skip) {
233225
renderUsing($this->output);
234226

235227
$this->successMessage(__('Lang::messages.constraint.success_message.constraint_added'));

src/Commands/DBSummaryCommand.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Console\Command;
66
use Vcian\LaravelDBAuditor\Traits\DBConnection;
7+
78
use function Termwind\{render};
89

910
class DBSummaryCommand extends Command
@@ -28,15 +29,15 @@ class DBSummaryCommand extends Command
2829
* Execute the console command.
2930
*/
3031
public function handle()
31-
{
32+
{
3233
$this->table(
33-
['Database Name', 'Size', 'Table Count', 'Engin', 'Character Set'],
34+
['Database Name', 'Size', 'Table Count', 'Engin', 'Character Set'],
3435
[[
3536
$this->getDatabaseName(),
3637
$this->getDatabaseSize(),
3738
count($this->getTableList()),
3839
$this->getDatabaseEngin(),
39-
$this->getCharacterSetName()
40+
$this->getCharacterSetName(),
4041
]]
4142
);
4243

src/Commands/DBTrackCommand.php

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
3+
namespace Vcian\LaravelDBAuditor\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use Vcian\LaravelDBAuditor\Traits\DBMigrationTrack;
7+
8+
use function Laravel\Prompts\table;
9+
use function Termwind\{render};
10+
11+
class DBTrackCommand extends Command
12+
{
13+
use DBMigrationTrack;
14+
15+
/**
16+
* The name and signature of the console command.
17+
*
18+
* @var string
19+
*/
20+
protected $signature = 'db:track {--table=} {--action=} {--status=}';
21+
22+
/**
23+
* The console command description.
24+
*
25+
* @var string
26+
*/
27+
protected $description = 'Track the the database information.';
28+
29+
/**
30+
* Execute the console command.
31+
*/
32+
public function handle()
33+
{
34+
$actionKeywords = ['U' => 'Update', 'u' => 'Update', 'c' => 'Create', 'C' => 'Create'];
35+
$statusKeywords = ['M' => 'Migrated', 'm' => 'Migrated', 'P' => 'Pending', 'p' => 'Pending'];
36+
37+
$data = $this->collectDataFromFile();
38+
39+
if ($this->option('table')) {
40+
41+
$data = $this->filter('table', $data, $this->option('table'));
42+
43+
} elseif ($this->option('action')) {
44+
45+
if (array_key_exists($this->option('action'), $actionKeywords)) {
46+
$action = $actionKeywords[$this->option('action')];
47+
} else {
48+
$action = ucfirst($this->option('action'));
49+
}
50+
$data = $this->filter('action', $data, $action);
51+
52+
} elseif ($this->option('status')) {
53+
54+
if (array_key_exists($this->option('status'), $statusKeywords)) {
55+
$status = $statusKeywords[$this->option('status')];
56+
} else {
57+
$status = ucfirst($this->option('status'));
58+
}
59+
$data = $this->filter('status', $data, $status);
60+
}
61+
62+
table(
63+
['Date', 'Table', 'Fields', 'Action', 'File Name', 'Status', 'Created By'],
64+
$data
65+
);
66+
67+
return self::SUCCESS;
68+
}
69+
70+
/**
71+
* Get Database related information from the migration file.
72+
* Collect all the details into one place.
73+
*/
74+
public function collectDataFromFile()
75+
{
76+
$data = [];
77+
78+
$filesInFolder = \File::files('database/migrations');
79+
80+
foreach ($filesInFolder as $path) {
81+
$file = pathinfo($path);
82+
83+
$fileName = $file['basename'];
84+
85+
array_push($data,
86+
[
87+
$this->getMigrationDate($file['filename']),
88+
$this->getMigrationTableName($fileName),
89+
$this->replaceStringWithDots($this->getMigrationFieldName($fileName)),
90+
$this->getMigrationAction($fileName),
91+
$fileName,
92+
$this->getMigrationStatus($file['filename']),
93+
$this->getMigrationCreatedBy($fileName),
94+
]
95+
);
96+
97+
}
98+
99+
return $data;
100+
}
101+
102+
/**
103+
* Filter based on the command argument.
104+
*/
105+
public function filter($filterType, $data, $filter)
106+
{
107+
$result = array_filter($data, function ($item) use ($filter, $filterType) {
108+
109+
switch ($filterType) {
110+
case 'table':
111+
return $item[1] === $filter;
112+
case 'action':
113+
return $item[3] === $filter;
114+
case 'status':
115+
return $item[5] === $filter;
116+
default:
117+
return $item;
118+
}
119+
});
120+
121+
return array_values($result);
122+
}
123+
}

0 commit comments

Comments
 (0)