From 254b1b3f84dda2a2042d4974089b1fe2d053871d Mon Sep 17 00:00:00 2001 From: Babis Papakonstantinou Date: Sat, 15 May 2021 15:02:51 +0300 Subject: [PATCH] Fix issue 515: Cannot create a new tenant with a different different db host --- src/Database/DatabaseManager.php | 17 +++++++++++++++++ src/DatabaseConfig.php | 14 ++++++++++++++ src/Jobs/CreateDatabase.php | 6 ++++++ src/Jobs/DeleteDatabase.php | 9 ++++++++- 4 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/Database/DatabaseManager.php b/src/Database/DatabaseManager.php index dd30f443..5e59b0b4 100644 --- a/src/Database/DatabaseManager.php +++ b/src/Database/DatabaseManager.php @@ -72,6 +72,23 @@ class DatabaseManager $this->app['config']['database.connections.tenant'] = $tenant->database()->connection(); } + /** + * Create the tenant's host database connection. + */ + public function createHostConnection(TenantWithDatabase $tenant) + { + $this->originalTemplate = $this->app['config']["database.connections.{$tenant->database()->getTemplateConnectionName()}"]; + $this->app['config']["database.connections.{$tenant->database()->getTemplateConnectionName()}"] = $tenant->database()->hostConnection(); + } + + /** + * Reset the tenant database connection. + */ + public function resetTenantConnection(TenantWithDatabase $tenant) + { + $this->app['config']["database.connections.{$tenant->database()->getTemplateConnectionName()}"] = $this->originalTemplate; + } + /** * Check if a tenant can be created. * diff --git a/src/DatabaseConfig.php b/src/DatabaseConfig.php index c8280632..c54aaebd 100644 --- a/src/DatabaseConfig.php +++ b/src/DatabaseConfig.php @@ -117,6 +117,20 @@ class DatabaseConfig ); } + /** + * Tenant host's database connection config. Used for + * creating and deleting the tenant database. + */ + public function hostConnection(): array + { + $template = $this->getTemplateConnectionName(); + $templateConnection = config("database.connections.{$template}"); + + return $this->manager()->makeConnectionConfig( + array_merge($templateConnection, $this->tenantConfig()), $templateConnection['database'] + ); + } + /** * Additional config for the database connection, specific to this tenant. */ diff --git a/src/Jobs/CreateDatabase.php b/src/Jobs/CreateDatabase.php index 3a74534d..3bb589a1 100644 --- a/src/Jobs/CreateDatabase.php +++ b/src/Jobs/CreateDatabase.php @@ -29,6 +29,9 @@ class CreateDatabase implements ShouldQueue public function handle(DatabaseManager $databaseManager) { + // update the connection to use the tenant's config + $databaseManager->createHostConnection($this->tenant); + event(new CreatingDatabase($this->tenant)); // Terminate execution of this job & other jobs in the pipeline @@ -41,5 +44,8 @@ class CreateDatabase implements ShouldQueue $this->tenant->database()->manager()->createDatabase($this->tenant); event(new DatabaseCreated($this->tenant)); + + // revert the configuration to the original template + $databaseManager->resetTenantConnection($this->tenant); } } diff --git a/src/Jobs/DeleteDatabase.php b/src/Jobs/DeleteDatabase.php index 589741cf..76650e8d 100644 --- a/src/Jobs/DeleteDatabase.php +++ b/src/Jobs/DeleteDatabase.php @@ -10,6 +10,7 @@ use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Stancl\Tenancy\Contracts\TenantWithDatabase; +use Stancl\Tenancy\Database\DatabaseManager; use Stancl\Tenancy\Events\DatabaseDeleted; use Stancl\Tenancy\Events\DeletingDatabase; @@ -25,12 +26,18 @@ class DeleteDatabase implements ShouldQueue $this->tenant = $tenant; } - public function handle() + public function handle(DatabaseManager $databaseManager) { + // update the connection to use the tenant's config + $databaseManager->createHostConnection($this->tenant); + event(new DeletingDatabase($this->tenant)); $this->tenant->database()->manager()->deleteDatabase($this->tenant); event(new DatabaseDeleted($this->tenant)); + + // revert the configuration to the original template + $databaseManager->resetTenantConnection($this->tenant); } }