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

implement fix, improve test

This commit is contained in:
Samuel Štancl 2025-11-08 00:42:01 +01:00
parent ffd3678e64
commit ccb52449f4
No known key found for this signature in database
GPG key ID: BA146259A1E16C57
2 changed files with 9 additions and 10 deletions

View file

@ -26,7 +26,7 @@ trait ManagesRLSPolicies
$policies = static::getRLSPolicies($table); $policies = static::getRLSPolicies($table);
foreach ($policies as $policy) { foreach ($policies as $policy) {
DB::statement('DROP POLICY ? ON ?', [$policy, $table]); DB::statement("DROP POLICY {$policy} ON {$table}");
} }
return count($policies); return count($policies);

View file

@ -190,21 +190,20 @@ test('rls command recreates policies if the force option is passed', function (s
TraitRLSManager::class, TraitRLSManager::class,
]); ]);
test('dropRLSPolicies removes postgres policies', function () { test('dropRLSPolicies only drops RLS policies', function () {
// create dummy policy with characters that require quoting to ensure identifier handling works DB::statement('CREATE POLICY "comments_dummy_rls_policy" ON comments USING (true)');
DB::statement('CREATE POLICY "comments_rls_policy_manual" ON comments USING (true)'); DB::statement('CREATE POLICY "comments_foo_policy" ON comments USING (true)'); // non-RLS policy
$policyCount = fn () => count(DB::select( $policyCount = fn () => count(DB::select("SELECT policyname FROM pg_policies WHERE tablename = 'comments'"));
'SELECT policyname FROM pg_policies WHERE tablename = ? AND policyname = ?',
['comments', 'comments_rls_policy_manual']
));
expect($policyCount())->toBe(1); expect($policyCount())->toBe(2);
$removed = Tenancy::dropRLSPolicies('comments'); $removed = Tenancy::dropRLSPolicies('comments');
expect($removed)->toBe(1); expect($removed)->toBe(1);
expect($policyCount())->toBe(0);
// Only the non-RLS policy remains
expect($policyCount())->toBe(1);
}); });
test('queries will stop working when the tenant session variable is not set', function(string $manager, bool $forceRls) { test('queries will stop working when the tenant session variable is not set', function(string $manager, bool $forceRls) {