From 04b4e0644f373e392caed360c63a0ff0ddc0b870 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Sun, 8 Sep 2019 13:46:07 +0200 Subject: [PATCH] WIP DatabaseManager --- assets/config.php | 1 + src/Commands/Migrate.php | 5 ++--- src/Contracts/Feature.php | 1 + src/DatabaseManagerv2.php | 19 +++++++++++++------ src/Tenant.php | 2 +- src/TenantManagerv2.php | 8 ++++++++ src/TenantModel.php | 2 +- 7 files changed, 27 insertions(+), 11 deletions(-) diff --git a/assets/config.php b/assets/config.php index c20751fa..564d3d55 100644 --- a/assets/config.php +++ b/assets/config.php @@ -68,6 +68,7 @@ return [ 'Stancl\Tenancy\Features\TelescopeTags', 'Stancl\Tenancy\Features\TenantRedirect', ], + 'migrate_after_creation' => false, // run migrations after creating a tenant 'queue_database_creation' => false, 'queue_database_deletion' => false, 'unique_id_generator' => 'Stancl\Tenancy\UUIDGenerator', diff --git a/src/Commands/Migrate.php b/src/Commands/Migrate.php index cb98d7e2..b5e525a1 100644 --- a/src/Commands/Migrate.php +++ b/src/Commands/Migrate.php @@ -54,9 +54,8 @@ class Migrate extends MigrateCommand // See Illuminate\Database\Migrations\DatabaseMigrationRepository::getConnection. // Database connections are cached by Illuminate\Database\ConnectionResolver. - $connectionName = "tenant{$tenant['uuid']}"; // todo use Illuminate DatabaseManager reconnect()? - $this->input->setOption('database', $connectionName); - $this->database->connectToTenant($tenant, $connectionName); + $this->input->setOption('database', 'tenant'); + $this->database->connectToTenant($tenant); // todo test that this works with multiple tenants with MySQL // Migrate parent::handle(); diff --git a/src/Contracts/Feature.php b/src/Contracts/Feature.php index ffc77ba7..70307108 100644 --- a/src/Contracts/Feature.php +++ b/src/Contracts/Feature.php @@ -7,6 +7,7 @@ namespace Stancl\Tenancy\Contracts; use Stancl\Tenancy\TenantManager; /** Additional features, like Telescope tags and tenant redirects. */ +// todo should this be FeatureProvider? interface Feature { // todo is the tenantManager argument necessary? diff --git a/src/DatabaseManagerv2.php b/src/DatabaseManagerv2.php index 04337161..0d648e01 100644 --- a/src/DatabaseManagerv2.php +++ b/src/DatabaseManagerv2.php @@ -4,37 +4,44 @@ declare(strict_types=1); namespace Stancl\Tenancy; +use Illuminate\Foundation\Application; use Illuminate\Database\DatabaseManager as BaseDatabaseManager; class DatabaseManagerv2 { + /** @var string */ + public $originalDefaultConnectionName; + + /** @var Application */ + protected $app; + /** @var BaseDatabaseManager */ protected $database; - public function __construct(BaseDatabaseManager $database) + public function __construct(Application $app, BaseDatabaseManager $database) { $this->database = $database; + $this->originalDefaultConnectionName = $app['config']['database.default']; } public function connect(Tenant $tenant) { - $connection = $tenant->getConnectionName(); // todo - + $connection = 'tenant'; // todo tenant-specific connections $this->createTenantConnection($tenant->getDatabaseName(), $connection); $this->switchConnection($connection); } public function reconnect() { - $this->switchConnection($this->originalDefaultConnection); + $this->switchConnection($this->originalDefaultConnectionName); } public function createTenantConnection(string $databaseName, string $connectionName = null) { - $connectionName = $connectionName ?: $this->defaultTenantConnectionName; + $connectionName = $connectionName ?? 'tenant'; // todo // Create the database connection. - $based_on = config('tenancy.database.based_on') ?: config('database.default'); + $based_on = config('tenancy.database.based_on') ?? $this->originalDefaultConnectionName; config()->set([ "database.connections.$connectionName" => config('database.connections.' . $based_on), ]); diff --git a/src/Tenant.php b/src/Tenant.php index b99670d5..e3606f25 100644 --- a/src/Tenant.php +++ b/src/Tenant.php @@ -97,7 +97,7 @@ class Tenant implements ArrayAccess public function getDatabaseName() { - return $this['_tenancy_database'] ?? $this->app['config']['tenancy.database.prefix'] . $this->uuid . $this->app['config']['tenancy.database.suffix']; + return $this['_tenancy_db_name'] ?? $this->app['config']['tenancy.database.prefix'] . $this->uuid . $this->app['config']['tenancy.database.suffix']; } public function __get($name) diff --git a/src/TenantManagerv2.php b/src/TenantManagerv2.php index 811177d6..4c6836ab 100644 --- a/src/TenantManagerv2.php +++ b/src/TenantManagerv2.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Stancl\Tenancy; use Illuminate\Foundation\Application; +use Stancl\Tenancy\Exceptions\NoTenantIdentifiedException; use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException; /** @@ -41,6 +42,7 @@ class TenantManagerv2 { // todo make this atomic $this->storage->createTenant($tenant); + $this->database->create($tenant); // todo create database, optionally migrate return $this; @@ -125,6 +127,12 @@ class TenantManagerv2 return $this; } + /** + * Get the current tenant + * + * @return Tenant + * @throws NoTenantIdentifiedException + */ public function getTenant(): Tenant { if (! $this->tenant) { diff --git a/src/TenantModel.php b/src/TenantModel.php index 9907b541..902f12e3 100644 --- a/src/TenantModel.php +++ b/src/TenantModel.php @@ -37,7 +37,7 @@ class TenantModel extends Model public function getConnectionName() { - return config('tenancy.storage.db.connection'); + return config('tenancy.storage.db.connection') ?? app(DatabaseManager::class)->originalDefaultConnectionName; } public static function getAllTenants(array $uuids)