mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 17:24:03 +00:00
Improve DatabaseManager create & delete fns code
This commit is contained in:
parent
11d7c564d9
commit
08319695b9
2 changed files with 19 additions and 21 deletions
|
|
@ -7,6 +7,7 @@ namespace Stancl\Tenancy;
|
|||
use Illuminate\Foundation\Application;
|
||||
use Stancl\Tenancy\Jobs\QueuedTenantDatabaseCreator;
|
||||
use Illuminate\Database\DatabaseManager as BaseDatabaseManager;
|
||||
use Stancl\Tenancy\Exceptions\DatabaseManagerNotRegisteredException;
|
||||
|
||||
class DatabaseManagerv2
|
||||
{
|
||||
|
|
@ -100,16 +101,8 @@ class DatabaseManagerv2
|
|||
public function createDatabase(Tenant $tenant)
|
||||
{
|
||||
$database = $tenant->getDatabaseName();
|
||||
$connection = $tenant->getConnectionName(); // todo
|
||||
$driver = $this->getDriver($connection);
|
||||
$manager = $this->getTenantDatabaseManager($tenant);
|
||||
|
||||
$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 {
|
||||
|
|
@ -117,24 +110,29 @@ class DatabaseManagerv2
|
|||
}
|
||||
}
|
||||
|
||||
// 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);
|
||||
$manager = $this->getTenantDatabaseManager($tenant);
|
||||
|
||||
$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 $this->app[$manager]->deleteDatabase($database);
|
||||
}
|
||||
}
|
||||
|
||||
protected function getTenantDatabaseManager(Tenant $tenant)
|
||||
{
|
||||
$connection = $tenant->getConnectionName(); // todo
|
||||
$driver = $this->getDriver($connection);
|
||||
|
||||
$databaseManagers = $this->app['config']['tenancy.database_managers'];
|
||||
|
||||
if (! \array_key_exists($driver, $databaseManagers)) {
|
||||
throw new DatabaseManagerNotRegisteredException($driver);
|
||||
}
|
||||
|
||||
return $databaseManagers[$driver];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,8 +6,8 @@ namespace Stancl\Tenancy\Exceptions;
|
|||
|
||||
class DatabaseManagerNotRegisteredException extends \Exception
|
||||
{
|
||||
public function __construct($error, $driver)
|
||||
public function __construct($driver)
|
||||
{
|
||||
$this->message = "$error: no database manager for driver $driver is registered.";
|
||||
$this->message = "Database manager for driver $driver is not registered.";
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue