mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 10:14:04 +00:00
parent
ee73ac5dce
commit
7ebe9450fb
4 changed files with 33 additions and 34 deletions
|
|
@ -92,7 +92,9 @@ return [
|
|||
'queue_database_creation' => false,
|
||||
'migrate_after_creation' => false, // run migrations after creating a tenant
|
||||
'seed_after_migration' => false, // should the seeder run after automatic migration
|
||||
'seeder_class' => 'DatabaseSeeder', // root seeder class to run after automatic migrations, eg: 'DatabaseSeeder'
|
||||
'seeder_parameters' => [
|
||||
'--class' => 'DatabaseSeeder', // root seeder class to run after automatic migrations, eg: 'DatabaseSeeder'
|
||||
],
|
||||
'queue_database_deletion' => false,
|
||||
'delete_database_after_tenant_deletion' => false, // delete the tenant's database after deleting the tenant
|
||||
'unique_id_generator' => Stancl\Tenancy\UniqueIDGenerators\UUIDGenerator::class,
|
||||
|
|
|
|||
|
|
@ -20,12 +20,12 @@ class QueuedTenantDatabaseSeeder implements ShouldQueue
|
|||
protected $tenantId;
|
||||
|
||||
/** @var array */
|
||||
protected $seederClassParameter = [];
|
||||
protected $seederParameters = [];
|
||||
|
||||
public function __construct(Tenant $tenant, string $seederClassName)
|
||||
public function __construct(Tenant $tenant, $seederParameters = [])
|
||||
{
|
||||
$this->tenantId = $tenant->id;
|
||||
$this->seederClassParameter = ! empty($seederClassName) ? ['--class' => $seederClassName] : [];
|
||||
$this->seederParameters = $seederParameters;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -37,6 +37,6 @@ class QueuedTenantDatabaseSeeder implements ShouldQueue
|
|||
{
|
||||
Artisan::call('tenants:seed', [
|
||||
'--tenants' => [$this->tenantId],
|
||||
] + $this->seederClassParameter);
|
||||
] + $this->seederParameters);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -68,30 +68,23 @@ class TenantManager
|
|||
$afterCreating = [];
|
||||
|
||||
if ($this->shouldMigrateAfterCreation()) {
|
||||
$afterCreating = array_merge($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()) {
|
||||
$seederClassName = $this->getSeederRootClass();
|
||||
$seederClassParameter = ! empty($seederClassName) ? ['--class' => $seederClassName] : [];
|
||||
|
||||
$afterCreating = array_merge($afterCreating, $this->databaseCreationQueued() ? [
|
||||
new QueuedTenantDatabaseSeeder($tenant, $seederClassName),
|
||||
] : [
|
||||
function () use ($tenant, $seederClassParameter) {
|
||||
$this->artisan->call('tenants:seed', [
|
||||
'--tenants' => [$tenant['id']],
|
||||
] + $seederClassParameter);
|
||||
},
|
||||
]);
|
||||
}
|
||||
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);
|
||||
|
|
@ -352,9 +345,9 @@ class TenantManager
|
|||
return $this->app['config']['tenancy.delete_database_after_tenant_deletion'] ?? false;
|
||||
}
|
||||
|
||||
public function getSeederRootClass()
|
||||
public function getSeederParameters()
|
||||
{
|
||||
return $this->app['config']['tenancy.seeder_class'] ?? null;
|
||||
return $this->app['config']['tenancy.seeder_parameters'] ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ use Stancl\Tenancy\Jobs\QueuedTenantDatabaseMigrator;
|
|||
use Stancl\Tenancy\Jobs\QueuedTenantDatabaseSeeder;
|
||||
use Stancl\Tenancy\Tenant;
|
||||
use Stancl\Tenancy\TenantManager;
|
||||
use Stancl\Tenancy\Tests\Etc\ExampleSeeder;
|
||||
|
||||
class TenantManagerTest extends TestCase
|
||||
{
|
||||
|
|
@ -242,19 +243,22 @@ class TenantManagerTest extends TestCase
|
|||
/** @test */
|
||||
public function automatic_seeding_works()
|
||||
{
|
||||
config(['tenancy.migrate_after_creation' => true]);
|
||||
|
||||
$tenant = Tenant::create(['foo.localhost']);
|
||||
tenancy()->initialize($tenant);
|
||||
$this->assertFalse(\Schema::hasTable('users'));
|
||||
$this->assertSame(0, \DB::table('users')->count());
|
||||
|
||||
config([
|
||||
'tenancy.migrate_after_creation' => true,
|
||||
'tenancy.seed_after_migration' => true,
|
||||
'tenancy.seeder_class' => \Stancl\Tenancy\Tests\Etc\ExampleSeeder::class,
|
||||
'tenancy.seeder_parameters' => [
|
||||
'--class' => ExampleSeeder::class,
|
||||
],
|
||||
]);
|
||||
|
||||
$tenant2 = Tenant::create(['bar.localhost']);
|
||||
tenancy()->initialize($tenant2);
|
||||
$this->assertTrue(\Schema::hasTable('users'));
|
||||
$this->assertCount(1, \DB::select('select * from users'));
|
||||
$this->assertSame(1, \DB::table('users')->count());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
|
|
@ -293,7 +297,7 @@ class TenantManagerTest extends TestCase
|
|||
}
|
||||
|
||||
/** @test */
|
||||
public function auto_seeding_is_queued_when_enabled()
|
||||
public function autoseeding_is_queued_when_db_creation_is_queued()
|
||||
{
|
||||
Queue::fake();
|
||||
|
||||
|
|
@ -303,7 +307,7 @@ class TenantManagerTest extends TestCase
|
|||
'tenancy.seed_after_migration' => true,
|
||||
]);
|
||||
|
||||
$tenant = Tenant::new()->save();
|
||||
Tenant::new()->save();
|
||||
|
||||
Queue::assertPushedWithChain(QueuedTenantDatabaseCreator::class, [
|
||||
QueuedTenantDatabaseMigrator::class,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue