mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-04 23:14:05 +00:00
Dump DB statement
This commit is contained in:
parent
796a251b7d
commit
9aee480a80
1 changed files with 14 additions and 2 deletions
|
|
@ -19,9 +19,7 @@ class CreateRLSPoliciesForTenantTables extends Command
|
||||||
|
|
||||||
public function handle(): int
|
public function handle(): int
|
||||||
{
|
{
|
||||||
dump('Current RLS tables', DB::select('SELECT * FROM pg_policies'));
|
|
||||||
tenancy()->getModels()->each(fn (Model $model) => $this->useRlsOnModel($model));
|
tenancy()->getModels()->each(fn (Model $model) => $this->useRlsOnModel($model));
|
||||||
dump('RLS tables created', DB::select('SELECT * FROM pg_policies'));
|
|
||||||
|
|
||||||
return Command::SUCCESS;
|
return Command::SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
@ -50,6 +48,18 @@ class CreateRLSPoliciesForTenantTables extends Command
|
||||||
$parentKey = $model->$parentName()->getForeignKeyName();
|
$parentKey = $model->$parentName()->getForeignKeyName();
|
||||||
$parentTable = $model->$parentName()->make()->getTable();
|
$parentTable = $model->$parentName()->make()->getTable();
|
||||||
|
|
||||||
|
dump("CREATE POLICY {$table}_rls_policy ON {$table} USING (
|
||||||
|
{$parentKey} IN (
|
||||||
|
SELECT id
|
||||||
|
FROM {$parentTable}
|
||||||
|
WHERE ({$tenantKey} = (
|
||||||
|
SELECT {$tenantKey}
|
||||||
|
FROM {$parentTable}
|
||||||
|
WHERE id = {$parentKey}
|
||||||
|
))
|
||||||
|
)
|
||||||
|
)");
|
||||||
|
|
||||||
DB::statement("CREATE POLICY {$table}_rls_policy ON {$table} USING (
|
DB::statement("CREATE POLICY {$table}_rls_policy ON {$table} USING (
|
||||||
{$parentKey} IN (
|
{$parentKey} IN (
|
||||||
SELECT id
|
SELECT id
|
||||||
|
|
@ -62,6 +72,8 @@ class CreateRLSPoliciesForTenantTables extends Command
|
||||||
)
|
)
|
||||||
)");
|
)");
|
||||||
|
|
||||||
|
dump('statement completed');
|
||||||
|
|
||||||
$this->enableRls($table);
|
$this->enableRls($table);
|
||||||
|
|
||||||
$this->components->info("Created RLS policy for table '$table'");
|
$this->components->info("Created RLS policy for table '$table'");
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue