mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-04 14:44:05 +00:00
SOLIDify database creation
This commit is contained in:
parent
0adbfee86e
commit
623c8494ba
5 changed files with 52 additions and 7 deletions
13
src/DatabaseCreators/MySQLDatabaseCreator.php
Normal file
13
src/DatabaseCreators/MySQLDatabaseCreator.php
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\DatabaseCreators;
|
||||
|
||||
use Stancl\Tenancy\Interfaces\DatabaseCreator;
|
||||
|
||||
class MySQLDatabaseCreator implements DatabaseCreator
|
||||
{
|
||||
public function createDatabase(string $name): bool
|
||||
{
|
||||
return DB::statement("CREATE DATABASE `$name` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");
|
||||
}
|
||||
}
|
||||
15
src/DatabaseCreators/SQLiteDatabaseCreator.php
Normal file
15
src/DatabaseCreators/SQLiteDatabaseCreator.php
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\DatabaseCreators;
|
||||
|
||||
use Stancl\Tenancy\Interfaces\DatabaseCreator;
|
||||
|
||||
class SQLiteDatabaseCreator implements DatabaseCreator
|
||||
{
|
||||
public function createDatabase(string $name): bool
|
||||
{
|
||||
fclose(fopen(database_path($name), 'w'));
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
@ -34,14 +34,14 @@ class DatabaseManager
|
|||
{
|
||||
$this->createTenantConnection($name);
|
||||
$driver = $driver ?: $this->getDriver();
|
||||
if ($driver === "sqlite") {
|
||||
$f = fopen(database_path($name), 'w');
|
||||
fclose($f);
|
||||
|
||||
return;
|
||||
|
||||
$databaseCreators = config('tenancy.database_creators');
|
||||
|
||||
if (! array_key_exists($driver, $databaseCreators)) {
|
||||
throw new \Exception("Database could not be created: no database creator for driver $driver is registered.");
|
||||
}
|
||||
|
||||
return DB::statement("CREATE DATABASE `$name` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");
|
||||
app($databaseCreators[$driver])->createDatabase($name);
|
||||
}
|
||||
|
||||
public function getDriver(): ?string
|
||||
|
|
|
|||
14
src/Interfaces/DatabaseCreator.php
Normal file
14
src/Interfaces/DatabaseCreator.php
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Interfaces;
|
||||
|
||||
interface DatabaseCreator
|
||||
{
|
||||
/**
|
||||
* Create a database.
|
||||
*
|
||||
* @param string $name Name of the database.
|
||||
* @return void
|
||||
*/
|
||||
public function createDatabase(string $name): bool;
|
||||
}
|
||||
|
|
@ -21,7 +21,6 @@ return [
|
|||
'cache' => [
|
||||
'prefix_base' => 'tenant',
|
||||
],
|
||||
|
||||
'filesystem' => [
|
||||
'suffix_base' => 'tenant',
|
||||
// Disks which should be suffixed with the suffix_base + tenant UUID.
|
||||
|
|
@ -30,4 +29,8 @@ return [
|
|||
// 's3',
|
||||
],
|
||||
],
|
||||
'database_creators' => [
|
||||
'sqlite' => 'Stancl\Tenancy\DatabaseCreators\SQLiteDatabaseCreator',
|
||||
'mysql' => 'Stancl\Tenancy\DatabaseCreators\MySQLDatabaseCreator',
|
||||
],
|
||||
];
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue