mirror of
https://github.com/archtechx/tenancy.git
synced 2026-05-06 16:24:03 +00:00
Merge branch 'pending-improvements' into boilerplate-dev
This commit is contained in:
commit
2dfbbef0f3
5 changed files with 196 additions and 46 deletions
|
|
@ -18,7 +18,7 @@ trait HasTenantOptions
|
||||||
{
|
{
|
||||||
return array_merge([
|
return array_merge([
|
||||||
new InputOption('tenants', null, InputOption::VALUE_IS_ARRAY|InputOption::VALUE_OPTIONAL, 'The tenants to run this command for. Leave empty for all tenants', null),
|
new InputOption('tenants', null, InputOption::VALUE_IS_ARRAY|InputOption::VALUE_OPTIONAL, 'The tenants to run this command for. Leave empty for all tenants', null),
|
||||||
new InputOption('with-pending', null, InputOption::VALUE_NONE, 'Include pending tenants in query'), // todo@pending should we also offer without-pending? if we add this, mention in docs
|
new InputOption('with-pending', null, InputOption::VALUE_OPTIONAL, 'Include pending tenants in query if true/1, exclude if false/0. Defaults to the tenancy.pending.include_in_queries config value.'), // todo@pending mention in docs
|
||||||
], parent::getOptions());
|
], parent::getOptions());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -43,7 +43,11 @@ trait HasTenantOptions
|
||||||
$query->whereIn(tenancy()->model()->getTenantKeyName(), $this->option('tenants'));
|
$query->whereIn(tenancy()->model()->getTenantKeyName(), $this->option('tenants'));
|
||||||
})
|
})
|
||||||
->when(tenancy()->model()::hasGlobalScope(PendingScope::class), function ($query) {
|
->when(tenancy()->model()::hasGlobalScope(PendingScope::class), function ($query) {
|
||||||
$query->withPending(config('tenancy.pending.include_in_queries') ?: $this->option('with-pending'));
|
$includePending = $this->input->hasParameterOption('--with-pending')
|
||||||
|
? filter_var($this->option('with-pending') ?? true, FILTER_VALIDATE_BOOLEAN)
|
||||||
|
: config('tenancy.pending.include_in_queries');
|
||||||
|
|
||||||
|
$query->withPending($includePending);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,18 @@ trait HasPending
|
||||||
public static function bootHasPending(): void
|
public static function bootHasPending(): void
|
||||||
{
|
{
|
||||||
static::addGlobalScope(new PendingScope());
|
static::addGlobalScope(new PendingScope());
|
||||||
|
|
||||||
|
static::creating(function (self $tenant): void {
|
||||||
|
if ($tenant->pending()) {
|
||||||
|
event(new CreatingPendingTenant($tenant));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
static::created(function (self $tenant): void {
|
||||||
|
if ($tenant->pending()) {
|
||||||
|
event(new PendingTenantCreated($tenant));
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Initialize the trait. */
|
/** Initialize the trait. */
|
||||||
|
|
@ -49,22 +61,11 @@ trait HasPending
|
||||||
*/
|
*/
|
||||||
public static function createPending(array $attributes = []): Model&Tenant
|
public static function createPending(array $attributes = []): Model&Tenant
|
||||||
{
|
{
|
||||||
$tenant = null;
|
return static::create(array_merge(
|
||||||
|
static::getPendingAttributes($attributes),
|
||||||
try {
|
$attributes,
|
||||||
$tenant = static::create(array_merge(static::getPendingAttributes($attributes), $attributes));
|
['pending_since' => now()->timestamp],
|
||||||
event(new CreatingPendingTenant($tenant));
|
));
|
||||||
} finally {
|
|
||||||
// Update the pending_since value only after the tenant is created so it's
|
|
||||||
// not marked as pending until after migrations, seeders, etc are run.
|
|
||||||
$tenant?->update([
|
|
||||||
'pending_since' => now()->timestamp,
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
event(new PendingTenantCreated($tenant));
|
|
||||||
|
|
||||||
return $tenant;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,12 @@ class MigrateDatabase implements ShouldQueue
|
||||||
{
|
{
|
||||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Should pending tenants be included while migrating,
|
||||||
|
* regardless of the tenancy.pending.include_in_queries config value.
|
||||||
|
*/
|
||||||
|
public static bool $includePending = true;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
protected TenantWithDatabase&Model $tenant,
|
protected TenantWithDatabase&Model $tenant,
|
||||||
) {}
|
) {}
|
||||||
|
|
@ -25,6 +31,7 @@ class MigrateDatabase implements ShouldQueue
|
||||||
{
|
{
|
||||||
Artisan::call('tenants:migrate', [
|
Artisan::call('tenants:migrate', [
|
||||||
'--tenants' => [$this->tenant->getTenantKey()],
|
'--tenants' => [$this->tenant->getTenantKey()],
|
||||||
|
'--with-pending' => static::$includePending,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,12 @@ class SeedDatabase implements ShouldQueue
|
||||||
{
|
{
|
||||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Should pending tenants be included while seeding,
|
||||||
|
* regardless of the tenancy.pending.include_in_queries config value.
|
||||||
|
*/
|
||||||
|
public static bool $includePending = true;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
protected TenantWithDatabase&Model $tenant,
|
protected TenantWithDatabase&Model $tenant,
|
||||||
) {}
|
) {}
|
||||||
|
|
@ -25,6 +31,7 @@ class SeedDatabase implements ShouldQueue
|
||||||
{
|
{
|
||||||
Artisan::call('tenants:seed', [
|
Artisan::call('tenants:seed', [
|
||||||
'--tenants' => [$this->tenant->getTenantKey()],
|
'--tenants' => [$this->tenant->getTenantKey()],
|
||||||
|
'--with-pending' => static::$includePending,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,10 +16,25 @@ use Stancl\Tenancy\Events\PendingTenantPulled;
|
||||||
use Stancl\Tenancy\Events\PullingPendingTenant;
|
use Stancl\Tenancy\Events\PullingPendingTenant;
|
||||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||||
use function Stancl\Tenancy\Tests\pest;
|
use function Stancl\Tenancy\Tests\pest;
|
||||||
|
use Stancl\Tenancy\Events\TenantCreated;
|
||||||
|
use Stancl\JobPipeline\JobPipeline;
|
||||||
|
use Stancl\Tenancy\Jobs\CreateDatabase;
|
||||||
|
use Stancl\Tenancy\Jobs\MigrateDatabase;
|
||||||
|
use Stancl\Tenancy\Jobs\SeedDatabase;
|
||||||
|
use Stancl\Tenancy\Tests\Etc\User;
|
||||||
|
use Stancl\Tenancy\Tests\Etc\TestSeeder;
|
||||||
|
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
|
||||||
|
use Stancl\Tenancy\Events\TenancyInitialized;
|
||||||
|
use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
||||||
|
use Stancl\Tenancy\Events\TenancyEnded;
|
||||||
|
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
||||||
|
|
||||||
beforeEach($cleanup = function () {
|
beforeEach($cleanup = function () {
|
||||||
Tenant::$extraCustomColumns = [];
|
Tenant::$extraCustomColumns = [];
|
||||||
Tenant::$getPendingAttributesUsing = null;
|
Tenant::$getPendingAttributesUsing = null;
|
||||||
|
|
||||||
|
MigrateDatabase::$includePending = true;
|
||||||
|
SeedDatabase::$includePending = true;
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach($cleanup);
|
afterEach($cleanup);
|
||||||
|
|
@ -154,8 +169,8 @@ test('pending events are dispatched', function () {
|
||||||
Event::assertDispatched(PendingTenantPulled::class);
|
Event::assertDispatched(PendingTenantPulled::class);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('commands do not run for pending tenants if tenancy.pending.include_in_queries is false and the with pending option does not get passed', function() {
|
test('commands include tenants based on the include_in_queries config when --with-pending is not passed', function (bool $includeInQueries) {
|
||||||
config(['tenancy.pending.include_in_queries' => false]);
|
config(['tenancy.pending.include_in_queries' => $includeInQueries]);
|
||||||
|
|
||||||
$tenants = collect([
|
$tenants = collect([
|
||||||
Tenant::create(),
|
Tenant::create(),
|
||||||
|
|
@ -164,21 +179,21 @@ test('commands do not run for pending tenants if tenancy.pending.include_in_quer
|
||||||
Tenant::createPending(),
|
Tenant::createPending(),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
pest()->artisan('tenants:migrate --with-pending');
|
$command = pest()->artisan("tenants:run 'bar testing testing@test.test password foo'");
|
||||||
|
|
||||||
$artisan = pest()->artisan("tenants:run 'foo foo --b=bar --c=xyz'");
|
$tenants->each(function ($tenant) use ($command, $includeInQueries) {
|
||||||
|
if ($tenant->pending() && ! $includeInQueries) {
|
||||||
$pendingTenants = $tenants->filter->pending();
|
$command->doesntExpectOutputToContain("Tenant: {$tenant->getTenantKey()}");
|
||||||
$readyTenants = $tenants->reject->pending();
|
} else {
|
||||||
|
$command->expectsOutputToContain("Tenant: {$tenant->getTenantKey()}");
|
||||||
$pendingTenants->each(fn ($tenant) => $artisan->doesntExpectOutputToContain("Tenant: {$tenant->getTenantKey()}"));
|
}
|
||||||
$readyTenants->each(fn ($tenant) => $artisan->expectsOutputToContain("Tenant: {$tenant->getTenantKey()}"));
|
|
||||||
|
|
||||||
$artisan->assertExitCode(0);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('commands run for pending tenants too if tenancy.pending.include_in_queries is true', function() {
|
$command->assertSuccessful();
|
||||||
config(['tenancy.pending.include_in_queries' => true]);
|
})->with([true, false]);
|
||||||
|
|
||||||
|
test('commands include pending tenants when truthy --with-pending is passed', function (bool $includeInQueries) {
|
||||||
|
config(['tenancy.pending.include_in_queries' => $includeInQueries]);
|
||||||
|
|
||||||
$tenants = collect([
|
$tenants = collect([
|
||||||
Tenant::create(),
|
Tenant::create(),
|
||||||
|
|
@ -187,17 +202,22 @@ test('commands run for pending tenants too if tenancy.pending.include_in_queries
|
||||||
Tenant::createPending(),
|
Tenant::createPending(),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
pest()->artisan('tenants:migrate --with-pending');
|
foreach ([
|
||||||
|
'--with-pending',
|
||||||
|
'--with-pending=true',
|
||||||
|
'--with-pending=1'
|
||||||
|
] as $option) {
|
||||||
|
$command = pest()->artisan("tenants:run 'bar testing testing@test.test password foo' {$option}");
|
||||||
|
|
||||||
$artisan = pest()->artisan("tenants:run 'foo foo --b=bar --c=xyz'");
|
// Pending tenants are included regardless of tenancy.pending.include_in_queries
|
||||||
|
$tenants->each(fn ($tenant) => $command->expectsOutputToContain("Tenant: {$tenant->getTenantKey()}"));
|
||||||
|
|
||||||
$tenants->each(fn ($tenant) => $artisan->expectsOutputToContain("Tenant: {$tenant->getTenantKey()}"));
|
$command->assertSuccessful();
|
||||||
|
}
|
||||||
|
})->with([true, false]);
|
||||||
|
|
||||||
$artisan->assertExitCode(0);
|
test('commands exclude pending tenants when falsy --with-pending is passed', function (bool $includeInQueries) {
|
||||||
});
|
config(['tenancy.pending.include_in_queries' => $includeInQueries]);
|
||||||
|
|
||||||
test('commands run for pending tenants too if the with pending option is passed', function() {
|
|
||||||
config(['tenancy.pending.include_in_queries' => false]);
|
|
||||||
|
|
||||||
$tenants = collect([
|
$tenants = collect([
|
||||||
Tenant::create(),
|
Tenant::create(),
|
||||||
|
|
@ -206,15 +226,26 @@ test('commands run for pending tenants too if the with pending option is passed'
|
||||||
Tenant::createPending(),
|
Tenant::createPending(),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
pest()->artisan('tenants:migrate --with-pending');
|
foreach ([
|
||||||
|
'--with-pending=false',
|
||||||
|
'--with-pending=0',
|
||||||
|
'--with-pending=foo' // Invalid values are treated as false
|
||||||
|
] as $option) {
|
||||||
|
$command = pest()->artisan("tenants:run 'bar testing testing@test.test password foo' {$option}");
|
||||||
|
|
||||||
$artisan = pest()->artisan("tenants:run 'foo foo --b=bar --c=xyz' --with-pending");
|
$tenants->each(function ($tenant) use ($command) {
|
||||||
|
if ($tenant->pending()) {
|
||||||
$tenants->each(fn ($tenant) => $artisan->expectsOutputToContain("Tenant: {$tenant->getTenantKey()}"));
|
// Pending tenants are excluded regardless of tenancy.pending.include_in_queries
|
||||||
|
$command->doesntExpectOutputToContain("Tenant: {$tenant->getTenantKey()}");
|
||||||
$artisan->assertExitCode(0);
|
} else {
|
||||||
|
$command->expectsOutputToContain("Tenant: {$tenant->getTenantKey()}");
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
$command->assertSuccessful();
|
||||||
|
}
|
||||||
|
})->with([true, false]);
|
||||||
|
|
||||||
test('pending tenants can have default attributes for non-nullable columns', function (bool $withPendingAttributes) {
|
test('pending tenants can have default attributes for non-nullable columns', function (bool $withPendingAttributes) {
|
||||||
Schema::table('tenants', function (Blueprint $table) {
|
Schema::table('tenants', function (Blueprint $table) {
|
||||||
$table->string('slug')->unique();
|
$table->string('slug')->unique();
|
||||||
|
|
@ -236,3 +267,103 @@ test('pending tenants can have default attributes for non-nullable columns', fun
|
||||||
else
|
else
|
||||||
expect($fn)->toThrow(QueryException::class);
|
expect($fn)->toThrow(QueryException::class);
|
||||||
})->with([true, false]);
|
})->with([true, false]);
|
||||||
|
|
||||||
|
test('pending tenant databases can be migrated using a job unless configured otherwise', function (bool $includeInQueries, bool $migrateWithPending) {
|
||||||
|
config([
|
||||||
|
'tenancy.bootstrappers' => [DatabaseTenancyBootstrapper::class],
|
||||||
|
'tenancy.pending.include_in_queries' => $includeInQueries,
|
||||||
|
]);
|
||||||
|
|
||||||
|
MigrateDatabase::$includePending = $migrateWithPending;
|
||||||
|
|
||||||
|
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
|
||||||
|
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
|
||||||
|
Event::listen(TenantCreated::class, JobPipeline::make([
|
||||||
|
CreateDatabase::class,
|
||||||
|
MigrateDatabase::class,
|
||||||
|
])->send(function (TenantCreated $event) {
|
||||||
|
return $event->tenant;
|
||||||
|
})->toListener());
|
||||||
|
|
||||||
|
$pendingTenant = Tenant::createPending();
|
||||||
|
|
||||||
|
expect(Schema::hasTable('users'))->toBeFalse();
|
||||||
|
|
||||||
|
tenancy()->initialize($pendingTenant);
|
||||||
|
|
||||||
|
// MigrateDatabase includes/excludes pending tenants based on its $includePending property,
|
||||||
|
// regardless of the tenancy.pending.include_in_queries config.
|
||||||
|
expect(Schema::hasTable('users'))->toBe($migrateWithPending);
|
||||||
|
})->with([
|
||||||
|
'include pending in queries' => [true],
|
||||||
|
'exclude pending from queries' => [false],
|
||||||
|
])->with([
|
||||||
|
'migrate with pending' => [true],
|
||||||
|
'migrate without pending' => [false],
|
||||||
|
]);
|
||||||
|
|
||||||
|
test('pending tenant databases can be seeded using a job unless configured otherwise', function ($includeInQueries, $seedWithPending) {
|
||||||
|
config([
|
||||||
|
'tenancy.bootstrappers' => [DatabaseTenancyBootstrapper::class],
|
||||||
|
'tenancy.pending.include_in_queries' => $includeInQueries,
|
||||||
|
'tenancy.seeder_parameters.--class' => TestSeeder::class,
|
||||||
|
]);
|
||||||
|
|
||||||
|
MigrateDatabase::$includePending = true;
|
||||||
|
SeedDatabase::$includePending = $seedWithPending;
|
||||||
|
|
||||||
|
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
|
||||||
|
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
|
||||||
|
Event::listen(TenantCreated::class, JobPipeline::make([
|
||||||
|
CreateDatabase::class,
|
||||||
|
MigrateDatabase::class,
|
||||||
|
SeedDatabase::class,
|
||||||
|
])->send(function (TenantCreated $event) {
|
||||||
|
return $event->tenant;
|
||||||
|
})->toListener());
|
||||||
|
|
||||||
|
$pendingTenant = Tenant::createPending();
|
||||||
|
|
||||||
|
tenancy()->initialize($pendingTenant);
|
||||||
|
|
||||||
|
// SeedDatabase includes/excludes pending tenants based on its $includePending property,
|
||||||
|
// regardless of the tenancy.pending.include_in_queries config.
|
||||||
|
expect(User::where('email', 'seeded@user')->exists())->toBe($seedWithPending);
|
||||||
|
})->with([
|
||||||
|
'include pending in queries' => [true],
|
||||||
|
'exclude pending from queries' => [false],
|
||||||
|
])->with([
|
||||||
|
'seed with pending' => [true],
|
||||||
|
'seed without pending' => [false],
|
||||||
|
]);
|
||||||
|
|
||||||
|
test('jobs that run before tenants get fully created recognize pending tenants', function () {
|
||||||
|
config([
|
||||||
|
'tenancy.bootstrappers' => [DatabaseTenancyBootstrapper::class],
|
||||||
|
]);
|
||||||
|
|
||||||
|
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
|
||||||
|
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
|
||||||
|
Event::listen(TenantCreated::class, JobPipeline::make([
|
||||||
|
CreateDatabase::class,
|
||||||
|
PendingTenantJob::class,
|
||||||
|
])->send(function (TenantCreated $event) {
|
||||||
|
return $event->tenant;
|
||||||
|
})->toListener());
|
||||||
|
|
||||||
|
Tenant::createPending();
|
||||||
|
|
||||||
|
expect(app('tenant_is_pending'))->toBeTrue();
|
||||||
|
});
|
||||||
|
|
||||||
|
class PendingTenantJob
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
public Tenant $tenant,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
app()->instance('tenant_is_pending', $this->tenant->pending());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue