From 9aee480a801dd0598b7df0ab5d6e8e70ad23768b Mon Sep 17 00:00:00 2001 From: lukinovec Date: Fri, 16 Jun 2023 09:33:55 +0200 Subject: [PATCH] Dump DB statement --- .../CreateRLSPoliciesForTenantTables.php | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Commands/CreateRLSPoliciesForTenantTables.php b/src/Commands/CreateRLSPoliciesForTenantTables.php index 7d346521..6bd10c1c 100644 --- a/src/Commands/CreateRLSPoliciesForTenantTables.php +++ b/src/Commands/CreateRLSPoliciesForTenantTables.php @@ -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'");