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

Delete single-call DB transactions

This commit is contained in:
lukinovec 2023-05-31 07:38:43 +02:00
parent 8ccc27e8c3
commit 3ef1c38414
3 changed files with 6 additions and 6 deletions

View file

@ -31,7 +31,7 @@ class CreateRLSPoliciesForTenantTables extends Command
$table = $model->getTable();
$tenantKey = tenancy()->tenantKeyColumn();
DB::transaction(fn () => DB::statement("DROP POLICY IF EXISTS {$table}_rls_policy ON {$table}"));
DB::statement("DROP POLICY IF EXISTS {$table}_rls_policy ON {$table}");
if (! Schema::hasColumn($table, $tenantKey)) {
// Table is not directly related to tenant
@ -43,7 +43,7 @@ class CreateRLSPoliciesForTenantTables extends Command
$this->components->info("Table '$table' is not related to tenant. Make sure $modelName uses the BelongsToPrimaryModel trait.");
}
} else {
DB::transaction(fn () => DB::statement("CREATE POLICY {$table}_rls_policy ON {$table} USING ({$tenantKey}::TEXT = current_user);"));
DB::statement("CREATE POLICY {$table}_rls_policy ON {$table} USING ({$tenantKey}::TEXT = current_user);");
$this->enableRls($table);
@ -62,7 +62,7 @@ class CreateRLSPoliciesForTenantTables extends Command
$parentModel = $model->$parentName()->make();
$parentTable = str($parentModel->getTable())->toString();
DB::transaction(fn () => DB::statement("CREATE POLICY {$table}_rls_policy ON {$table} USING (
DB::statement("CREATE POLICY {$table}_rls_policy ON {$table} USING (
{$parentKey} IN (
SELECT id
FROM {$parentTable}
@ -72,7 +72,7 @@ class CreateRLSPoliciesForTenantTables extends Command
WHERE id = {$parentKey}
))
)
)"));
)");
$this->enableRls($table);
}

View file

@ -39,7 +39,7 @@ class CreatePostgresUserForTenant implements ShouldQueue
// Create the user only if it doesn't already exist
if (! count(DB::select("SELECT usename FROM pg_user WHERE usename = '$name';")) > 0) {
DB::transaction(fn () => DB::statement("CREATE USER \"$name\" LOGIN PASSWORD '$password';"));
DB::statement("CREATE USER \"$name\" LOGIN PASSWORD '$password';");
}
$this->grantPermissions((string) $name);

View file

@ -114,7 +114,7 @@ test('correct rls policies get created', function () {
// Drop all existing policies to check if the command creates policies for multiple tables
foreach ($getRlsPolicies() as $policy) {
DB::transaction(fn () => DB::statement("DROP POLICY IF EXISTS {$policy->policyname} ON {$policy->tablename}"));
DB::statement("DROP POLICY IF EXISTS {$policy->policyname} ON {$policy->tablename}");
}
expect($getRlsPolicies())->toHaveCount(0);