Skip to content

Commit 6dbdc89

Browse files
authored
Merge pull request #42 from vc-urvin/main
Add Current User Name If Git commit not found or not commited the file
2 parents f94614b + 005b7aa commit 6dbdc89

File tree

2 files changed

+73
-56
lines changed

2 files changed

+73
-56
lines changed

src/Commands/DBTrackCommand.php

+1-55
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function handle()
3434
$actionKeywords = ['U' => 'Update', 'u' => 'Update', 'c' => 'Create', 'C' => 'Create'];
3535
$statusKeywords = ['M' => 'Migrated', 'm' => 'Migrated', 'P' => 'Pending', 'p' => 'Pending'];
3636

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

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

@@ -66,58 +66,4 @@ public function handle()
6666

6767
return self::SUCCESS;
6868
}
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-
}
12369
}

src/Traits/DBMigrationTrack.php

+72-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,79 @@
22

33
namespace Vcian\LaravelDBAuditor\Traits;
44

5+
use Illuminate\Support\Facades\File;
56
use Vcian\LaravelDBAuditor\Constants\Constant;
67

78
trait DBMigrationTrack
89
{
10+
/**
11+
* Get Database related information from the migration file.
12+
* Collect all the details into one place.
13+
*/
14+
public function collectDataFromFile($type = 'command')
15+
{
16+
$data = [];
17+
18+
$filesInFolder = File::files(database_path('migrations'));
19+
20+
foreach ($filesInFolder as $path) {
21+
$file = pathinfo($path);
22+
23+
$fileName = $file['basename'];
24+
25+
if ($type === 'command') {
26+
array_push($data,
27+
[
28+
$this->getMigrationDate($file['filename']),
29+
$this->getMigrationTableName($fileName),
30+
$this->replaceStringWithDots($this->getMigrationFieldName($fileName)),
31+
$this->getMigrationAction($fileName),
32+
$fileName,
33+
$this->getMigrationStatus($file['filename']),
34+
$this->getMigrationCreatedBy($fileName),
35+
]
36+
);
37+
} else {
38+
array_push($data,
39+
[
40+
'date' => $this->getMigrationDate($file['filename']),
41+
'table' => $this->getMigrationTableName($fileName),
42+
'fields' => $this->getMigrationFieldName($fileName),
43+
'action' => $this->getMigrationAction($fileName),
44+
'file' => $fileName,
45+
'status' => $this->getMigrationStatus($file['filename']),
46+
'createdby' => $this->getMigrationCreatedBy($fileName),
47+
]
48+
);
49+
}
50+
51+
}
52+
53+
return $data;
54+
}
55+
56+
/**
57+
* Filter based on the command argument.
58+
*/
59+
public function filter($filterType, $data, $filter)
60+
{
61+
$result = array_filter($data, function ($item) use ($filter, $filterType) {
62+
63+
switch ($filterType) {
64+
case 'table':
65+
return $item[1] === $filter;
66+
case 'action':
67+
return $item[3] === $filter;
68+
case 'status':
69+
return $item[5] === $filter;
70+
default:
71+
return $item;
72+
}
73+
});
74+
75+
return array_values($result);
76+
}
77+
978
/**
1079
* Read File and Get File Content
1180
*/
@@ -112,10 +181,12 @@ public function replaceStringWithDots(string $inputString): string
112181
*/
113182
public function getMigrationCreatedBy(string $file): string
114183
{
184+
$currentUser = gethostname();
185+
115186
$repositoryPath = base_path();
116187
$migrationFilePath = 'database/migrations/'.$file;
117188
$authorName = trim(shell_exec("git -C $repositoryPath log --follow --format='%an' -- $migrationFilePath"));
118189

119-
return $authorName != '' ? $authorName : '-';
190+
return $authorName != '' ? $authorName : $currentUser;
120191
}
121192
}

0 commit comments

Comments
 (0)