mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 18:04:03 +00:00
Add the option to specify connection in DatabaseManager
This commit is contained in:
parent
824d299dcd
commit
be077ae73c
3 changed files with 25 additions and 14 deletions
|
|
@ -47,11 +47,14 @@ class Migrate extends MigrateCommand
|
|||
return;
|
||||
}
|
||||
|
||||
$this->input->setOption('database', 'tenant');
|
||||
|
||||
tenant()->all($this->option('tenants'))->each(function ($tenant) {
|
||||
$this->line("Tenant: {$tenant['uuid']} ({$tenant['domain']})");
|
||||
$this->database->connectToTenant($tenant);
|
||||
|
||||
// See Illuminate\Database\Migrations\DatabaseMigrationRepository::getConnection.
|
||||
// Database connections are cached by Illuminate\Database\ConnectionResolver.
|
||||
$connectionName = "tenant{$tenant['uuid']}";
|
||||
$this->input->setOption('database', $connectionName);
|
||||
$this->database->connectToTenant($tenant, $connectionName);
|
||||
|
||||
// Migrate
|
||||
parent::handle();
|
||||
|
|
|
|||
|
|
@ -9,6 +9,8 @@ use Illuminate\Database\DatabaseManager as BaseDatabaseManager;
|
|||
final class DatabaseManager
|
||||
{
|
||||
public $originalDefaultConnection;
|
||||
|
||||
protected $defaultTenantConnectionName = 'tenant';
|
||||
|
||||
public function __construct(BaseDatabaseManager $database)
|
||||
{
|
||||
|
|
@ -16,15 +18,15 @@ final class DatabaseManager
|
|||
$this->database = $database;
|
||||
}
|
||||
|
||||
public function connect(string $database)
|
||||
public function connect(string $database, string $connectionName = null)
|
||||
{
|
||||
$this->createTenantConnection($database);
|
||||
$this->useConnection('tenant');
|
||||
$this->createTenantConnection($database, $connectionName);
|
||||
$this->useConnection($connectionName);
|
||||
}
|
||||
|
||||
public function connectToTenant($tenant)
|
||||
public function connectToTenant($tenant, string $connectionName = null)
|
||||
{
|
||||
$this->connect(tenant()->getDatabaseName($tenant));
|
||||
$this->connect(tenant()->getDatabaseName($tenant), $connectionName);
|
||||
}
|
||||
|
||||
public function disconnect()
|
||||
|
|
@ -92,21 +94,25 @@ final class DatabaseManager
|
|||
return config('database.connections.tenant.driver');
|
||||
}
|
||||
|
||||
public function createTenantConnection(string $database_name)
|
||||
public function createTenantConnection(string $databaseName, string $connectionName = null)
|
||||
{
|
||||
// Create the `tenant` database connection.
|
||||
$connectionName = $connectionName ?: $this->defaultTenantConnectionName;
|
||||
|
||||
// Create the database connection.
|
||||
$based_on = config('tenancy.database.based_on') ?: config('database.default');
|
||||
config()->set([
|
||||
'database.connections.tenant' => config('database.connections.' . $based_on),
|
||||
"database.connections.$connectionName" => config('database.connections.' . $based_on),
|
||||
]);
|
||||
|
||||
// Change DB name
|
||||
$database_name = $this->getDriver() === 'sqlite' ? database_path($database_name) : $database_name;
|
||||
config()->set(['database.connections.tenant.database' => $database_name]);
|
||||
$databaseName = $this->getDriver() === 'sqlite' ? database_path($databaseName) : $databaseName;
|
||||
config()->set(["database.connections.$connectionName.database" => $databaseName]);
|
||||
}
|
||||
|
||||
public function useConnection(string $connection)
|
||||
public function useConnection(string $connection = null)
|
||||
{
|
||||
$connection = $connection ?: $this->defaultTenantConnectionName;
|
||||
|
||||
$this->database->setDefaultConnection($connection);
|
||||
$this->database->reconnect($connection);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue