From 85bdbd57f7b215ce72138124579aa223a95469ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Mon, 25 Nov 2024 04:44:39 +0100 Subject: [PATCH] Fix pullFromPendingPool() behavior (#70) --- src/Bootstrappers/JobBatchBootstrapper.php | 1 + src/Database/Concerns/HasPending.php | 17 ++++++----------- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/src/Bootstrappers/JobBatchBootstrapper.php b/src/Bootstrappers/JobBatchBootstrapper.php index 3ccf3b97..87db4869 100644 --- a/src/Bootstrappers/JobBatchBootstrapper.php +++ b/src/Bootstrappers/JobBatchBootstrapper.php @@ -27,6 +27,7 @@ class JobBatchBootstrapper implements TenancyBootstrapper public function bootstrap(Tenant $tenant): void { + // todo@revisit // Update batch repository connection to use the tenant connection $this->previousConnection = $this->batchRepository->getConnection(); $this->batchRepository->setConnection($this->databaseManager->connection('tenant')); diff --git a/src/Database/Concerns/HasPending.php b/src/Database/Concerns/HasPending.php index 83cf0cf2..ce0b1a37 100644 --- a/src/Database/Concerns/HasPending.php +++ b/src/Database/Concerns/HasPending.php @@ -73,20 +73,15 @@ trait HasPending } /** Try to pull a tenant from the pool of pending tenants. */ - public static function pullPendingFromPool(bool $firstOrCreate = false, array $attributes = []): ?Tenant + public static function pullPendingFromPool(bool $firstOrCreate = true, array $attributes = []): ?Tenant { - if (! static::onlyPending()->exists()) { - if (! $firstOrCreate) { - return null; - } - - static::createPending($attributes); - } - - // A pending tenant is surely available at this point - /** @var Model&Tenant $tenant */ + /** @var (Model&Tenant)|null $tenant */ $tenant = static::onlyPending()->first(); + if ($tenant === null) { + return $firstOrCreate ? static::create($attributes) : null; + } + event(new PullingPendingTenant($tenant)); $tenant->update(array_merge($attributes, [