Skip to content

Commit

Permalink
Merge pull request #63 from ruchit288/feature/postgres
Browse files Browse the repository at this point in the history
Integrate postgresql.
  • Loading branch information
ruchit288 authored Aug 2, 2024
2 parents 8c3c3f7 + 503e35f commit 27f2d79
Show file tree
Hide file tree
Showing 27 changed files with 864 additions and 183 deletions.
13 changes: 6 additions & 7 deletions src/Commands/DBAuditCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace Vcian\LaravelDBAuditor\Commands;

use Illuminate\Console\Command;
use League\Flysystem\Config;
use Vcian\LaravelDBAuditor\Constants\Constant;

use function Laravel\Prompts\search;
use function Laravel\Prompts\select;

class DBAuditCommand extends Command
Expand All @@ -29,14 +29,13 @@ class DBAuditCommand extends Command
*/
public function handle(): void
{
$commands = match (connection_driver()) {
Constant::SQLITE_DB => config('audit.sqlite_commands'),
Constant::MYSQL_DB => config('audit.mysql_commands'),
};

$commandSelect = select(
label: 'Please Select feature which would you like to do',
options: $commands,
options: match (connection_driver()) {
Constant::SQLITE_DB => config('audit.sqlite_commands'),
Constant::MYSQL_DB => config('audit.mysql_commands'),
Constant::POSTGRESQL_DB => config('audit.pgsql_commands'),
},
default: Constant::SUMMARY_COMMAND
);

Expand Down
53 changes: 48 additions & 5 deletions src/Commands/DBConstraintCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Vcian\LaravelDBAuditor\Constants\Constant;
use Vcian\LaravelDBAuditor\Traits\Audit;

use Vcian\LaravelDBAuditor\Traits\DBConstraint;
use Vcian\LaravelDBAuditor\Traits\DisplayTable;
use function Laravel\Prompts\confirm;
use function Laravel\Prompts\select;
Expand All @@ -14,7 +15,7 @@

class DBConstraintCommand extends Command
{
use DisplayTable, Audit;
use Audit, DisplayTable, DBConstraint;
/**
* @var bool
*/
Expand All @@ -40,8 +41,21 @@ class DBConstraintCommand extends Command
*/
public function handle(): int|string
{
$this->connection = connection_driver();
$tableList = $this->getTableList();
return match (connection_driver()) {
'sqlite' => $this->sqlite(),
'pgsql' => $this->pgsql(),
default => $this->mysql(),
};
}

/**
* MySQL Constraint
* @return int
*/
public function mysql()
{
$tableList = collect($this->getTableList())
->diff(config('audit.skip_tables'))->values()->toArray();

$tableName = select(
label: __('Lang::messages.constraint.question.table_selection'),
Expand All @@ -60,10 +74,10 @@ public function handle(): int|string
if (empty($noConstraintFields)) {
$continue = Constant::STATUS_FALSE;
} else {
if (confirm(label: __('Lang::messages.constraint.question.continue'))) {

if (confirm(label: __('Lang::messages.constraint.question.add_constraint'))) {
$this->skip = Constant::STATUS_FALSE;
$constraintList = $this->getConstraintList($tableName, $noConstraintFields);

$selectConstrain = select(
label: __('Lang::messages.constraint.question.constraint_selection'),
options: $constraintList,
Expand All @@ -83,6 +97,35 @@ public function handle(): int|string
return self::SUCCESS;
}

/**
* PostgreSQL Constraint
* @return int
*/
public function pgsql()
{
$tableList = collect($this->getTableList())
->diff(config('audit.skip_tables'))->values()->toArray();
$tableName = select(
label: __('Lang::messages.constraint.question.table_selection'),
options: $tableList,
default: reset($tableList)
);

$this->display($tableName);

if ($tableName) {

$continue = Constant::STATUS_TRUE;

do {
if ($this->getNoConstraintFields($tableName)) {
$continue = Constant::STATUS_FALSE;
};
} while ($continue === Constant::STATUS_TRUE);
}

return self::SUCCESS;
}

/**
* Display error messages
Expand Down
4 changes: 2 additions & 2 deletions src/Commands/DBStandardCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ public function tableReport(string $tableName, string $connection)

if (!$tableStatus) {
return render(view('DBAuditor::error_message', ['message' => 'No Table Found']));
} else {
render(view('DBAuditor::'.$connection.'.table_standard', ['tableStatus' => $tableStatus]));
}

render(view('DBAuditor::'.$connection.'.table_standard', ['tableStatus' => $tableStatus]));
}
}
18 changes: 17 additions & 1 deletion src/Commands/DBSummaryCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function handle()
{
return match (connection_driver()) {
'sqlite' => $this->sqlite(),
'pgsql' => $this->pgsql(),
default => $this->mysql(),
};
}
Expand Down Expand Up @@ -72,7 +73,7 @@ public function sqlite(): int
public function mysql(): int
{
$this->table(
['Database Name', 'Size', 'Table Count', 'Engin', 'Character Set'],
['Database Name', 'Size (MB)', 'Table Count', 'DB Version', 'Character Set'],
[[
database_name(),
$this->getDatabaseSize(),
Expand All @@ -84,4 +85,19 @@ public function mysql(): int

return self::SUCCESS;
}

public function pgsql(): int
{
$this->table(
['Database Name', 'Size (MB)', 'Table Count', 'Character Set'],
[[
database_name(),
$this->getDatabaseSize(),
count($this->getTableList()),
$this->getCharacterSetName(),
]]
);

return self::SUCCESS;
}
}
9 changes: 7 additions & 2 deletions src/Config/audit.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@
'sqlite_sequence',
'migrations',
'migrations_history',
'sessions',
'password_resets',
'failed_jobs',
'jobs',
'queue_job',
'queue_failed_jobs',
'queue_failed_jobs'
],
'mysql_commands' => [
Constant::STANDARD_COMMAND,
Expand All @@ -26,5 +25,11 @@
Constant::CONSTRAINT_COMMAND,
Constant::SUMMARY_COMMAND,
Constant::TRACK_COMMAND,
],
'pgsql_commands' => [
Constant::STANDARD_COMMAND,
Constant::CONSTRAINT_COMMAND,
Constant::SUMMARY_COMMAND,
Constant::TRACK_COMMAND,
]
];
2 changes: 2 additions & 0 deletions src/Constants/Constant.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class Constant

public const SQLITE_DB = 'sqlite';

public const POSTGRESQL_DB = 'pgsql';

public const UNIQUE_RULES = 'unique';

public const INDEX_FILE_NAME = 'update_table_index.php';
Expand Down
2 changes: 1 addition & 1 deletion src/Lang/en/messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
'constraint' => [
'question' => [
'table_selection' => 'Which table would you like to audit?',
'continue' => 'Do you want add more constraint?',
'add_constraint' => 'Do you want add more constraint?',
'constraint_selection' => 'Please select a constraint which you want to add.',
'field_selection' => 'Please select a field to add constraint',
'foreign_table' => 'Please add foreign table name.',
Expand Down
8 changes: 8 additions & 0 deletions src/Queries/DatabaseCharacterSetClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function __invoke(): string
{
return match ($this->driver) {
'sqlite' => $this->sqlite(),
'pgsql' => $this->pgsql(),
default => $this->mysql(),
};
}
Expand All @@ -45,4 +46,11 @@ public function select($query): array
{
return DB::select($query);
}

public function pgsql(): string
{
$result = $this->select("SELECT pg_encoding_to_char(encoding) AS character_set FROM pg_database WHERE datname = current_database();");

return reset($result)?->character_set ?? Constant::DASH;
}
}
88 changes: 81 additions & 7 deletions src/Queries/DatabaseConstraintClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
use Illuminate\Support\Facades\DB;
use Vcian\LaravelDBAuditor\Constants\Constant;
use Vcian\LaravelDBAuditor\Traits\Audit;
use Vcian\LaravelDBAuditor\Traits\DBConstraint;

class DatabaseConstraintClass
{
use Audit;
protected string $driver, $database;

public function __construct(protected string $table)
public function __construct(protected string $table, protected string $fields = '')
{
$this->driver = connection_driver();
$this->database = database_name();
Expand All @@ -21,10 +22,20 @@ public function __invoke(): array
{
return match ($this->driver) {
'sqlite' => $this->sqlite(),
'pgsql' => $this->pgsql(),
default => $this->mysql(),
};
}

/**
* @param $query
* @return array
*/
public function select($query): array
{
return DB::select($query);
}

/**
* Sqlite query.
*
Expand Down Expand Up @@ -62,12 +73,75 @@ public function mysql(): array
];
}

/**
* @param $query
* @return array
*/
public function select($query): array

public function pgsql(): array
{
return DB::select($query);
return [
'primary' => $this->getPgsqlConstraintField($this->table, Constant::CONSTRAINT_PRIMARY_KEY),
'unique' => $this->getPgsqlConstraintField($this->table, Constant::CONSTRAINT_UNIQUE_KEY),
'foreign' => $this->getPgsqlConstraintField($this->table, Constant::CONSTRAINT_FOREIGN_KEY),
'index' => $this->getPgsqlConstraintField($this->table, Constant::CONSTRAINT_INDEX_KEY),
];
}

public function getPgsqlConstraintField(string $tableName, string $input): array
{
if ($input === Constant::CONSTRAINT_FOREIGN_KEY){
return collect(DB::select("SELECT
conname AS constraint_name,
a.attname AS column_name,
confrelid::regclass AS foreign_table,
af.attname AS foreign_column
FROM
pg_constraint AS c
JOIN
pg_class AS t ON c.conrelid = t.oid
JOIN
pg_attribute AS a ON a.attnum = ANY(c.conkey) AND a.attrelid = t.oid
JOIN
pg_attribute AS af ON af.attnum = ANY(c.confkey) AND af.attrelid = c.confrelid
WHERE
t.relname = ?
AND c.contype = 'f'", [$tableName]))
->flatten()
->toArray();
}

if ($input === Constant::CONSTRAINT_INDEX_KEY) {
return collect(DB::select("SELECT
i.relname AS index_name,
a.attname AS column_name
FROM
pg_class AS t
JOIN
pg_index AS ix ON t.oid = ix.indrelid
JOIN
pg_class AS i ON i.oid = ix.indexrelid
JOIN
pg_attribute AS a ON a.attnum = ANY(ix.indkey) AND a.attrelid = t.oid
WHERE t.relname = ?",[$tableName]))
->select('column_name')
->flatten()
->toArray();
} else {
$conType = Constant::CONSTRAINT_PRIMARY_KEY ? 'p' : 'u';
return collect(DB::select("
SELECT
conname AS constraint_name,
a.attname AS column_name
FROM
pg_constraint AS c
JOIN
pg_class AS t ON c.conrelid = t.oid
JOIN
pg_attribute AS a ON a.attnum = ANY(c.conkey) AND a.attrelid = t.oid
WHERE
t.relname = ?
AND c.contype = ?",[$tableName,$conType]))
->select('column_name')
->flatten()
->toArray();
}

}
}
Loading

0 comments on commit 27f2d79

Please sign in to comment.