From bf1ba69fe3f0a4496854ee87199e852e024bc6ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Wed, 27 Mar 2024 21:44:30 +0100 Subject: [PATCH] add $ignoreExisting static property to CreateDatabase --- src/Jobs/CreateDatabase.php | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/Jobs/CreateDatabase.php b/src/Jobs/CreateDatabase.php index dbc4b097..681d3d7a 100644 --- a/src/Jobs/CreateDatabase.php +++ b/src/Jobs/CreateDatabase.php @@ -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; }