From eff41553d711d0d3d3ee57df7b877efe16c6fc14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Fri, 27 Dec 2024 21:33:44 +0100 Subject: [PATCH] deprecate JobBatchBootstrapper --- assets/config.php | 3 +- src/Bootstrappers/JobBatchBootstrapper.php | 33 +++++++--------- tests/BatchTest.php | 46 ---------------------- 3 files changed, 15 insertions(+), 67 deletions(-) delete mode 100644 tests/BatchTest.php diff --git a/assets/config.php b/assets/config.php index c97bdd67..a16a4201 100644 --- a/assets/config.php +++ b/assets/config.php @@ -148,9 +148,8 @@ return [ Bootstrappers\QueueTenancyBootstrapper::class, // Bootstrappers\RedisTenancyBootstrapper::class, // Note: phpredis is needed - // Support for edge cases + // Adds support for the database session driver Bootstrappers\DatabaseSessionBootstrapper::class, - Bootstrappers\JobBatchBootstrapper::class, // Configurable bootstrappers // Bootstrappers\RootUrlBootstrapper::class, diff --git a/src/Bootstrappers/JobBatchBootstrapper.php b/src/Bootstrappers/JobBatchBootstrapper.php index 87db4869..db4d8157 100644 --- a/src/Bootstrappers/JobBatchBootstrapper.php +++ b/src/Bootstrappers/JobBatchBootstrapper.php @@ -4,41 +4,36 @@ declare(strict_types=1); namespace Stancl\Tenancy\Bootstrappers; -use Illuminate\Bus\DatabaseBatchRepository; -use Illuminate\Database\Connection; -use Illuminate\Database\DatabaseManager; +use Exception; +use Illuminate\Contracts\Foundation\Application; use Stancl\Tenancy\Contracts\TenancyBootstrapper; use Stancl\Tenancy\Contracts\Tenant; /** * Adds support for running queued tenant jobs in batches. + * + * @deprecated Doesn't seem to 1. be necessary, 2. work correctly in Laravel 11. Please don't use this bootstrapper, the class will be removed before release. */ class JobBatchBootstrapper implements TenancyBootstrapper { - /** - * The previous database connection instance. - */ - protected ?Connection $previousConnection = null; - public function __construct( - protected DatabaseBatchRepository $batchRepository, - protected DatabaseManager $databaseManager + protected Application $app, ) {} 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')); + $this->deprecatedNotice(); + } + + protected function deprecatedNotice(): void + { + if ($this->app->environment() == 'local' && $this->app->hasDebugModeEnabled()) { + throw new Exception("JobBatchBootstrapper is not supported anymore, please remove it from your tenancy config. Job batches should work out of the box in Laravel 11. If they don't, please open a bug report."); + } } public function revert(): void { - if ($this->previousConnection) { - // Replace batch repository connection with the previously replaced one - $this->batchRepository->setConnection($this->previousConnection); - $this->previousConnection = null; - } + $this->deprecatedNotice(); } } diff --git a/tests/BatchTest.php b/tests/BatchTest.php deleted file mode 100644 index 91ed3c0e..00000000 --- a/tests/BatchTest.php +++ /dev/null @@ -1,46 +0,0 @@ - [ - DatabaseTenancyBootstrapper::class, - JobBatchBootstrapper::class, - ], - ]); - - Event::listen(TenancyInitialized::class, BootstrapTenancy::class); - Event::listen(TenancyEnded::class, RevertToCentralContext::class); -}); - -test('batch repository is set to tenant connection and reverted', function () { - withTenantDatabases(); - - $tenant = Tenant::create(); - $tenant2 = Tenant::create(); - - expect(getBatchRepositoryConnectionName())->toBe('central'); - - tenancy()->initialize($tenant); - expect(getBatchRepositoryConnectionName())->toBe('tenant'); - - tenancy()->initialize($tenant2); - expect(getBatchRepositoryConnectionName())->toBe('tenant'); - - tenancy()->end(); - expect(getBatchRepositoryConnectionName())->toBe('central'); -}); - -function getBatchRepositoryConnectionName() -{ - return app(BatchRepository::class)->getConnection()->getName(); -}