mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-06 07:34:02 +00:00
Fix issue 515: Cannot create a new tenant with a different different db host
This commit is contained in:
parent
54a33f93a8
commit
254b1b3f84
4 changed files with 45 additions and 1 deletions
|
|
@ -72,6 +72,23 @@ class DatabaseManager
|
||||||
$this->app['config']['database.connections.tenant'] = $tenant->database()->connection();
|
$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.
|
* Check if a tenant can be created.
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -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.
|
* Additional config for the database connection, specific to this tenant.
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,9 @@ class CreateDatabase implements ShouldQueue
|
||||||
|
|
||||||
public function handle(DatabaseManager $databaseManager)
|
public function handle(DatabaseManager $databaseManager)
|
||||||
{
|
{
|
||||||
|
// update the connection to use the tenant's config
|
||||||
|
$databaseManager->createHostConnection($this->tenant);
|
||||||
|
|
||||||
event(new CreatingDatabase($this->tenant));
|
event(new CreatingDatabase($this->tenant));
|
||||||
|
|
||||||
// Terminate execution of this job & other jobs in the pipeline
|
// 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);
|
$this->tenant->database()->manager()->createDatabase($this->tenant);
|
||||||
|
|
||||||
event(new DatabaseCreated($this->tenant));
|
event(new DatabaseCreated($this->tenant));
|
||||||
|
|
||||||
|
// revert the configuration to the original template
|
||||||
|
$databaseManager->resetTenantConnection($this->tenant);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ use Illuminate\Foundation\Bus\Dispatchable;
|
||||||
use Illuminate\Queue\InteractsWithQueue;
|
use Illuminate\Queue\InteractsWithQueue;
|
||||||
use Illuminate\Queue\SerializesModels;
|
use Illuminate\Queue\SerializesModels;
|
||||||
use Stancl\Tenancy\Contracts\TenantWithDatabase;
|
use Stancl\Tenancy\Contracts\TenantWithDatabase;
|
||||||
|
use Stancl\Tenancy\Database\DatabaseManager;
|
||||||
use Stancl\Tenancy\Events\DatabaseDeleted;
|
use Stancl\Tenancy\Events\DatabaseDeleted;
|
||||||
use Stancl\Tenancy\Events\DeletingDatabase;
|
use Stancl\Tenancy\Events\DeletingDatabase;
|
||||||
|
|
||||||
|
|
@ -25,12 +26,18 @@ class DeleteDatabase implements ShouldQueue
|
||||||
$this->tenant = $tenant;
|
$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));
|
event(new DeletingDatabase($this->tenant));
|
||||||
|
|
||||||
$this->tenant->database()->manager()->deleteDatabase($this->tenant);
|
$this->tenant->database()->manager()->deleteDatabase($this->tenant);
|
||||||
|
|
||||||
event(new DatabaseDeleted($this->tenant));
|
event(new DatabaseDeleted($this->tenant));
|
||||||
|
|
||||||
|
// revert the configuration to the original template
|
||||||
|
$databaseManager->resetTenantConnection($this->tenant);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue