From 2de46e4274ae68d90c446c6f9780dc8c9782e7bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Thu, 30 Apr 2020 21:25:41 +0200 Subject: [PATCH] Make phpunit run --- src/Commands/Migrate.php | 2 +- src/Commands/MigrateFresh.php | 2 +- src/Commands/Rollback.php | 2 +- src/Commands/Seed.php | 2 +- .../ModifiesDatabaseNameForConnection.php | 11 ++++++++ src/DatabaseConfig.php | 27 ++++++++++--------- src/DatabaseManager.php | 8 +++--- .../SQLiteDatabaseManager.php | 8 +++++- 8 files changed, 40 insertions(+), 22 deletions(-) create mode 100644 src/Contracts/ModifiesDatabaseNameForConnection.php diff --git a/src/Commands/Migrate.php b/src/Commands/Migrate.php index 279a371b..8151ff5a 100644 --- a/src/Commands/Migrate.php +++ b/src/Commands/Migrate.php @@ -52,7 +52,7 @@ class Migrate extends MigrateCommand tenancy()->all($this->option('tenants'))->each(function ($tenant) { $this->line("Tenant: {$tenant['id']}"); - $this->input->setOption('database', $tenant->getConnectionName()); + $this->input->setOption('database', $tenant->database()->getTemplateConnectionName()); $tenant->run(function () { // Migrate diff --git a/src/Commands/MigrateFresh.php b/src/Commands/MigrateFresh.php index bef3a09a..e08a0fa0 100644 --- a/src/Commands/MigrateFresh.php +++ b/src/Commands/MigrateFresh.php @@ -39,7 +39,7 @@ final class MigrateFresh extends Command $tenant->run(function ($tenant) { $this->info('Dropping tables.'); $this->call('db:wipe', array_filter([ - '--database' => $tenant->getConnectionName(), + '--database' => $tenant->database()->getTemplateConnectionName(), '--force' => true, ])); diff --git a/src/Commands/Rollback.php b/src/Commands/Rollback.php index eee99ddd..ec228412 100644 --- a/src/Commands/Rollback.php +++ b/src/Commands/Rollback.php @@ -52,7 +52,7 @@ class Rollback extends RollbackCommand tenancy()->all($this->option('tenants'))->each(function ($tenant) { $this->line("Tenant: {$tenant['id']}"); - $this->input->setOption('database', $tenant->getConnectionName()); + $this->input->setOption('database', $tenant->database()->getTemplateConnectionName()); $tenant->run(function () { // Rollback diff --git a/src/Commands/Seed.php b/src/Commands/Seed.php index 36f59dd0..c57c36eb 100644 --- a/src/Commands/Seed.php +++ b/src/Commands/Seed.php @@ -56,7 +56,7 @@ class Seed extends SeedCommand tenancy()->all($this->option('tenants'))->each(function ($tenant) { $this->line("Tenant: {$tenant['id']}"); - $this->input->setOption('database', $tenant->getConnectionName()); + $this->input->setOption('database', $tenant->database()->getTemplateConnectionName()); $tenant->run(function () { // Seed diff --git a/src/Contracts/ModifiesDatabaseNameForConnection.php b/src/Contracts/ModifiesDatabaseNameForConnection.php new file mode 100644 index 00000000..57114c38 --- /dev/null +++ b/src/Contracts/ModifiesDatabaseNameForConnection.php @@ -0,0 +1,11 @@ +tenant->data['_tenancy_db_name']; + return $this->tenant->data['_tenancy_db_name'] ?? (static::$databaseNameGenerator)($this->tenant); } - public function getUsername(): string + public function getUsername(): ?string { - return $this->tenant->data['_tenancy_db_username']; + return $this->tenant->data['_tenancy_db_username'] ?? null; } - public function getPassword(): string + public function getPassword(): ?string { - return $this->tenant->data['_tenancy_db_password']; - } - - public function getTemplateDatabaseConnection(): string - { - return $this->tenant->data['_tenancy_db_connection']; + return $this->tenant->data['_tenancy_db_password'] ?? null; } public function makeCredentials(): void @@ -100,7 +96,7 @@ class DatabaseConfig */ public function getTemplateConnectionName() { - $name = $this->tenant->getTemplateDatabaseConnection(); + $name = $this->tenant->data['_tenancy_db_connection'] ?? 'tenant'; // If we're using e.g. 'tenant', the default, template connection // and it doesn't exist, we'll go for the default DB template. @@ -118,8 +114,13 @@ class DatabaseConfig { $templateConnection = config("database.connections.{$this->getTemplateConnectionName()}"); + if (($manager = $this->manager()) instanceof ModifiesDatabaseNameForConnection) { + /** @var ModifiesDatabaseNameForConnection $manager */ + $databaseName = $manager->getDatabaseNameForConnection($this->getName()); + } + return array_merge($templateConnection, $this->tenantConfig(), [ - $this->manager()->getSeparator() => $this->tenant->database()->getName(), + $this->manager()->getSeparator() => $databaseName, ]); } diff --git a/src/DatabaseManager.php b/src/DatabaseManager.php index af5919fe..727e353d 100644 --- a/src/DatabaseManager.php +++ b/src/DatabaseManager.php @@ -54,9 +54,9 @@ class DatabaseManager */ public function connect(Tenant $tenant) { - $this->createTenantConnection($tenant, $tenant->getConnectionName()); - $this->setDefaultConnection($tenant->getConnectionName()); - $this->switchConnection($tenant->getConnectionName()); + $this->createTenantConnection($tenant, $tenant->database()->getTemplateConnectionName()); + $this->setDefaultConnection($tenant->database()->getTemplateConnectionName()); + $this->switchConnection($tenant->database()->getTemplateConnectionName()); } /** @@ -83,7 +83,7 @@ class DatabaseManager */ public function createTenantConnection(Tenant $tenant, $connectionName) { - $this->app['config']["database.connections.$connectionName"] = $tenant->database->connection(); + $this->app['config']["database.connections.$connectionName"] = $tenant->database()->connection(); } /** diff --git a/src/TenantDatabaseManagers/SQLiteDatabaseManager.php b/src/TenantDatabaseManagers/SQLiteDatabaseManager.php index 95609681..20d60665 100644 --- a/src/TenantDatabaseManagers/SQLiteDatabaseManager.php +++ b/src/TenantDatabaseManagers/SQLiteDatabaseManager.php @@ -4,9 +4,10 @@ declare(strict_types=1); namespace Stancl\Tenancy\TenantDatabaseManagers; +use Stancl\Tenancy\Contracts\ModifiesDatabaseNameForConnection; use Stancl\Tenancy\Contracts\TenantDatabaseManager; -class SQLiteDatabaseManager implements TenantDatabaseManager +class SQLiteDatabaseManager implements TenantDatabaseManager, ModifiesDatabaseNameForConnection { public function getSeparator(): string { @@ -35,4 +36,9 @@ class SQLiteDatabaseManager implements TenantDatabaseManager { return file_exists(database_path($name)); } + + public function getDatabaseNameForConnection(string $original): string + { + return database_path($original); + } }