mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 04:54:03 +00:00
fix: don't create database if its exists
This commit is contained in:
parent
85c7465aca
commit
ba15a4bfb9
2 changed files with 9 additions and 5 deletions
|
|
@ -86,20 +86,22 @@ class DatabaseManager
|
|||
/**
|
||||
* Check if a tenant can be created.
|
||||
*
|
||||
* @retur bool
|
||||
* @throws TenantCannotBeCreatedException
|
||||
* @throws DatabaseManagerNotRegisteredException
|
||||
* @throws TenantDatabaseAlreadyExistsException
|
||||
*/
|
||||
public function ensureTenantCanBeCreated(TenantWithDatabase $tenant): void
|
||||
public function ensureTenantCanBeCreated(TenantWithDatabase $tenant): bool
|
||||
{
|
||||
$manager = $tenant->database()->manager();
|
||||
|
||||
if ($manager->databaseExists($database = $tenant->database()->getName())) {
|
||||
throw new TenantDatabaseAlreadyExistsException($database);
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($manager instanceof ManagesDatabaseUsers && $manager->userExists($username = $tenant->database()->getUsername())) {
|
||||
throw new TenantDatabaseUserAlreadyExistsException($username);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,9 +37,11 @@ class CreateDatabase implements ShouldQueue
|
|||
}
|
||||
|
||||
$this->tenant->database()->makeCredentials();
|
||||
$databaseManager->ensureTenantCanBeCreated($this->tenant);
|
||||
$this->tenant->database()->manager()->createDatabase($this->tenant);
|
||||
|
||||
if ($databaseManager->ensureTenantCanBeCreated($this->tenant)) {
|
||||
$this->tenant->database()->manager()->createDatabase($this->tenant);
|
||||
|
||||
event(new DatabaseCreated($this->tenant));
|
||||
event(new DatabaseCreated($this->tenant));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue