1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 08:44:02 +00:00

add $ignoreExisting static property to CreateDatabase

This commit is contained in:
Samuel Štancl 2024-03-27 21:44:30 +01:00
parent fdd401fc8f
commit bf1ba69fe3

View file

@ -12,6 +12,8 @@ use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Stancl\Tenancy\Database\Contracts\TenantWithDatabase;
use Stancl\Tenancy\Database\DatabaseManager;
use Stancl\Tenancy\Database\Exceptions\TenantDatabaseAlreadyExistsException;
use Stancl\Tenancy\Database\Exceptions\TenantDatabaseUserAlreadyExistsException;
use Stancl\Tenancy\Events\CreatingDatabase;
use Stancl\Tenancy\Events\DatabaseCreated;
@ -19,6 +21,8 @@ class CreateDatabase implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public static bool $ignoreExisting = false;
public function __construct(
protected TenantWithDatabase&Model $tenant,
) {
@ -34,10 +38,17 @@ class CreateDatabase implements ShouldQueue
}
$this->tenant->database()->makeCredentials();
$databaseManager->ensureTenantCanBeCreated($this->tenant);
$this->tenant->database()->manager()->createDatabase($this->tenant);
event(new DatabaseCreated($this->tenant));
try {
$databaseManager->ensureTenantCanBeCreated($this->tenant);
$this->tenant->database()->manager()->createDatabase($this->tenant);
event(new DatabaseCreated($this->tenant));
} catch (TenantDatabaseAlreadyExistsException | TenantDatabaseUserAlreadyExistsException $e) {
if (! static::$ignoreExisting) {
throw $e;
}
}
return true;
}