Skip to content

Commit

Permalink
Change the track command file function
Browse files Browse the repository at this point in the history
Replace the dash symbol with current user in created by field
  • Loading branch information
vc-urvin committed Jan 18, 2024
1 parent ced846e commit 38f1d75
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 56 deletions.
56 changes: 1 addition & 55 deletions src/Commands/DBTrackCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function handle()
$actionKeywords = ['U' => 'Update', 'u' => 'Update', 'c' => 'Create', 'C' => 'Create'];
$statusKeywords = ['M' => 'Migrated', 'm' => 'Migrated', 'P' => 'Pending', 'p' => 'Pending'];

$data = $this->collectDataFromFile();
$data = $this->collectDataFromFile('command');

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

Expand Down Expand Up @@ -66,58 +66,4 @@ public function handle()

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);
}
}
73 changes: 72 additions & 1 deletion src/Traits/DBMigrationTrack.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,79 @@

namespace Vcian\LaravelDBAuditor\Traits;

use Illuminate\Support\Facades\File;
use Vcian\LaravelDBAuditor\Constants\Constant;

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

$filesInFolder = File::files(database_path('migrations'));

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

$fileName = $file['basename'];

if ($type === 'command') {
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),
]
);
} else {
array_push($data,
[
'date' => $this->getMigrationDate($file['filename']),
'table' => $this->getMigrationTableName($fileName),
'fields' => $this->getMigrationFieldName($fileName),
'action' => $this->getMigrationAction($fileName),
'file' => $fileName,
'status' => $this->getMigrationStatus($file['filename']),
'createdby' => $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);
}

/**
* Read File and Get File Content
*/
Expand Down Expand Up @@ -112,10 +181,12 @@ public function replaceStringWithDots(string $inputString): string
*/
public function getMigrationCreatedBy(string $file): string
{
$currentUser = gethostname();

$repositoryPath = base_path();
$migrationFilePath = 'database/migrations/'.$file;
$authorName = trim(shell_exec("git -C $repositoryPath log --follow --format='%an' -- $migrationFilePath"));

return $authorName != '' ? $authorName : '-';
return $authorName != '' ? $authorName : $currentUser;
}
}

0 comments on commit 38f1d75

Please sign in to comment.