mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 20:34:03 +00:00
template_tenant_connection can be array or string
This commit is contained in:
parent
f42f08cb87
commit
2872b36da4
2 changed files with 71 additions and 10 deletions
|
|
@ -87,7 +87,7 @@ class DatabaseConfig
|
||||||
{
|
{
|
||||||
$this->tenant->setInternal('db_name', $this->getName());
|
$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_username', $this->getUsername() ?? (static::$usernameGenerator)($this->tenant));
|
||||||
$this->tenant->setInternal('db_password', $this->getPassword() ?? (static::$passwordGenerator)($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')
|
return $this->getTemplateConnection()['driver'];
|
||||||
?? config('tenancy.database.template_tenant_connection')
|
}
|
||||||
?? config('tenancy.database.central_connection');
|
|
||||||
|
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
|
public function getTenantHostConnectionName(): string
|
||||||
|
|
@ -114,8 +127,7 @@ class DatabaseConfig
|
||||||
*/
|
*/
|
||||||
public function connection(): array
|
public function connection(): array
|
||||||
{
|
{
|
||||||
$template = $this->getTemplateConnectionName();
|
$templateConnection = $this->getTemplateConnection();
|
||||||
$templateConnection = config("database.connections.{$template}");
|
|
||||||
|
|
||||||
return $this->manager()->makeConnectionConfig(
|
return $this->manager()->makeConnectionConfig(
|
||||||
array_merge($templateConnection, $this->tenantConfig()),
|
array_merge($templateConnection, $this->tenantConfig()),
|
||||||
|
|
@ -129,10 +141,9 @@ class DatabaseConfig
|
||||||
public function hostConnection(): array
|
public function hostConnection(): array
|
||||||
{
|
{
|
||||||
$config = $this->tenantConfig();
|
$config = $this->tenantConfig();
|
||||||
$template = $this->getTemplateConnectionName();
|
$templateConnection = $this->getTemplateConnection();
|
||||||
$templateConnection = config("database.connections.{$template}");
|
|
||||||
|
|
||||||
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
|
// 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,
|
// 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
|
// consider creating a new connection and use it as `tenancy_db_connection` tenant config key
|
||||||
|
|
|
||||||
|
|
@ -390,6 +390,56 @@ test('path used by sqlite manager can be customized', function () {
|
||||||
expect(file_exists($customPath . '/' . $name))->toBeTrue();
|
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
|
// Datasets
|
||||||
dataset('database_managers', [
|
dataset('database_managers', [
|
||||||
['mysql', MySQLDatabaseManager::class],
|
['mysql', MySQLDatabaseManager::class],
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue