mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 18:04:03 +00:00
DB manager create & delete database functions
This commit is contained in:
parent
35fd19fc57
commit
74da4ddfde
2 changed files with 43 additions and 1 deletions
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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',
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue