mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 18:44:03 +00:00
[4.x] Fix dropRLSPolicies() (#1413)
`?` parameters are not supported in these statements, so we have to use string interpolation like in other related code. --------- Co-authored-by: Samuel Štancl <samuel@archte.ch>
This commit is contained in:
parent
8bdacf566b
commit
947894fa1d
2 changed files with 18 additions and 1 deletions
|
|
@ -26,7 +26,7 @@ trait ManagesRLSPolicies
|
|||
$policies = static::getRLSPolicies($table);
|
||||
|
||||
foreach ($policies as $policy) {
|
||||
DB::statement('DROP POLICY ? ON ?', [$policy, $table]);
|
||||
DB::statement("DROP POLICY {$policy} ON {$table}");
|
||||
}
|
||||
|
||||
return count($policies);
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ use Stancl\Tenancy\Commands\CreateUserWithRLSPolicies;
|
|||
use Stancl\Tenancy\RLS\PolicyManagers\TableRLSManager;
|
||||
use Stancl\Tenancy\RLS\PolicyManagers\TraitRLSManager;
|
||||
use Stancl\Tenancy\Bootstrappers\PostgresRLSBootstrapper;
|
||||
use Stancl\Tenancy\Tenancy;
|
||||
use function Stancl\Tenancy\Tests\pest;
|
||||
|
||||
beforeEach(function () {
|
||||
|
|
@ -189,6 +190,22 @@ test('rls command recreates policies if the force option is passed', function (s
|
|||
TraitRLSManager::class,
|
||||
]);
|
||||
|
||||
test('dropRLSPolicies only drops RLS policies', function () {
|
||||
DB::statement('CREATE POLICY "comments_dummy_rls_policy" ON comments USING (true)');
|
||||
DB::statement('CREATE POLICY "comments_foo_policy" ON comments USING (true)'); // non-RLS policy
|
||||
|
||||
$policyCount = fn () => count(DB::select("SELECT policyname FROM pg_policies WHERE tablename = 'comments'"));
|
||||
|
||||
expect($policyCount())->toBe(2);
|
||||
|
||||
$removed = Tenancy::dropRLSPolicies('comments');
|
||||
|
||||
expect($removed)->toBe(1);
|
||||
|
||||
// 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) {
|
||||
CreateUserWithRLSPolicies::$forceRls = $forceRls;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue