mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 14:14:04 +00:00
Make database creation queueable (#17)
* SOLIDify database creation * Return the result of fclose in SQLiteDBCreator * Make database creation queueable * Add DatabaseCreationTest * Make comment more descriptive [ci skip] * Add composer script to copy .env.example to .env * Minor tweaks * Fix env section * Fix DB_PASSWORD for Travis
This commit is contained in:
parent
136862369d
commit
c53a05cff4
13 changed files with 186 additions and 24 deletions
14
src/DatabaseCreators/MySQLDatabaseCreator.php
Normal file
14
src/DatabaseCreators/MySQLDatabaseCreator.php
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\DatabaseCreators;
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
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");
|
||||
}
|
||||
}
|
||||
13
src/DatabaseCreators/SQLiteDatabaseCreator.php
Normal file
13
src/DatabaseCreators/SQLiteDatabaseCreator.php
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\DatabaseCreators;
|
||||
|
||||
use Stancl\Tenancy\Interfaces\DatabaseCreator;
|
||||
|
||||
class SQLiteDatabaseCreator implements DatabaseCreator
|
||||
{
|
||||
public function createDatabase(string $name): bool
|
||||
{
|
||||
return fclose(fopen(database_path($name), 'w'));
|
||||
}
|
||||
}
|
||||
|
|
@ -3,6 +3,7 @@
|
|||
namespace Stancl\Tenancy;
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Stancl\Tenancy\Jobs\QueuedDatabaseCreator;
|
||||
use Illuminate\Database\DatabaseManager as BaseDatabaseManager;
|
||||
|
||||
class DatabaseManager
|
||||
|
|
@ -34,14 +35,18 @@ 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");
|
||||
if (config('tenancy.queue_database_creation', false)) {
|
||||
QueuedDatabaseCreator::dispatch(app($databaseCreators[$driver]), $name);
|
||||
} else {
|
||||
app($databaseCreators[$driver])->createDatabase($name);
|
||||
}
|
||||
}
|
||||
|
||||
public function delete()
|
||||
|
|
|
|||
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;
|
||||
}
|
||||
38
src/Jobs/QueuedDatabaseCreator.php
Normal file
38
src/Jobs/QueuedDatabaseCreator.php
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Jobs;
|
||||
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Stancl\Tenancy\Interfaces\DatabaseCreator;
|
||||
|
||||
class QueuedDatabaseCreator implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param DatabaseCreator $databaseCreator
|
||||
* @param string $databaseName
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(DatabaseCreator $databaseCreator, string $databaseName)
|
||||
{
|
||||
$this->databaseCreator = $databaseCreator;
|
||||
$this->databaseName = $databaseName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$this->databaseCreator->createDatabase($databaseName);
|
||||
}
|
||||
}
|
||||
|
|
@ -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,9 @@ return [
|
|||
// 's3',
|
||||
],
|
||||
],
|
||||
'database_creators' => [
|
||||
'sqlite' => 'Stancl\Tenancy\DatabaseCreators\SQLiteDatabaseCreator',
|
||||
'mysql' => 'Stancl\Tenancy\DatabaseCreators\MySQLDatabaseCreator',
|
||||
],
|
||||
'queue_database_creation' => false,
|
||||
];
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue