mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-04 20:54:03 +00:00
Make database creation queueable
This commit is contained in:
parent
aeaa33c743
commit
b62fc5fe31
2 changed files with 40 additions and 1 deletions
|
|
@ -2,8 +2,14 @@
|
||||||
|
|
||||||
namespace Stancl\Tenancy;
|
namespace Stancl\Tenancy;
|
||||||
|
|
||||||
|
use Illuminate\Bus\Queueable;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Illuminate\Queue\SerializesModels;
|
||||||
|
use Illuminate\Queue\InteractsWithQueue;
|
||||||
|
use Illuminate\Foundation\Bus\Dispatchable;
|
||||||
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
use Illuminate\Database\DatabaseManager as BaseDatabaseManager;
|
use Illuminate\Database\DatabaseManager as BaseDatabaseManager;
|
||||||
|
use Stancl\Tenancy\Interfaces\DatabaseCreator;
|
||||||
|
|
||||||
class DatabaseManager
|
class DatabaseManager
|
||||||
{
|
{
|
||||||
|
|
@ -41,7 +47,11 @@ class DatabaseManager
|
||||||
throw new \Exception("Database could not be created: no database creator for driver $driver is registered.");
|
throw new \Exception("Database could not be created: no database creator for driver $driver is registered.");
|
||||||
}
|
}
|
||||||
|
|
||||||
app($databaseCreators[$driver])->createDatabase($name);
|
if (config('tenancy.queue_database_creation', false)) {
|
||||||
|
QueuedDatabaseCreator::dispatch(app($databaseCreators[$driver], $name));
|
||||||
|
} else {
|
||||||
|
app($databaseCreators[$driver])->createDatabase($name);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getDriver(): ?string
|
public function getDriver(): ?string
|
||||||
|
|
@ -62,3 +72,31 @@ class DatabaseManager
|
||||||
config()->set(['database.connections.tenant.database' => $database_name]);
|
config()->set(['database.connections.tenant.database' => $database_name]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -33,4 +33,5 @@ return [
|
||||||
'sqlite' => 'Stancl\Tenancy\DatabaseCreators\SQLiteDatabaseCreator',
|
'sqlite' => 'Stancl\Tenancy\DatabaseCreators\SQLiteDatabaseCreator',
|
||||||
'mysql' => 'Stancl\Tenancy\DatabaseCreators\MySQLDatabaseCreator',
|
'mysql' => 'Stancl\Tenancy\DatabaseCreators\MySQLDatabaseCreator',
|
||||||
],
|
],
|
||||||
|
'queue_database_creation' => false,
|
||||||
];
|
];
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue