mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 06:04:03 +00:00
Delete single-call DB transactions
This commit is contained in:
parent
8ccc27e8c3
commit
3ef1c38414
3 changed files with 6 additions and 6 deletions
|
|
@ -31,7 +31,7 @@ class CreateRLSPoliciesForTenantTables extends Command
|
||||||
$table = $model->getTable();
|
$table = $model->getTable();
|
||||||
$tenantKey = tenancy()->tenantKeyColumn();
|
$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)) {
|
if (! Schema::hasColumn($table, $tenantKey)) {
|
||||||
// Table is not directly related to tenant
|
// 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.");
|
$this->components->info("Table '$table' is not related to tenant. Make sure $modelName uses the BelongsToPrimaryModel trait.");
|
||||||
}
|
}
|
||||||
} else {
|
} 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);
|
$this->enableRls($table);
|
||||||
|
|
||||||
|
|
@ -62,7 +62,7 @@ class CreateRLSPoliciesForTenantTables extends Command
|
||||||
$parentModel = $model->$parentName()->make();
|
$parentModel = $model->$parentName()->make();
|
||||||
$parentTable = str($parentModel->getTable())->toString();
|
$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 (
|
{$parentKey} IN (
|
||||||
SELECT id
|
SELECT id
|
||||||
FROM {$parentTable}
|
FROM {$parentTable}
|
||||||
|
|
@ -72,7 +72,7 @@ class CreateRLSPoliciesForTenantTables extends Command
|
||||||
WHERE id = {$parentKey}
|
WHERE id = {$parentKey}
|
||||||
))
|
))
|
||||||
)
|
)
|
||||||
)"));
|
)");
|
||||||
|
|
||||||
$this->enableRls($table);
|
$this->enableRls($table);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -39,7 +39,7 @@ class CreatePostgresUserForTenant implements ShouldQueue
|
||||||
|
|
||||||
// Create the user only if it doesn't already exist
|
// Create the user only if it doesn't already exist
|
||||||
if (! count(DB::select("SELECT usename FROM pg_user WHERE usename = '$name';")) > 0) {
|
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);
|
$this->grantPermissions((string) $name);
|
||||||
|
|
|
||||||
|
|
@ -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
|
// Drop all existing policies to check if the command creates policies for multiple tables
|
||||||
foreach ($getRlsPolicies() as $policy) {
|
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);
|
expect($getRlsPolicies())->toHaveCount(0);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue