Skip to content

Commit 27f2d79

Browse files
authored
Merge pull request #63 from ruchit288/feature/postgres
Integrate postgresql.
2 parents 8c3c3f7 + 503e35f commit 27f2d79

27 files changed

+864
-183
lines changed

src/Commands/DBAuditCommand.php

+6-7
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
namespace Vcian\LaravelDBAuditor\Commands;
44

55
use Illuminate\Console\Command;
6-
use League\Flysystem\Config;
76
use Vcian\LaravelDBAuditor\Constants\Constant;
87

8+
use function Laravel\Prompts\search;
99
use function Laravel\Prompts\select;
1010

1111
class DBAuditCommand extends Command
@@ -29,14 +29,13 @@ class DBAuditCommand extends Command
2929
*/
3030
public function handle(): void
3131
{
32-
$commands = match (connection_driver()) {
33-
Constant::SQLITE_DB => config('audit.sqlite_commands'),
34-
Constant::MYSQL_DB => config('audit.mysql_commands'),
35-
};
36-
3732
$commandSelect = select(
3833
label: 'Please Select feature which would you like to do',
39-
options: $commands,
34+
options: match (connection_driver()) {
35+
Constant::SQLITE_DB => config('audit.sqlite_commands'),
36+
Constant::MYSQL_DB => config('audit.mysql_commands'),
37+
Constant::POSTGRESQL_DB => config('audit.pgsql_commands'),
38+
},
4039
default: Constant::SUMMARY_COMMAND
4140
);
4241

src/Commands/DBConstraintCommand.php

+48-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Vcian\LaravelDBAuditor\Constants\Constant;
77
use Vcian\LaravelDBAuditor\Traits\Audit;
88

9+
use Vcian\LaravelDBAuditor\Traits\DBConstraint;
910
use Vcian\LaravelDBAuditor\Traits\DisplayTable;
1011
use function Laravel\Prompts\confirm;
1112
use function Laravel\Prompts\select;
@@ -14,7 +15,7 @@
1415

1516
class DBConstraintCommand extends Command
1617
{
17-
use DisplayTable, Audit;
18+
use Audit, DisplayTable, DBConstraint;
1819
/**
1920
* @var bool
2021
*/
@@ -40,8 +41,21 @@ class DBConstraintCommand extends Command
4041
*/
4142
public function handle(): int|string
4243
{
43-
$this->connection = connection_driver();
44-
$tableList = $this->getTableList();
44+
return match (connection_driver()) {
45+
'sqlite' => $this->sqlite(),
46+
'pgsql' => $this->pgsql(),
47+
default => $this->mysql(),
48+
};
49+
}
50+
51+
/**
52+
* MySQL Constraint
53+
* @return int
54+
*/
55+
public function mysql()
56+
{
57+
$tableList = collect($this->getTableList())
58+
->diff(config('audit.skip_tables'))->values()->toArray();
4559

4660
$tableName = select(
4761
label: __('Lang::messages.constraint.question.table_selection'),
@@ -60,10 +74,10 @@ public function handle(): int|string
6074
if (empty($noConstraintFields)) {
6175
$continue = Constant::STATUS_FALSE;
6276
} else {
63-
if (confirm(label: __('Lang::messages.constraint.question.continue'))) {
64-
77+
if (confirm(label: __('Lang::messages.constraint.question.add_constraint'))) {
6578
$this->skip = Constant::STATUS_FALSE;
6679
$constraintList = $this->getConstraintList($tableName, $noConstraintFields);
80+
6781
$selectConstrain = select(
6882
label: __('Lang::messages.constraint.question.constraint_selection'),
6983
options: $constraintList,
@@ -83,6 +97,35 @@ public function handle(): int|string
8397
return self::SUCCESS;
8498
}
8599

100+
/**
101+
* PostgreSQL Constraint
102+
* @return int
103+
*/
104+
public function pgsql()
105+
{
106+
$tableList = collect($this->getTableList())
107+
->diff(config('audit.skip_tables'))->values()->toArray();
108+
$tableName = select(
109+
label: __('Lang::messages.constraint.question.table_selection'),
110+
options: $tableList,
111+
default: reset($tableList)
112+
);
113+
114+
$this->display($tableName);
115+
116+
if ($tableName) {
117+
118+
$continue = Constant::STATUS_TRUE;
119+
120+
do {
121+
if ($this->getNoConstraintFields($tableName)) {
122+
$continue = Constant::STATUS_FALSE;
123+
};
124+
} while ($continue === Constant::STATUS_TRUE);
125+
}
126+
127+
return self::SUCCESS;
128+
}
86129

87130
/**
88131
* Display error messages

src/Commands/DBStandardCommand.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ public function tableReport(string $tableName, string $connection)
9191

9292
if (!$tableStatus) {
9393
return render(view('DBAuditor::error_message', ['message' => 'No Table Found']));
94-
} else {
95-
render(view('DBAuditor::'.$connection.'.table_standard', ['tableStatus' => $tableStatus]));
9694
}
95+
96+
render(view('DBAuditor::'.$connection.'.table_standard', ['tableStatus' => $tableStatus]));
9797
}
9898
}

src/Commands/DBSummaryCommand.php

+17-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public function handle()
3333
{
3434
return match (connection_driver()) {
3535
'sqlite' => $this->sqlite(),
36+
'pgsql' => $this->pgsql(),
3637
default => $this->mysql(),
3738
};
3839
}
@@ -72,7 +73,7 @@ public function sqlite(): int
7273
public function mysql(): int
7374
{
7475
$this->table(
75-
['Database Name', 'Size', 'Table Count', 'Engin', 'Character Set'],
76+
['Database Name', 'Size (MB)', 'Table Count', 'DB Version', 'Character Set'],
7677
[[
7778
database_name(),
7879
$this->getDatabaseSize(),
@@ -84,4 +85,19 @@ public function mysql(): int
8485

8586
return self::SUCCESS;
8687
}
88+
89+
public function pgsql(): int
90+
{
91+
$this->table(
92+
['Database Name', 'Size (MB)', 'Table Count', 'Character Set'],
93+
[[
94+
database_name(),
95+
$this->getDatabaseSize(),
96+
count($this->getTableList()),
97+
$this->getCharacterSetName(),
98+
]]
99+
);
100+
101+
return self::SUCCESS;
102+
}
87103
}

src/Config/audit.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@
88
'sqlite_sequence',
99
'migrations',
1010
'migrations_history',
11-
'sessions',
1211
'password_resets',
1312
'failed_jobs',
1413
'jobs',
1514
'queue_job',
16-
'queue_failed_jobs',
15+
'queue_failed_jobs'
1716
],
1817
'mysql_commands' => [
1918
Constant::STANDARD_COMMAND,
@@ -26,5 +25,11 @@
2625
Constant::CONSTRAINT_COMMAND,
2726
Constant::SUMMARY_COMMAND,
2827
Constant::TRACK_COMMAND,
28+
],
29+
'pgsql_commands' => [
30+
Constant::STANDARD_COMMAND,
31+
Constant::CONSTRAINT_COMMAND,
32+
Constant::SUMMARY_COMMAND,
33+
Constant::TRACK_COMMAND,
2934
]
3035
];

src/Constants/Constant.php

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ class Constant
3636

3737
public const SQLITE_DB = 'sqlite';
3838

39+
public const POSTGRESQL_DB = 'pgsql';
40+
3941
public const UNIQUE_RULES = 'unique';
4042

4143
public const INDEX_FILE_NAME = 'update_table_index.php';

src/Lang/en/messages.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
'constraint' => [
1919
'question' => [
2020
'table_selection' => 'Which table would you like to audit?',
21-
'continue' => 'Do you want add more constraint?',
21+
'add_constraint' => 'Do you want add more constraint?',
2222
'constraint_selection' => 'Please select a constraint which you want to add.',
2323
'field_selection' => 'Please select a field to add constraint',
2424
'foreign_table' => 'Please add foreign table name.',

src/Queries/DatabaseCharacterSetClass.php

+8
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public function __invoke(): string
1919
{
2020
return match ($this->driver) {
2121
'sqlite' => $this->sqlite(),
22+
'pgsql' => $this->pgsql(),
2223
default => $this->mysql(),
2324
};
2425
}
@@ -45,4 +46,11 @@ public function select($query): array
4546
{
4647
return DB::select($query);
4748
}
49+
50+
public function pgsql(): string
51+
{
52+
$result = $this->select("SELECT pg_encoding_to_char(encoding) AS character_set FROM pg_database WHERE datname = current_database();");
53+
54+
return reset($result)?->character_set ?? Constant::DASH;
55+
}
4856
}

src/Queries/DatabaseConstraintClass.php

+81-7
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
use Illuminate\Support\Facades\DB;
66
use Vcian\LaravelDBAuditor\Constants\Constant;
77
use Vcian\LaravelDBAuditor\Traits\Audit;
8+
use Vcian\LaravelDBAuditor\Traits\DBConstraint;
89

910
class DatabaseConstraintClass
1011
{
1112
use Audit;
1213
protected string $driver, $database;
1314

14-
public function __construct(protected string $table)
15+
public function __construct(protected string $table, protected string $fields = '')
1516
{
1617
$this->driver = connection_driver();
1718
$this->database = database_name();
@@ -21,10 +22,20 @@ public function __invoke(): array
2122
{
2223
return match ($this->driver) {
2324
'sqlite' => $this->sqlite(),
25+
'pgsql' => $this->pgsql(),
2426
default => $this->mysql(),
2527
};
2628
}
2729

30+
/**
31+
* @param $query
32+
* @return array
33+
*/
34+
public function select($query): array
35+
{
36+
return DB::select($query);
37+
}
38+
2839
/**
2940
* Sqlite query.
3041
*
@@ -62,12 +73,75 @@ public function mysql(): array
6273
];
6374
}
6475

65-
/**
66-
* @param $query
67-
* @return array
68-
*/
69-
public function select($query): array
76+
77+
public function pgsql(): array
7078
{
71-
return DB::select($query);
79+
return [
80+
'primary' => $this->getPgsqlConstraintField($this->table, Constant::CONSTRAINT_PRIMARY_KEY),
81+
'unique' => $this->getPgsqlConstraintField($this->table, Constant::CONSTRAINT_UNIQUE_KEY),
82+
'foreign' => $this->getPgsqlConstraintField($this->table, Constant::CONSTRAINT_FOREIGN_KEY),
83+
'index' => $this->getPgsqlConstraintField($this->table, Constant::CONSTRAINT_INDEX_KEY),
84+
];
85+
}
86+
87+
public function getPgsqlConstraintField(string $tableName, string $input): array
88+
{
89+
if ($input === Constant::CONSTRAINT_FOREIGN_KEY){
90+
return collect(DB::select("SELECT
91+
conname AS constraint_name,
92+
a.attname AS column_name,
93+
confrelid::regclass AS foreign_table,
94+
af.attname AS foreign_column
95+
FROM
96+
pg_constraint AS c
97+
JOIN
98+
pg_class AS t ON c.conrelid = t.oid
99+
JOIN
100+
pg_attribute AS a ON a.attnum = ANY(c.conkey) AND a.attrelid = t.oid
101+
JOIN
102+
pg_attribute AS af ON af.attnum = ANY(c.confkey) AND af.attrelid = c.confrelid
103+
WHERE
104+
t.relname = ?
105+
AND c.contype = 'f'", [$tableName]))
106+
->flatten()
107+
->toArray();
108+
}
109+
110+
if ($input === Constant::CONSTRAINT_INDEX_KEY) {
111+
return collect(DB::select("SELECT
112+
i.relname AS index_name,
113+
a.attname AS column_name
114+
FROM
115+
pg_class AS t
116+
JOIN
117+
pg_index AS ix ON t.oid = ix.indrelid
118+
JOIN
119+
pg_class AS i ON i.oid = ix.indexrelid
120+
JOIN
121+
pg_attribute AS a ON a.attnum = ANY(ix.indkey) AND a.attrelid = t.oid
122+
WHERE t.relname = ?",[$tableName]))
123+
->select('column_name')
124+
->flatten()
125+
->toArray();
126+
} else {
127+
$conType = Constant::CONSTRAINT_PRIMARY_KEY ? 'p' : 'u';
128+
return collect(DB::select("
129+
SELECT
130+
conname AS constraint_name,
131+
a.attname AS column_name
132+
FROM
133+
pg_constraint AS c
134+
JOIN
135+
pg_class AS t ON c.conrelid = t.oid
136+
JOIN
137+
pg_attribute AS a ON a.attnum = ANY(c.conkey) AND a.attrelid = t.oid
138+
WHERE
139+
t.relname = ?
140+
AND c.contype = ?",[$tableName,$conType]))
141+
->select('column_name')
142+
->flatten()
143+
->toArray();
144+
}
145+
72146
}
73147
}

0 commit comments

Comments
 (0)