mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-16 04:04:04 +00:00
create host connection for creating, deleting tenants
This commit is contained in:
parent
abd17f83a1
commit
cd6e989c09
7 changed files with 201 additions and 8 deletions
|
|
@ -100,6 +100,11 @@ class DatabaseConfig
|
|||
?? config('tenancy.database.central_connection');
|
||||
}
|
||||
|
||||
public function getTenantHostConnectionName(): ?string
|
||||
{
|
||||
return config('tenancy.database.tenant_host_connection_name');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tenant's own database connection config.
|
||||
*/
|
||||
|
|
@ -114,6 +119,27 @@ class DatabaseConfig
|
|||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tenant's host database connection config.
|
||||
*/
|
||||
public function hostConnection(): array
|
||||
{
|
||||
$config = $this->tenantConfig();
|
||||
$template = $this->getTemplateConnectionName();
|
||||
$templateConnection = config("database.connections.{$template}");
|
||||
|
||||
if ($this->manager() instanceof Contracts\ManagesDatabaseUsers) {
|
||||
unset($config['username']);
|
||||
unset($config['password']);
|
||||
}
|
||||
|
||||
if (empty($config)) {
|
||||
$config = $templateConnection;
|
||||
}
|
||||
|
||||
return array_replace($templateConnection, $config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Additional config for the database connection, specific to this tenant.
|
||||
*/
|
||||
|
|
@ -158,4 +184,25 @@ class DatabaseConfig
|
|||
|
||||
return $databaseManager;
|
||||
}
|
||||
|
||||
/** Get the TenantDatabaseManager for this tenant's connection. */
|
||||
public function hostManager(): Contracts\TenantDatabaseManager
|
||||
{
|
||||
$tenantHostConnection = $this->getTenantHostConnectionName();
|
||||
config(["database.connections.{$tenantHostConnection}" => $this->hostConnection()]);
|
||||
|
||||
$driver = config("database.connections.{$tenantHostConnection}.driver");
|
||||
$databaseManagers = config('tenancy.database.managers');
|
||||
|
||||
if (! array_key_exists($driver, $databaseManagers)) {
|
||||
throw new Exceptions\DatabaseManagerNotRegisteredException($driver);
|
||||
}
|
||||
|
||||
/** @var Contracts\TenantDatabaseManager $databaseManager */
|
||||
$databaseManager = app($databaseManagers[$driver]);
|
||||
|
||||
$databaseManager->setConnection($tenantHostConnection);
|
||||
|
||||
return $databaseManager;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -88,7 +88,7 @@ class DatabaseManager
|
|||
*/
|
||||
public function ensureTenantCanBeCreated(TenantWithDatabase $tenant): void
|
||||
{
|
||||
$manager = $tenant->database()->manager();
|
||||
$manager = $tenant->database()->hostManager();
|
||||
|
||||
if ($manager->databaseExists($database = $tenant->database()->getName())) {
|
||||
throw new Exceptions\TenantDatabaseAlreadyExistsException($database);
|
||||
|
|
|
|||
|
|
@ -20,6 +20,10 @@ abstract class TenantDatabaseManager implements Contract // todo better naming?
|
|||
throw new NoConnectionSetException(static::class);
|
||||
}
|
||||
|
||||
if (config("database.connections.{$this->getTenantHostConnectionName()}")) {
|
||||
// DB::purge($this->getTenantHostConnectionName());
|
||||
}
|
||||
|
||||
return DB::connection($this->connection);
|
||||
}
|
||||
|
||||
|
|
@ -34,4 +38,9 @@ abstract class TenantDatabaseManager implements Contract // todo better naming?
|
|||
|
||||
return $baseConfig;
|
||||
}
|
||||
|
||||
public function getTenantHostConnectionName(): ?string
|
||||
{
|
||||
return config('tenancy.database.tenant_host_connection_name');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@ class CreateDatabase implements ShouldQueue
|
|||
|
||||
$this->tenant->database()->makeCredentials();
|
||||
$databaseManager->ensureTenantCanBeCreated($this->tenant);
|
||||
$this->tenant->database()->manager()->createDatabase($this->tenant);
|
||||
$this->tenant->database()->hostManager()->createDatabase($this->tenant);
|
||||
|
||||
event(new DatabaseCreated($this->tenant));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ class DeleteDatabase implements ShouldQueue
|
|||
{
|
||||
event(new DeletingDatabase($this->tenant));
|
||||
|
||||
$this->tenant->database()->manager()->deleteDatabase($this->tenant);
|
||||
$this->tenant->database()->hostManager()->deleteDatabase($this->tenant);
|
||||
|
||||
event(new DatabaseDeleted($this->tenant));
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue