From 2872b36da40a040265ed5de0357dbf540ce90de5 Mon Sep 17 00:00:00 2001 From: Abrar Ahmad Date: Thu, 29 Dec 2022 15:04:38 +0500 Subject: [PATCH] `template_tenant_connection` can be array or string --- src/Database/DatabaseConfig.php | 31 ++++++++++++------ tests/TenantDatabaseManagerTest.php | 50 +++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 10 deletions(-) diff --git a/src/Database/DatabaseConfig.php b/src/Database/DatabaseConfig.php index 309d828f..fa97a1cb 100644 --- a/src/Database/DatabaseConfig.php +++ b/src/Database/DatabaseConfig.php @@ -87,7 +87,7 @@ class DatabaseConfig { $this->tenant->setInternal('db_name', $this->getName()); - if ($this->connectionDriverManager($this->getTemplateConnectionName()) instanceof Contracts\ManagesDatabaseUsers) { + if ($this->connectionDriverManager($this->getTemplateConnectionDriver()) instanceof Contracts\ManagesDatabaseUsers) { $this->tenant->setInternal('db_username', $this->getUsername() ?? (static::$usernameGenerator)($this->tenant)); $this->tenant->setInternal('db_password', $this->getPassword() ?? (static::$passwordGenerator)($this->tenant)); } @@ -97,11 +97,24 @@ class DatabaseConfig } } - public function getTemplateConnectionName(): string + public function getTemplateConnectionDriver(): string { - return $this->tenant->getInternal('db_connection') - ?? config('tenancy.database.template_tenant_connection') - ?? config('tenancy.database.central_connection'); + return $this->getTemplateConnection()['driver']; + } + + public function getTemplateConnection(): array + { + if ($template = $this->tenant->getInternal('db_connection')) { + return config("database.connections.{$template}"); + } + + if ($template = config('tenancy.database.template_tenant_connection')) { + return is_array($template) ? $template : config("database.connections.{$template}"); + } + + $template = config('tenancy.database.central_connection'); + + return config("database.connections.{$template}"); } public function getTenantHostConnectionName(): string @@ -114,8 +127,7 @@ class DatabaseConfig */ public function connection(): array { - $template = $this->getTemplateConnectionName(); - $templateConnection = config("database.connections.{$template}"); + $templateConnection = $this->getTemplateConnection(); return $this->manager()->makeConnectionConfig( array_merge($templateConnection, $this->tenantConfig()), @@ -129,10 +141,9 @@ class DatabaseConfig public function hostConnection(): array { $config = $this->tenantConfig(); - $template = $this->getTemplateConnectionName(); - $templateConnection = config("database.connections.{$template}"); + $templateConnection = $this->getTemplateConnection(); - if ($this->connectionDriverManager($template) instanceof Contracts\ManagesDatabaseUsers) { + if ($this->connectionDriverManager($this->getTemplateConnectionDriver()) instanceof Contracts\ManagesDatabaseUsers) { // We're removing the username and password because user with these credentials is not created yet // If you need to provide username and password when using PermissionControlledMySQLDatabaseManager, // consider creating a new connection and use it as `tenancy_db_connection` tenant config key diff --git a/tests/TenantDatabaseManagerTest.php b/tests/TenantDatabaseManagerTest.php index 19b74e21..3e6c1c87 100644 --- a/tests/TenantDatabaseManagerTest.php +++ b/tests/TenantDatabaseManagerTest.php @@ -390,6 +390,56 @@ test('path used by sqlite manager can be customized', function () { expect(file_exists($customPath . '/' . $name))->toBeTrue(); }); +test('template tenant connection config can be both connection name or connection array', function () { + Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) { + return $event->tenant; + })->toListener()); + + config([ + 'tenancy.database.managers.mysql' => MySQLDatabaseManager::class, + 'tenancy.database.template_tenant_connection' => 'mysql', + ]); + + $name = 'foo' . Str::random(8); + $tenant = Tenant::create([ + 'tenancy_db_name' => $name, + ]); + + /** @var MySQLDatabaseManager $manager */ + $manager = $tenant->database()->manager(); + expect($manager->databaseExists($name))->toBeTrue(); + expect($manager->database()->getConfig('host'))->toBe('mysql'); + + config([ + 'tenancy.database.template_tenant_connection' => [ + 'driver' => 'mysql', + 'url' => null, + 'host' => 'mysql2', + 'port' => '3306', + 'database' => 'main', + 'username' => 'root', + 'password' => 'password', + 'unix_socket' => '', + 'charset' => 'utf8mb4', + 'collation' => 'utf8mb4_unicode_ci', + 'prefix' => '', + 'prefix_indexes' => true, + 'strict' => true, + 'engine' => null, + 'options' => [], + ], + ]); + + $tenant = Tenant::create([ + 'tenancy_db_name' => $name, + ]); + + /** @var MySQLDatabaseManager $manager */ + $manager = $tenant->database()->manager(); + expect($manager->databaseExists($name))->toBeTrue(); + expect($manager->database()->getConfig('host'))->toBe('mysql2'); +})->group('current'); + // Datasets dataset('database_managers', [ ['mysql', MySQLDatabaseManager::class],