diff --git a/assets/config.php b/assets/config.php index a875c083..9e348173 100644 --- a/assets/config.php +++ b/assets/config.php @@ -152,16 +152,6 @@ return [ // 'pgsql' => Stancl\Tenancy\TenantDatabaseManagers\PostgreSQLSchemaManager::class, // Separate by schema instead of database ], - /** - * Connections used by TenantDatabaseManagers. This tells, for example, the - * MySQLDatabaseManager to use the mysql connection to create databases. - */ - 'database_manager_connections' => [ - 'sqlite' => 'sqlite', - 'mysql' => 'mysql', - 'pgsql' => 'pgsql', - ], - /** * Features are classes that provide additional functionality * not needed for tenancy to be bootstrapped. They are run diff --git a/src/Contracts/TenantDatabaseManager.php b/src/Contracts/TenantDatabaseManager.php index 10f69075..0604bb80 100644 --- a/src/Contracts/TenantDatabaseManager.php +++ b/src/Contracts/TenantDatabaseManager.php @@ -4,6 +4,8 @@ declare(strict_types=1); namespace Stancl\Tenancy\Contracts; +use Stancl\Tenancy\Exceptions\NoConnectionSetException; + interface TenantDatabaseManager { /** @@ -35,6 +37,8 @@ interface TenantDatabaseManager /** * Set the DB connection that should be used by the tenant database manager. + * + * @throws NoConnectionSetException * * @param string $connection * @return void diff --git a/src/Exceptions/ModelNotSyncMaster.php b/src/Exceptions/ModelNotSyncMasterException.php similarity index 85% rename from src/Exceptions/ModelNotSyncMaster.php rename to src/Exceptions/ModelNotSyncMasterException.php index 46fa743c..064448bc 100644 --- a/src/Exceptions/ModelNotSyncMaster.php +++ b/src/Exceptions/ModelNotSyncMasterException.php @@ -4,7 +4,7 @@ namespace Stancl\Tenancy\Exceptions; use Exception; -class ModelNotSyncMaster extends Exception +class ModelNotSyncMasterException extends Exception { public function __construct(string $class) { diff --git a/src/Exceptions/NoConnectionSetException.php b/src/Exceptions/NoConnectionSetException.php new file mode 100644 index 00000000..0b0c7c85 --- /dev/null +++ b/src/Exceptions/NoConnectionSetException.php @@ -0,0 +1,13 @@ +connection = $config->get('tenancy.database_manager_connections.mysql'); - } - protected function database(): Connection { + if ($this->connection === null) { + throw new NoConnectionSetException(static::class); + } + return DB::connection($this->connection); } diff --git a/src/TenantDatabaseManagers/PostgreSQLDatabaseManager.php b/src/TenantDatabaseManagers/PostgreSQLDatabaseManager.php index 08c7d1e7..680fcdc7 100644 --- a/src/TenantDatabaseManagers/PostgreSQLDatabaseManager.php +++ b/src/TenantDatabaseManagers/PostgreSQLDatabaseManager.php @@ -4,24 +4,23 @@ declare(strict_types=1); namespace Stancl\Tenancy\TenantDatabaseManagers; -use Illuminate\Contracts\Config\Repository; use Illuminate\Database\Connection; use Illuminate\Support\Facades\DB; use Stancl\Tenancy\Contracts\TenantDatabaseManager; use Stancl\Tenancy\Contracts\TenantWithDatabase; +use Stancl\Tenancy\Exceptions\NoConnectionSetException; class PostgreSQLDatabaseManager implements TenantDatabaseManager { /** @var string */ protected $connection; - public function __construct(Repository $config) - { - $this->connection = $config->get('tenancy.database_manager_connections.pgsql'); - } - protected function database(): Connection { + if ($this->connection === null) { + throw new NoConnectionSetException(static::class); + } + return DB::connection($this->connection); } diff --git a/src/TenantDatabaseManagers/PostgreSQLSchemaManager.php b/src/TenantDatabaseManagers/PostgreSQLSchemaManager.php index d1b40a99..e2c2cf8c 100644 --- a/src/TenantDatabaseManagers/PostgreSQLSchemaManager.php +++ b/src/TenantDatabaseManagers/PostgreSQLSchemaManager.php @@ -9,19 +9,19 @@ use Illuminate\Database\Connection; use Illuminate\Support\Facades\DB; use Stancl\Tenancy\Contracts\TenantDatabaseManager; use Stancl\Tenancy\Contracts\TenantWithDatabase; +use Stancl\Tenancy\Exceptions\NoConnectionSetException; class PostgreSQLSchemaManager implements TenantDatabaseManager { /** @var string */ protected $connection; - public function __construct(Repository $config) - { - $this->connection = $config->get('tenancy.database_manager_connections.pgsql'); - } - protected function database(): Connection { + if ($this->connection === null) { + throw new NoConnectionSetException(static::class); + } + return DB::connection($this->connection); } diff --git a/tests/DatabasePreparationTest.php b/tests/DatabasePreparationTest.php index 9f2a1e2b..8ebc4fa1 100644 --- a/tests/DatabasePreparationTest.php +++ b/tests/DatabasePreparationTest.php @@ -29,7 +29,10 @@ class DatabasePreparationTest extends TestCase $tenant = Tenant::create(); - $this->assertTrue(app(MySQLDatabaseManager::class)->databaseExists($tenant->database()->getName())); + $manager = app(MySQLDatabaseManager::class); + $manager->setConnection('mysql'); + + $this->assertTrue($manager->databaseExists($tenant->database()->getName())); } /** @test */ diff --git a/tests/ResourceSyncingTest.php b/tests/ResourceSyncingTest.php index a4ab27cd..2bd9415c 100644 --- a/tests/ResourceSyncingTest.php +++ b/tests/ResourceSyncingTest.php @@ -22,7 +22,7 @@ use Stancl\Tenancy\Events\SyncedResourceSaved; use Stancl\Tenancy\Events\TenancyEnded; use Stancl\Tenancy\Events\TenancyInitialized; use Stancl\Tenancy\Events\TenantCreated; -use Stancl\Tenancy\Exceptions\ModelNotSyncMaster; +use Stancl\Tenancy\Exceptions\ModelNotSyncMasterException; use Stancl\Tenancy\Jobs\CreateDatabase; use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper; use Stancl\Tenancy\Tests\Etc\Tenant; @@ -187,7 +187,7 @@ class ResourceSyncingTest extends TestCase tenancy()->end(); $this->assertFalse(tenancy()->initialized); - $this->expectException(ModelNotSyncMaster::class); + $this->expectException(ModelNotSyncMasterException::class); ResourceUser::first()->update(['role' => 'foobar']); } diff --git a/tests/TenantDatabaseManagerTest.php b/tests/TenantDatabaseManagerTest.php index 513cf603..6ea29ead 100644 --- a/tests/TenantDatabaseManagerTest.php +++ b/tests/TenantDatabaseManagerTest.php @@ -43,16 +43,19 @@ class TenantDatabaseManagerTest extends TestCase $name = 'db' . $this->randomString(); - $this->assertFalse(app($databaseManager)->databaseExists($name)); + $manager = app($databaseManager); + $manager->setConnection($driver); + + $this->assertFalse($manager->databaseExists($name)); $tenant = Tenant::create([ 'tenancy_db_name' => $name, 'tenancy_db_connection' => $driver, ]); - $this->assertTrue(app($databaseManager)->databaseExists($name)); - app($databaseManager)->deleteDatabase($tenant); - $this->assertFalse(app($databaseManager)->databaseExists($name)); + $this->assertTrue($manager->databaseExists($name)); + $manager->deleteDatabase($tenant); + $this->assertFalse($manager->databaseExists($name)); } /** @test */ @@ -68,23 +71,29 @@ class TenantDatabaseManagerTest extends TestCase $database = 'db' . $this->randomString(); - $this->assertFalse(app(MySQLDatabaseManager::class)->databaseExists($database)); + $mysqlmanager = app(MySQLDatabaseManager::class); + $mysqlmanager->setConnection('mysql'); + + $this->assertFalse($mysqlmanager->databaseExists($database)); Tenant::create([ 'tenancy_db_name' => $database, 'tenancy_db_connection' => 'mysql', ]); - $this->assertTrue(app(MySQLDatabaseManager::class)->databaseExists($database)); + $this->assertTrue($mysqlmanager->databaseExists($database)); + + $postgresManager = app(PostgreSQLDatabaseManager::class); + $postgresManager->setConnection('pgsql'); $database = 'db' . $this->randomString(); - $this->assertFalse(app(PostgreSQLDatabaseManager::class)->databaseExists($database)); + $this->assertFalse($postgresManager->databaseExists($database)); Tenant::create([ 'tenancy_db_name' => $database, 'tenancy_db_connection' => 'pgsql', ]); - $this->assertTrue(app(PostgreSQLDatabaseManager::class)->databaseExists($database)); + $this->assertTrue($postgresManager->databaseExists($database)); } public function database_manager_provider()