Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement the database track command #38

Merged
merged 2 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,14 @@ If you want to check standalone feature then you can execute below artisan comma

<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>


---
> #### **php artisan db:track**
>
> 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.
>
>
> You can also filter with --table=, --action=, --status=.
>

**Note:**

Expand Down
20 changes: 17 additions & 3 deletions src/Commands/DBAuditCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Illuminate\Console\Command;
use Vcian\LaravelDBAuditor\Constants\Constant;

use function Laravel\Prompts\select;

class DBAuditCommand extends Command
{
/**
Expand All @@ -19,15 +21,23 @@ class DBAuditCommand extends Command
*
* @var string
*/
protected $description = 'Database Audit : Check Standard and Constraint';
protected $description = 'Database Audit : Check Standard and Constraint with track';

/**
* Execute the console command.
* @return void
*/
public function handle(): void
{
$commandSelect = $this->choice('Please Select feature which would you like to do', [Constant::STANDARD_COMMAND, Constant::CONSTRAINT_COMMAND, Constant::SUMMARY_COMMAND]);
$commandSelect = select(
label: 'Please Select feature which would you like to do',
options: [
Constant::STANDARD_COMMAND,
Constant::CONSTRAINT_COMMAND,
Constant::SUMMARY_COMMAND,
Constant::TRACK_COMMAND,
],
default: Constant::SUMMARY_COMMAND
);

if ($commandSelect === Constant::STANDARD_COMMAND) {
$this->call('db:standard');
Expand All @@ -40,5 +50,9 @@ public function handle(): void
if ($commandSelect === Constant::SUMMARY_COMMAND) {
$this->call('db:summary');
}

if ($commandSelect === Constant::TRACK_COMMAND) {
$this->call('db:track');
}
}
}
66 changes: 29 additions & 37 deletions src/Commands/DBConstraintCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
use Illuminate\Support\Facades\Log;
use Vcian\LaravelDBAuditor\Constants\Constant;
use Vcian\LaravelDBAuditor\Traits\Audit;
use function Termwind\{render};

use function Laravel\Prompts\confirm;
use function Laravel\Prompts\select;
use function Termwind\{renderUsing};
use function Termwind\{render};

class DBConstraintCommand extends Command
{
Expand Down Expand Up @@ -38,10 +41,11 @@ class DBConstraintCommand extends Command
public function handle(): int|string
{
try {

$tableName = $this->components->choice(
__('Lang::messages.constraint.question.table_selection'),
$this->getTableList()

$tableName = select(
label: __('Lang::messages.constraint.question.table_selection'),
options: $this->getTableList(),
default: $this->getTableList()[0]
);

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

$this->skip = Constant::STATUS_FALSE;
$constraintList = $this->getConstraintList($tableName, $noConstraintFields);
$selectConstrain = $this->choice(
__('Lang::messages.constraint.question.constraint_selection'),
$constraintList
$selectConstrain = select(
label: __('Lang::messages.constraint.question.constraint_selection'),
options: $constraintList,
default: $constraintList[0]
);

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

/**
* Display selected table
* @param string $tableName
* @return void
*/
public function displayTable(string $tableName): void
{

$data = [
"table" => $tableName,
"size" => $this->getTableSize($tableName),
"fields" => $this->getFieldsDetails($tableName),
'table' => $tableName,
'size' => $this->getTableSize($tableName),
'fields' => $this->getFieldsDetails($tableName),
'field_count' => count($this->getFieldsDetails($tableName)),
'constrain' => [
'primary' => $this->getConstraintField($tableName, Constant::CONSTRAINT_PRIMARY_KEY),
'unique' => $this->getConstraintField($tableName, Constant::CONSTRAINT_UNIQUE_KEY),
'foreign' => $this->getConstraintField($tableName, Constant::CONSTRAINT_FOREIGN_KEY),
'index' => $this->getConstraintField($tableName, Constant::CONSTRAINT_INDEX_KEY)
]
'index' => $this->getConstraintField($tableName, Constant::CONSTRAINT_INDEX_KEY),
],
];

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

/**
* Display error messages
* @param string $message
*/
public function errorMessage(string $message): void
{
Expand All @@ -115,7 +117,6 @@ public function errorMessage(string $message): void

/**
* Display success messages
* @param string $message
*/
public function successMessage(string $message): void
{
Expand All @@ -124,9 +125,6 @@ public function successMessage(string $message): void

/**
* Get Foreign Key Constrain
* @param string $tableName
* @param string $selectField
* @return void
*/
public function foreignKeyConstraint(string $tableName, string $selectField): void
{
Expand All @@ -145,7 +143,7 @@ public function foreignKeyConstraint(string $tableName, string $selectField): vo
do {
$referenceField = $this->anticipate(__('Lang::messages.constraint.question.foreign_field'), $fields);

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

if (!$this->skip) {
if (! $this->skip) {
if ($referenceFieldType['data_type'] !== $selectedFieldType['data_type']) {

render('
<div class="mt-1">
<div class="flex space-x-1">
<span class="font-bold text-green">' . $selectedFieldType['data_type'] . '</span>
<i class="text-blue">' . $selectField . '</i>
<span class="font-bold text-green">'.$selectedFieldType['data_type'].'</span>
<i class="text-blue">'.$selectField.'</i>
<span class="flex-1 content-repeat-[.] text-gray"></span>
<i class="text-blue">' . $referenceField . '</i>
<span class="font-bold text-green">' . $referenceFieldType['data_type'] . '</span>
<i class="text-blue">'.$referenceField.'</i>
<span class="font-bold text-green">'.$referenceFieldType['data_type'].'</span>
</div>
</div>
');
Expand All @@ -184,12 +182,6 @@ public function foreignKeyConstraint(string $tableName, string $selectField): vo
}
}

/**
* @param string $selectConstrain
* @param array $noConstraintFields
* @param string $tableName
* @return void
*/
public function selectedConstraint(string $selectConstrain, array $noConstraintFields, string $tableName): void
{

Expand All @@ -201,7 +193,7 @@ public function selectedConstraint(string $selectConstrain, array $noConstraintF
}
}

if (!$this->skip) {
if (! $this->skip) {
if ($selectConstrain === Constant::CONSTRAINT_PRIMARY_KEY || $selectConstrain === Constant::CONSTRAINT_FOREIGN_KEY) {
$fields = $noConstraintFields['integer'];
} else {
Expand All @@ -215,9 +207,9 @@ public function selectedConstraint(string $selectConstrain, array $noConstraintF
}
}

if (!$this->skip) {
if (! $this->skip) {
$selectField = $this->choice(
__('Lang::messages.constraint.question.field_selection') . ' ' . strtolower($selectConstrain) . ' key',
__('Lang::messages.constraint.question.field_selection').' '.strtolower($selectConstrain).' key',
$fields
);

Expand All @@ -229,7 +221,7 @@ public function selectedConstraint(string $selectConstrain, array $noConstraintF
}
}

if (!$this->skip) {
if (! $this->skip) {
renderUsing($this->output);

$this->successMessage(__('Lang::messages.constraint.success_message.constraint_added'));
Expand Down
7 changes: 4 additions & 3 deletions src/Commands/DBSummaryCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Console\Command;
use Vcian\LaravelDBAuditor\Traits\DBConnection;

use function Termwind\{render};

class DBSummaryCommand extends Command
Expand All @@ -28,15 +29,15 @@ class DBSummaryCommand extends Command
* Execute the console command.
*/
public function handle()
{
{
$this->table(
['Database Name', 'Size', 'Table Count', 'Engin', 'Character Set'],
['Database Name', 'Size', 'Table Count', 'Engin', 'Character Set'],
[[
$this->getDatabaseName(),
$this->getDatabaseSize(),
count($this->getTableList()),
$this->getDatabaseEngin(),
$this->getCharacterSetName()
$this->getCharacterSetName(),
]]
);

Expand Down
123 changes: 123 additions & 0 deletions src/Commands/DBTrackCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php

namespace Vcian\LaravelDBAuditor\Commands;

use Illuminate\Console\Command;
use Vcian\LaravelDBAuditor\Traits\DBMigrationTrack;

use function Laravel\Prompts\table;
use function Termwind\{render};

class DBTrackCommand extends Command
{
use DBMigrationTrack;

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'db:track {--table=} {--action=} {--status=}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Track the the database information.';

/**
* Execute the console command.
*/
public function handle()
{
$actionKeywords = ['U' => 'Update', 'u' => 'Update', 'c' => 'Create', 'C' => 'Create'];
$statusKeywords = ['M' => 'Migrated', 'm' => 'Migrated', 'P' => 'Pending', 'p' => 'Pending'];

$data = $this->collectDataFromFile();

if ($this->option('table')) {

$data = $this->filter('table', $data, $this->option('table'));

} elseif ($this->option('action')) {

if (array_key_exists($this->option('action'), $actionKeywords)) {
$action = $actionKeywords[$this->option('action')];
} else {
$action = ucfirst($this->option('action'));
}
$data = $this->filter('action', $data, $action);

} elseif ($this->option('status')) {

if (array_key_exists($this->option('status'), $statusKeywords)) {
$status = $statusKeywords[$this->option('status')];
} else {
$status = ucfirst($this->option('status'));
}
$data = $this->filter('status', $data, $status);
}

table(
['Date', 'Table', 'Fields', 'Action', 'File Name', 'Status', 'Created By'],
$data
);

return self::SUCCESS;
}

/**
* Get Database related information from the migration file.
* Collect all the details into one place.
*/
public function collectDataFromFile()
{
$data = [];

$filesInFolder = \File::files('database/migrations');

foreach ($filesInFolder as $path) {
$file = pathinfo($path);

$fileName = $file['basename'];

array_push($data,
[
$this->getMigrationDate($file['filename']),
$this->getMigrationTableName($fileName),
$this->replaceStringWithDots($this->getMigrationFieldName($fileName)),
$this->getMigrationAction($fileName),
$fileName,
$this->getMigrationStatus($file['filename']),
$this->getMigrationCreatedBy($fileName),
]
);

}

return $data;
}

/**
* Filter based on the command argument.
*/
public function filter($filterType, $data, $filter)
{
$result = array_filter($data, function ($item) use ($filter, $filterType) {

switch ($filterType) {
case 'table':
return $item[1] === $filter;
case 'action':
return $item[3] === $filter;
case 'status':
return $item[5] === $filter;
default:
return $item;
}
});

return array_values($result);
}
}
Loading