mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-13 00:14:04 +00:00
[2.x] Allow automatic seeding after automatic migrations (#160)
This commit is contained in:
parent
3dbbbe8b24
commit
d5b01219fd
4 changed files with 113 additions and 6 deletions
42
src/Jobs/QueuedTenantDatabaseSeeder.php
Normal file
42
src/Jobs/QueuedTenantDatabaseSeeder.php
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Jobs;
|
||||
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Stancl\Tenancy\Tenant;
|
||||
|
||||
class QueuedTenantDatabaseSeeder implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
/** @var string */
|
||||
protected $tenantId;
|
||||
|
||||
/** @var array */
|
||||
protected $seederParameters = [];
|
||||
|
||||
public function __construct(Tenant $tenant, $seederParameters = [])
|
||||
{
|
||||
$this->tenantId = $tenant->id;
|
||||
$this->seederParameters = $seederParameters;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
Artisan::call('tenants:seed', [
|
||||
'--tenants' => [$this->tenantId],
|
||||
] + $this->seederParameters);
|
||||
}
|
||||
}
|
||||
|
|
@ -10,6 +10,7 @@ use Illuminate\Support\Collection;
|
|||
use Stancl\Tenancy\Contracts\TenantCannotBeCreatedException;
|
||||
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException;
|
||||
use Stancl\Tenancy\Jobs\QueuedTenantDatabaseMigrator;
|
||||
use Stancl\Tenancy\Jobs\QueuedTenantDatabaseSeeder;
|
||||
|
||||
/**
|
||||
* @internal Class is subject to breaking changes in minor and patch versions.
|
||||
|
|
@ -67,15 +68,23 @@ class TenantManager
|
|||
$afterCreating = [];
|
||||
|
||||
if ($this->shouldMigrateAfterCreation()) {
|
||||
$afterCreating += $this->databaseCreationQueued() ? [
|
||||
new QueuedTenantDatabaseMigrator($tenant),
|
||||
] : [
|
||||
function () use ($tenant) {
|
||||
$afterCreating[] = $this->databaseCreationQueued()
|
||||
? new QueuedTenantDatabaseMigrator($tenant)
|
||||
: function () use ($tenant) {
|
||||
$this->artisan->call('tenants:migrate', [
|
||||
'--tenants' => [$tenant['id']],
|
||||
]);
|
||||
},
|
||||
];
|
||||
};
|
||||
}
|
||||
|
||||
if ($this->shouldSeedAfterMigration()) {
|
||||
$afterCreating[] = $this->databaseCreationQueued()
|
||||
? new QueuedTenantDatabaseSeeder($tenant, $this->getSeederParameters())
|
||||
: function () use ($tenant) {
|
||||
$this->artisan->call('tenants:seed', [
|
||||
'--tenants' => [$tenant['id']],
|
||||
] + $this->getSeederParameters());
|
||||
};
|
||||
}
|
||||
|
||||
$this->database->createDatabase($tenant, $afterCreating);
|
||||
|
|
@ -321,6 +330,11 @@ class TenantManager
|
|||
return $this->app['config']['tenancy.migrate_after_creation'] ?? false;
|
||||
}
|
||||
|
||||
public function shouldSeedAfterMigration(): bool
|
||||
{
|
||||
return $this->shouldMigrateAfterCreation() && $this->app['config']['tenancy.seed_after_migration'] ?? false;
|
||||
}
|
||||
|
||||
public function databaseCreationQueued(): bool
|
||||
{
|
||||
return $this->app['config']['tenancy.queue_database_creation'] ?? false;
|
||||
|
|
@ -331,6 +345,11 @@ class TenantManager
|
|||
return $this->app['config']['tenancy.delete_database_after_tenant_deletion'] ?? false;
|
||||
}
|
||||
|
||||
public function getSeederParameters()
|
||||
{
|
||||
return $this->app['config']['tenancy.seeder_parameters'] ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an event listener.
|
||||
*
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue