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

fix: don't create database if its exists

This commit is contained in:
mohammad mahdi khakdaman 2023-10-07 19:48:32 +03:30 committed by GitHub
parent 85c7465aca
commit ba15a4bfb9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 5 deletions

View file

@ -86,20 +86,22 @@ class DatabaseManager
/** /**
* Check if a tenant can be created. * Check if a tenant can be created.
* *
* @retur bool
* @throws TenantCannotBeCreatedException * @throws TenantCannotBeCreatedException
* @throws DatabaseManagerNotRegisteredException * @throws DatabaseManagerNotRegisteredException
* @throws TenantDatabaseAlreadyExistsException * @throws TenantDatabaseAlreadyExistsException
*/ */
public function ensureTenantCanBeCreated(TenantWithDatabase $tenant): void public function ensureTenantCanBeCreated(TenantWithDatabase $tenant): bool
{ {
$manager = $tenant->database()->manager(); $manager = $tenant->database()->manager();
if ($manager->databaseExists($database = $tenant->database()->getName())) { if ($manager->databaseExists($database = $tenant->database()->getName())) {
throw new TenantDatabaseAlreadyExistsException($database); return false;
} }
if ($manager instanceof ManagesDatabaseUsers && $manager->userExists($username = $tenant->database()->getUsername())) { if ($manager instanceof ManagesDatabaseUsers && $manager->userExists($username = $tenant->database()->getUsername())) {
throw new TenantDatabaseUserAlreadyExistsException($username); throw new TenantDatabaseUserAlreadyExistsException($username);
} }
return true;
} }
} }

View file

@ -37,9 +37,11 @@ class CreateDatabase implements ShouldQueue
} }
$this->tenant->database()->makeCredentials(); $this->tenant->database()->makeCredentials();
$databaseManager->ensureTenantCanBeCreated($this->tenant);
if ($databaseManager->ensureTenantCanBeCreated($this->tenant)) {
$this->tenant->database()->manager()->createDatabase($this->tenant); $this->tenant->database()->manager()->createDatabase($this->tenant);
event(new DatabaseCreated($this->tenant)); event(new DatabaseCreated($this->tenant));
} }
}
} }