mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 13:54:03 +00:00
canCreate database
This commit is contained in:
parent
08319695b9
commit
de54b5708c
7 changed files with 60 additions and 7 deletions
|
|
@ -21,4 +21,12 @@ interface TenantDatabaseManager
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function deleteDatabase(string $name): bool;
|
public function deleteDatabase(string $name): bool;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Does a database exist.
|
||||||
|
*
|
||||||
|
* @param string $name
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public function databaseExists(string $name): bool;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ use Illuminate\Foundation\Application;
|
||||||
use Stancl\Tenancy\Jobs\QueuedTenantDatabaseCreator;
|
use Stancl\Tenancy\Jobs\QueuedTenantDatabaseCreator;
|
||||||
use Illuminate\Database\DatabaseManager as BaseDatabaseManager;
|
use Illuminate\Database\DatabaseManager as BaseDatabaseManager;
|
||||||
use Stancl\Tenancy\Exceptions\DatabaseManagerNotRegisteredException;
|
use Stancl\Tenancy\Exceptions\DatabaseManagerNotRegisteredException;
|
||||||
|
use Stancl\Tenancy\Exceptions\TenantDatabaseAlreadyExistsException;
|
||||||
|
|
||||||
class DatabaseManagerv2
|
class DatabaseManagerv2
|
||||||
{
|
{
|
||||||
|
|
@ -95,7 +96,11 @@ class DatabaseManagerv2
|
||||||
*/
|
*/
|
||||||
public function canCreate(Tenant $tenant)
|
public function canCreate(Tenant $tenant)
|
||||||
{
|
{
|
||||||
// todo
|
if ($this->getTenantDatabaseManager($tenant)->databaseExists($database = $tenant->getDatabaseName())) {
|
||||||
|
return new TenantDatabaseAlreadyExistsException($database);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function createDatabase(Tenant $tenant)
|
public function createDatabase(Tenant $tenant)
|
||||||
|
|
@ -129,7 +134,7 @@ class DatabaseManagerv2
|
||||||
|
|
||||||
$databaseManagers = $this->app['config']['tenancy.database_managers'];
|
$databaseManagers = $this->app['config']['tenancy.database_managers'];
|
||||||
|
|
||||||
if (! \array_key_exists($driver, $databaseManagers)) {
|
if (! array_key_exists($driver, $databaseManagers)) {
|
||||||
throw new DatabaseManagerNotRegisteredException($driver);
|
throw new DatabaseManagerNotRegisteredException($driver);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
23
src/Exceptions/TenantDatabaseAlreadyExistsException.php
Normal file
23
src/Exceptions/TenantDatabaseAlreadyExistsException.php
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Stancl\Tenancy\Exceptions;
|
||||||
|
|
||||||
|
use Stancl\Tenancy\Contracts\TenantCannotBeCreatedException;
|
||||||
|
|
||||||
|
class TenantDatabaseAlreadyExistsException extends TenantCannotBeCreatedException
|
||||||
|
{
|
||||||
|
/** @var string */
|
||||||
|
protected $database;
|
||||||
|
|
||||||
|
public function reason(): string
|
||||||
|
{
|
||||||
|
return "Database {$this->database} already exists.";
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __construct(string $database)
|
||||||
|
{
|
||||||
|
parent::__construct();
|
||||||
|
|
||||||
|
$this->database = $database;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -5,7 +5,7 @@ declare(strict_types=1);
|
||||||
namespace Stancl\Tenancy\TenantDatabaseManagers;
|
namespace Stancl\Tenancy\TenantDatabaseManagers;
|
||||||
|
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Stancl\Tenancy\Interfaces\TenantDatabaseManager;
|
use Stancl\Tenancy\Contracts\TenantDatabaseManager;
|
||||||
|
|
||||||
class MySQLDatabaseManager implements TenantDatabaseManager
|
class MySQLDatabaseManager implements TenantDatabaseManager
|
||||||
{
|
{
|
||||||
|
|
@ -18,4 +18,9 @@ class MySQLDatabaseManager implements TenantDatabaseManager
|
||||||
{
|
{
|
||||||
return DB::statement("DROP DATABASE `$name`");
|
return DB::statement("DROP DATABASE `$name`");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function databaseExists(string $name): bool
|
||||||
|
{
|
||||||
|
return (bool) DB::select("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '$name'");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ declare(strict_types=1);
|
||||||
namespace Stancl\Tenancy\TenantDatabaseManagers;
|
namespace Stancl\Tenancy\TenantDatabaseManagers;
|
||||||
|
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Stancl\Tenancy\Interfaces\TenantDatabaseManager;
|
use Stancl\Tenancy\Contracts\TenantDatabaseManager;
|
||||||
|
|
||||||
class PostgreSQLDatabaseManager implements TenantDatabaseManager
|
class PostgreSQLDatabaseManager implements TenantDatabaseManager
|
||||||
{
|
{
|
||||||
|
|
@ -18,4 +18,9 @@ class PostgreSQLDatabaseManager implements TenantDatabaseManager
|
||||||
{
|
{
|
||||||
return DB::statement("DROP DATABASE \"$name\"");
|
return DB::statement("DROP DATABASE \"$name\"");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function databaseExists(string $name): bool
|
||||||
|
{
|
||||||
|
return (bool) DB::select("SELECT datname FROM pg_database WHERE datname = '$name'");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,14 +4,14 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\TenantDatabaseManagers;
|
namespace Stancl\Tenancy\TenantDatabaseManagers;
|
||||||
|
|
||||||
use Stancl\Tenancy\Interfaces\TenantDatabaseManager;
|
use Stancl\Tenancy\Contracts\TenantDatabaseManager;
|
||||||
|
|
||||||
class SQLiteDatabaseManager implements TenantDatabaseManager
|
class SQLiteDatabaseManager implements TenantDatabaseManager
|
||||||
{
|
{
|
||||||
public function createDatabase(string $name): bool
|
public function createDatabase(string $name): bool
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
return \fclose(\fopen(database_path($name), 'w'));
|
return fclose(fopen(database_path($name), 'w'));
|
||||||
} catch (\Throwable $th) {
|
} catch (\Throwable $th) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
@ -20,9 +20,14 @@ class SQLiteDatabaseManager implements TenantDatabaseManager
|
||||||
public function deleteDatabase(string $name): bool
|
public function deleteDatabase(string $name): bool
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
return \unlink(database_path($name));
|
return unlink(database_path($name));
|
||||||
} catch (\Throwable $th) {
|
} catch (\Throwable $th) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function databaseExists(string $name): bool
|
||||||
|
{
|
||||||
|
return file_exists(database_path($name));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,8 @@ use Stancl\Tenancy\Jobs\QueuedTenantDatabaseDeleter;
|
||||||
|
|
||||||
class TenantDatabaseManagerTest extends TestCase
|
class TenantDatabaseManagerTest extends TestCase
|
||||||
{
|
{
|
||||||
|
// todo use data providers and TenantDatabaseManager::databaseExists()
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function sqlite_database_can_be_created_and_deleted()
|
public function sqlite_database_can_be_created_and_deleted()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue