1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-04 19:24:02 +00:00

Dump DB statement

This commit is contained in:
lukinovec 2023-06-16 09:33:55 +02:00
parent 796a251b7d
commit 9aee480a80

View file

@ -19,9 +19,7 @@ class CreateRLSPoliciesForTenantTables extends Command
public function handle(): int
{
dump('Current RLS tables', DB::select('SELECT * FROM pg_policies'));
tenancy()->getModels()->each(fn (Model $model) => $this->useRlsOnModel($model));
dump('RLS tables created', DB::select('SELECT * FROM pg_policies'));
return Command::SUCCESS;
}
@ -50,6 +48,18 @@ class CreateRLSPoliciesForTenantTables extends Command
$parentKey = $model->$parentName()->getForeignKeyName();
$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 (
{$parentKey} IN (
SELECT id
@ -62,6 +72,8 @@ class CreateRLSPoliciesForTenantTables extends Command
)
)");
dump('statement completed');
$this->enableRls($table);
$this->components->info("Created RLS policy for table '$table'");