diff --git a/src/DatabaseManagerv2.php b/src/DatabaseManagerv2.php index 11b13072..98a0ae58 100644 --- a/src/DatabaseManagerv2.php +++ b/src/DatabaseManagerv2.php @@ -6,6 +6,7 @@ namespace Stancl\Tenancy; use Illuminate\Foundation\Application; use Illuminate\Database\DatabaseManager as BaseDatabaseManager; +use Stancl\Tenancy\Jobs\QueuedTenantDatabaseCreator; class DatabaseManagerv2 { @@ -95,4 +96,45 @@ class DatabaseManagerv2 { // todo } + + public function createDatabase(Tenant $tenant) + { + $database = $tenant->getDatabaseName(); + $connection = $tenant->getConnectionName(); // todo + $driver = $this->getDriver($connection); + + $databaseManagers = $this->app['config']['tenancy.database_managers']; + + if (! \array_key_exists($driver, $databaseManagers)) { + throw new DatabaseManagerNotRegisteredException('Database could not be created', $driver); + } + + $manager = $databaseManagers[$driver]; + if ($this->app['config']['tenancy.queue_database_creation'] ?? false) { + QueuedTenantDatabaseCreator::dispatch($this->app[$manager], $database, 'create'); + } else { + return app($manager)->createDatabase($database); + } + } + + // todo this is the same as createDatabase. find a way to remove duplicite code + public function deleteDatabase(Tenant $tenant) + { + $database = $tenant->getDatabaseName(); + $connection = $tenant->getConnectionName(); // todo + $driver = $this->getDriver($connection); + + $databaseManagers = $this->app['config']['tenancy.database_managers']; + + if (! \array_key_exists($driver, $databaseManagers)) { + throw new DatabaseManagerNotRegisteredException('Database could not be deleted', $driver); + } + + $manager = $databaseManagers[$driver]; + if ($this->app['config']['tenancy.queue_database_creation'] ?? false) { + QueuedTenantDatabaseCreator::dispatch($this->app[$manager], $database, 'delete'); + } else { + return app($manager)->deleteDatabase($database); + } + } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 5dae9641..1a13cd69 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -82,7 +82,7 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase // Use the #14 Redis database unless specified otherwise. // Make sure you don't store anything in this db! 'database' => env('TENANCY_TEST_REDIS_DB', 14), - 'prefix' => 'abc', // todo unrelated to tenancy, but this doesn't seem to have an effect? try to replicate in a fresh laravel installation + 'prefix' => 'abc', // unrelated to tenancy, but this doesn't seem to have an effect? try to replicate in a fresh laravel installation ], 'database.connections.central' => [ 'driver' => 'sqlite',