mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 03:54:04 +00:00
Put DB operations inside transactions
This commit is contained in:
parent
6e2b56237f
commit
8eb9e5f19b
2 changed files with 11 additions and 5 deletions
|
|
@ -39,7 +39,9 @@ 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::statement("CREATE USER \"$name\" LOGIN PASSWORD '$password';");
|
DB::transaction(function () use ($name, $password) {
|
||||||
|
DB::statement("CREATE USER \"$name\" LOGIN PASSWORD '$password';");
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->grantPermissions((string) $name);
|
$this->grantPermissions((string) $name);
|
||||||
|
|
@ -60,8 +62,10 @@ class CreatePostgresUserForTenant implements ShouldQueue
|
||||||
foreach ($rlsModels as $model) {
|
foreach ($rlsModels as $model) {
|
||||||
$table = $model->getTable();
|
$table = $model->getTable();
|
||||||
|
|
||||||
$databaseManager->database()->statement("GRANT ALL ON {$table} TO \"{$userName}\"");
|
$databaseManager->database()->transaction(function () use ($databaseManager, $table, $userName) {
|
||||||
$databaseManager->database()->statement("GRANT USAGE ON ALL SEQUENCES IN SCHEMA public TO \"{$userName}\"");
|
$databaseManager->database()->statement("GRANT ALL ON {$table} TO \"{$userName}\"");
|
||||||
|
$databaseManager->database()->statement("GRANT USAGE ON ALL SEQUENCES IN SCHEMA public TO \"{$userName}\"");
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -39,8 +39,10 @@ class DeleteTenantsPostgresUser implements ShouldQueue
|
||||||
// Revoke all permissions of a Postgres user before dropping it
|
// Revoke all permissions of a Postgres user before dropping it
|
||||||
// Skip dropping permissions if the user doesn't exist
|
// Skip dropping permissions if the user doesn't 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::statement("DROP OWNED BY \"{$name}\";");
|
DB::transaction(function () use ($name) {
|
||||||
DB::statement("DROP USER \"{$name}\";");
|
DB::statement("DROP OWNED BY \"{$name}\";");
|
||||||
|
DB::statement("DROP USER \"{$name}\";");
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue