1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-13 07:34:03 +00:00

Add the option to specify connection in DatabaseManager

This commit is contained in:
Samuel Štancl 2019-08-17 15:25:39 +02:00
parent 824d299dcd
commit be077ae73c
3 changed files with 25 additions and 14 deletions

View file

@ -47,11 +47,14 @@ class Migrate extends MigrateCommand
return; return;
} }
$this->input->setOption('database', 'tenant');
tenant()->all($this->option('tenants'))->each(function ($tenant) { tenant()->all($this->option('tenants'))->each(function ($tenant) {
$this->line("Tenant: {$tenant['uuid']} ({$tenant['domain']})"); $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 // Migrate
parent::handle(); parent::handle();

View file

@ -10,21 +10,23 @@ final class DatabaseManager
{ {
public $originalDefaultConnection; public $originalDefaultConnection;
protected $defaultTenantConnectionName = 'tenant';
public function __construct(BaseDatabaseManager $database) public function __construct(BaseDatabaseManager $database)
{ {
$this->originalDefaultConnection = config('database.default'); $this->originalDefaultConnection = config('database.default');
$this->database = $database; $this->database = $database;
} }
public function connect(string $database) public function connect(string $database, string $connectionName = null)
{ {
$this->createTenantConnection($database); $this->createTenantConnection($database, $connectionName);
$this->useConnection('tenant'); $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() public function disconnect()
@ -92,21 +94,25 @@ final class DatabaseManager
return config('database.connections.tenant.driver'); 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'); $based_on = config('tenancy.database.based_on') ?: config('database.default');
config()->set([ config()->set([
'database.connections.tenant' => config('database.connections.' . $based_on), "database.connections.$connectionName" => config('database.connections.' . $based_on),
]); ]);
// Change DB name // Change DB name
$database_name = $this->getDriver() === 'sqlite' ? database_path($database_name) : $database_name; $databaseName = $this->getDriver() === 'sqlite' ? database_path($databaseName) : $databaseName;
config()->set(['database.connections.tenant.database' => $database_name]); 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->setDefaultConnection($connection);
$this->database->reconnect($connection); $this->database->reconnect($connection);
} }

View file

@ -114,6 +114,8 @@ class CommandsTest extends TestCase
->expectsOutput('xyz'); ->expectsOutput('xyz');
} }
// todo check that multiple tenants can be migrated at once using all database engines
/** @test */ /** @test */
public function install_command_works() public function install_command_works()
{ {