1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 14:54:04 +00:00

DB manager create & delete database functions

This commit is contained in:
Samuel Štancl 2019-09-08 15:47:58 +02:00
parent 35fd19fc57
commit 74da4ddfde
2 changed files with 43 additions and 1 deletions

View file

@ -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);
}
}
}

View file

@ -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',