From 0a37eb487add7a59b070abf7a9042f1b20b8281e Mon Sep 17 00:00:00 2001 From: "j.stein" Date: Sat, 12 Feb 2022 15:30:50 +0100 Subject: [PATCH] runForMultiple can scope pending tenants --- src/Commands/Migrate.php | 2 +- src/Commands/MigrateFresh.php | 2 +- src/Commands/Rollback.php | 2 +- src/Commands/Run.php | 2 +- src/Commands/Seed.php | 2 +- src/Concerns/HasATenantsOption.php | 6 ++++++ src/Database/TenantCollection.php | 4 ++-- src/Tenancy.php | 14 ++++++++++---- tests/PendingTenantsTest.php | 24 ++++++++++++++++++++++++ 9 files changed, 47 insertions(+), 11 deletions(-) diff --git a/src/Commands/Migrate.php b/src/Commands/Migrate.php index bf92dfcd..48d73a43 100644 --- a/src/Commands/Migrate.php +++ b/src/Commands/Migrate.php @@ -63,6 +63,6 @@ class Migrate extends MigrateCommand parent::handle(); event(new DatabaseMigrated($tenant)); - }); + }, $this->withPending()); } } diff --git a/src/Commands/MigrateFresh.php b/src/Commands/MigrateFresh.php index f50e2f5f..453bb304 100644 --- a/src/Commands/MigrateFresh.php +++ b/src/Commands/MigrateFresh.php @@ -45,7 +45,7 @@ final class MigrateFresh extends Command '--tenants' => [$tenant->getTenantKey()], '--force' => true, ]); - }); + }, $this->withPending()); $this->info('Done.'); } diff --git a/src/Commands/Rollback.php b/src/Commands/Rollback.php index 081872c8..f88df4f4 100644 --- a/src/Commands/Rollback.php +++ b/src/Commands/Rollback.php @@ -61,6 +61,6 @@ class Rollback extends RollbackCommand parent::handle(); event(new DatabaseRolledBack($tenant)); - }); + }, $this->withPending()); } } diff --git a/src/Commands/Run.php b/src/Commands/Run.php index aa518d7a..4e622c6b 100644 --- a/src/Commands/Run.php +++ b/src/Commands/Run.php @@ -52,6 +52,6 @@ class Run extends Command // Run command $this->call($this->argument('commandname'), array_merge($arguments, $options)); - }); + }, $this->withPending()); } } diff --git a/src/Commands/Seed.php b/src/Commands/Seed.php index dc97ae71..11cdaf48 100644 --- a/src/Commands/Seed.php +++ b/src/Commands/Seed.php @@ -59,6 +59,6 @@ class Seed extends SeedCommand parent::handle(); event(new DatabaseSeeded($tenant)); - }); + }, $this->withPending()); } } diff --git a/src/Concerns/HasATenantsOption.php b/src/Concerns/HasATenantsOption.php index a2b94ac5..055c6cd4 100644 --- a/src/Concerns/HasATenantsOption.php +++ b/src/Concerns/HasATenantsOption.php @@ -13,6 +13,7 @@ trait HasATenantsOption { return array_merge([ ['tenants', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, '', null], + ['with-pending', null, InputOption::VALUE_NONE, 'include pending tenants in query', null], ], parent::getOptions()); } @@ -26,6 +27,11 @@ trait HasATenantsOption ->cursor(); } + protected function withPending(): bool + { + return $this->option('with-pending'); + } + public function __construct() { parent::__construct(); diff --git a/src/Database/TenantCollection.php b/src/Database/TenantCollection.php index ba3a8fab..c4d09784 100644 --- a/src/Database/TenantCollection.php +++ b/src/Database/TenantCollection.php @@ -16,9 +16,9 @@ use Stancl\Tenancy\Contracts\Tenant; */ class TenantCollection extends Collection { - public function runForEach(callable $callable): self + public function runForEach(callable $callable, bool $withPending = null): self { - tenancy()->runForMultiple($this->items, $callable); + tenancy()->runForMultiple($this->items, $callable, $withPending); return $this; } diff --git a/src/Tenancy.php b/src/Tenancy.php index 30f138e3..4051294f 100644 --- a/src/Tenancy.php +++ b/src/Tenancy.php @@ -9,6 +9,7 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Traits\Macroable; use Stancl\Tenancy\Contracts\TenancyBootstrapper; use Stancl\Tenancy\Contracts\Tenant; +use Stancl\Tenancy\Database\Concerns\PendingScope; use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedById; class Tenancy @@ -135,10 +136,15 @@ class Tenancy * @param callable $callback * @return void */ - public function runForMultiple($tenants, callable $callback) + public function runForMultiple($tenants, callable $callback, bool $withPending = null) { + $query = $this->model()->newQuery(); + + if (is_bool($withPending) && $this->model()::hasGlobalScope(PendingScope::class)){ + $query->withPending($withPending); + } // Convert null to all tenants - $tenants = is_null($tenants) ? $this->model()->cursor() : $tenants; + $tenants = is_null($tenants) ? $query->cursor() : $tenants; // Convert incrementing int ids to strings $tenants = is_int($tenants) ? (string) $tenants : $tenants; @@ -146,8 +152,8 @@ class Tenancy // Wrap string in array $tenants = is_string($tenants) ? [$tenants] : $tenants; - // Use all tenants if $tenants is falsey - $tenants = $tenants ?: $this->model()->cursor(); + // Use all tenants if $tenants is false + $tenants = $tenants ?: $query->cursor(); $originalTenant = $this->tenant; diff --git a/tests/PendingTenantsTest.php b/tests/PendingTenantsTest.php index ff26e9b9..9dc5c097 100644 --- a/tests/PendingTenantsTest.php +++ b/tests/PendingTenantsTest.php @@ -169,4 +169,28 @@ class PendingTenantsTest extends TestCase Event::assertDispatched(PullingPendingTenant::class); Event::assertDispatched(PendingTenantPulled::class); } + + /** @test */ + public function tenancy_run_for_multiple_can_scope_pending_tenants() + { + config(['tenancy.pending.include_in_queries' => false]); + + Tenant::createPending(); + Tenant::create(); + + $executedCount = 0; + tenancy()->runForMultiple([], function () use (&$executedCount){ + $executedCount++; + }, false); + + self::assertEquals(1, $executedCount); + + $executedCount = 0; + + tenancy()->runForMultiple([], function () use (&$executedCount){ + $executedCount++; + }, true); + + self::assertEquals(2, $executedCount); + } }