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

Swap try/catch for if statements

This commit is contained in:
lukinovec 2023-04-24 13:20:02 +02:00
parent 2d75f185b8
commit eae4d378f7
2 changed files with 6 additions and 9 deletions

View file

@ -38,10 +38,8 @@ class CreatePostgresUserForTenant implements ShouldQueue
$name = $this->tenant->getTenantKey();
$password = $this->tenant->database()->getPassword() ?? 'password';
try {
if (count(DB::select("SELECT usename FROM pg_user WHERE usename = '$name';")) > 0) {
DB::statement("CREATE USER \"$name\" LOGIN PASSWORD '$password';");
} catch (QueryException $exception) {
// Skip creating Postgres user if it already exists
}
}
}

View file

@ -35,14 +35,13 @@ class DeleteTenantsPostgresUser implements ShouldQueue
*/
public function handle()
{
$tenantKey = $this->tenant->getTenantKey();
$name = $this->tenant->getTenantKey();
// Revoke all permissions of a Postgres user before dropping it
try {
DB::statement("DROP OWNED BY \"{$tenantKey}\";");
DB::statement("DROP USER \"{$tenantKey}\";");
} catch (QueryException $exception) {
// 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) {
DB::statement("DROP OWNED BY \"{$name}\";");
DB::statement("DROP USER \"{$name}\";");
}
}
}