From db9480f54e98b33ca967bbe3f791c314e772386b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Wed, 1 Jun 2022 15:35:39 +0200 Subject: [PATCH 01/18] exclude master from CI --- .github/workflows/ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index cc8ad9851..ed8ff9ac6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -5,9 +5,9 @@ env: on: push: - branches: [ 3.x, 2.x, master ] + branches: [ 3.x, 2.x ] pull_request: - branches: [ 3.x, 2.x, master ] + branches: [ 3.x, 2.x ] jobs: tests: From abac147c84405d9e4eb379041c646cece4fdac7e Mon Sep 17 00:00:00 2001 From: Riley Schoppa Date: Tue, 7 Jun 2022 20:51:47 -0400 Subject: [PATCH 02/18] Add batch tenancy queue bootstrapper --- assets/config.php | 1 + .../BatchTenancyBootstrapper.php | 51 +++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 src/Bootstrappers/BatchTenancyBootstrapper.php diff --git a/assets/config.php b/assets/config.php index 85592d143..1c6136646 100644 --- a/assets/config.php +++ b/assets/config.php @@ -33,6 +33,7 @@ Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper::class, Stancl\Tenancy\Bootstrappers\QueueTenancyBootstrapper::class, // Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper::class, // Note: phpredis is needed + // Stancl\Tenancy\Bootstrappers\BatchTenancyBootstrapper::class, ], /** diff --git a/src/Bootstrappers/BatchTenancyBootstrapper.php b/src/Bootstrappers/BatchTenancyBootstrapper.php new file mode 100644 index 000000000..ae920d24a --- /dev/null +++ b/src/Bootstrappers/BatchTenancyBootstrapper.php @@ -0,0 +1,51 @@ +getProperty('connection'); + $connectionProperty->setAccessible(true); + $connection = $connectionProperty->getValue($batchRepository); + + $this->previousConnection = $connection; + + $connectionProperty->setValue($batchRepository, DB::connection('tenant')); + } + } + + public function revert() + { + if ($this->previousConnection) { + /** + * Access the resolved batch repository instance and replace its connection with the previously replaced one + */ + $batchRepository = app(BatchRepository::class); + + $batchRepositoryReflection = new ReflectionClass($batchRepository); + $connectionProperty = $batchRepositoryReflection->getProperty('connection'); + $connectionProperty->setAccessible(true); + + $connectionProperty->setValue($batchRepository, $this->previousConnection); + $this->previousConnection = null; + } + } +} \ No newline at end of file From b0876ae8d813761c7a988316a04cc51bd4338624 Mon Sep 17 00:00:00 2001 From: Riley Schoppa Date: Tue, 7 Jun 2022 22:02:42 -0400 Subject: [PATCH 03/18] add test case --- tests/BatchTest.php | 66 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 tests/BatchTest.php diff --git a/tests/BatchTest.php b/tests/BatchTest.php new file mode 100644 index 000000000..154822c16 --- /dev/null +++ b/tests/BatchTest.php @@ -0,0 +1,66 @@ +app->singleton(BatchTenancyBootstrapper::class); + + config([ + 'tenancy.bootstrappers' => [ + DatabaseTenancyBootstrapper::class, + BatchTenancyBootstrapper::class, + ], + ]); + + Event::listen(TenancyInitialized::class, BootstrapTenancy::class); + Event::listen(TenancyEnded::class, RevertToCentralContext::class); + } + + /** @test */ + public function batch_repository_is_set_to_tenant_connection_and_reverted() + { + $tenant = Tenant::create(); + + $this->assertEquals('central', $this->getBatchRepositoryConnectionName(), 'Expected initial connection to be central'); + + tenancy()->initialize($tenant); + + $this->assertEquals('tenant', $this->getBatchRepositoryConnectionName(), 'Expected tenant connection to be tenant'); + + tenancy()->end(); + + $this->assertEquals('central', $this->getBatchRepositoryConnectionName(), 'Expected the reverted connection to be central'); + } + + + private function getBatchRepositoryConnectionName(): string + { + $batchRepository = app(BatchRepository::class); + + $batchRepositoryReflection = new ReflectionClass($batchRepository); + $connectionProperty = $batchRepositoryReflection->getProperty('connection'); + $connectionProperty->setAccessible(true); + $connection = $connectionProperty->getValue($batchRepository); + + return $connection->getName(); + } + +} From abd55556e2075fb0761ca3ef7d3c0e3506561917 Mon Sep 17 00:00:00 2001 From: Riley Schoppa Date: Mon, 13 Jun 2022 21:09:40 -0400 Subject: [PATCH 04/18] skip tests for old versions --- tests/BatchTest.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/BatchTest.php b/tests/BatchTest.php index 154822c16..66ebaec50 100644 --- a/tests/BatchTest.php +++ b/tests/BatchTest.php @@ -6,6 +6,7 @@ use Illuminate\Bus\BatchRepository; use Illuminate\Support\Facades\Event; +use Illuminate\Support\Str; use ReflectionClass; use Stancl\Tenancy\Bootstrappers\BatchTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper; @@ -37,6 +38,10 @@ public function setUp(): void /** @test */ public function batch_repository_is_set_to_tenant_connection_and_reverted() { + if (! version_compare(app()->version(), '8.0', '>=')) { + $this->markTestSkipped('Job batches are only supported in Laravel 8+'); + } + $tenant = Tenant::create(); $this->assertEquals('central', $this->getBatchRepositoryConnectionName(), 'Expected initial connection to be central'); From 0e72c71286cae871e878299785077b7e72543314 Mon Sep 17 00:00:00 2001 From: Abrar Ahmad Date: Thu, 25 Aug 2022 16:53:03 +0500 Subject: [PATCH 05/18] variable docblocks --- src/Bootstrappers/BatchTenancyBootstrapper.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Bootstrappers/BatchTenancyBootstrapper.php b/src/Bootstrappers/BatchTenancyBootstrapper.php index ae920d24a..515a4f061 100644 --- a/src/Bootstrappers/BatchTenancyBootstrapper.php +++ b/src/Bootstrappers/BatchTenancyBootstrapper.php @@ -11,7 +11,12 @@ class BatchTenancyBootstrapper implements TenancyBootstrapper { - private $previousConnection = null; + /** + * The database previous connection instance. + * + * @var \Illuminate\Database\Connection + */ + protected $previousConnection = null; public function bootstrap(Tenant $tenant) { From cc2df5c0b67b653b07b82afa8fe2970db8ef9730 Mon Sep 17 00:00:00 2001 From: Abrar Ahmad Date: Wed, 31 Aug 2022 11:16:10 +0500 Subject: [PATCH 06/18] use Laravel's connection getter and setter --- .../BatchTenancyBootstrapper.php | 25 ++++--------------- 1 file changed, 5 insertions(+), 20 deletions(-) diff --git a/src/Bootstrappers/BatchTenancyBootstrapper.php b/src/Bootstrappers/BatchTenancyBootstrapper.php index 515a4f061..e81617aba 100644 --- a/src/Bootstrappers/BatchTenancyBootstrapper.php +++ b/src/Bootstrappers/BatchTenancyBootstrapper.php @@ -23,33 +23,18 @@ public function bootstrap(Tenant $tenant) $batchRepository = app(BatchRepository::class); if ($batchRepository instanceof DatabaseBatchRepository) { - /** - * Access the resolved batch repository instance and update its connection to use the tenant connection - */ - $batchRepositoryReflection = new ReflectionClass($batchRepository); - $connectionProperty = $batchRepositoryReflection->getProperty('connection'); - $connectionProperty->setAccessible(true); - $connection = $connectionProperty->getValue($batchRepository); - - $this->previousConnection = $connection; - - $connectionProperty->setValue($batchRepository, DB::connection('tenant')); + // Access the resolved batch repository instance and update its connection to use the tenant connection + $this->previousConnection = $batchRepository->getConnection(); + $batchRepository->setConnection(DB::connection('tenant')); } } public function revert() { if ($this->previousConnection) { - /** - * Access the resolved batch repository instance and replace its connection with the previously replaced one - */ + // Access the resolved batch repository instance and replace its connection with the previously replaced one $batchRepository = app(BatchRepository::class); - - $batchRepositoryReflection = new ReflectionClass($batchRepository); - $connectionProperty = $batchRepositoryReflection->getProperty('connection'); - $connectionProperty->setAccessible(true); - - $connectionProperty->setValue($batchRepository, $this->previousConnection); + $batchRepository->setConnection($this->previousConnection); $this->previousConnection = null; } } From cbdcec2bcddfaddcbaadda8030ba31c7b31e0db9 Mon Sep 17 00:00:00 2001 From: Abrar Ahmad Date: Wed, 31 Aug 2022 17:12:58 +0500 Subject: [PATCH 07/18] convert test to pest --- .../BatchTenancyBootstrapper.php | 7 +- tests/BatchTest.php | 73 ++++++------------- 2 files changed, 27 insertions(+), 53 deletions(-) diff --git a/src/Bootstrappers/BatchTenancyBootstrapper.php b/src/Bootstrappers/BatchTenancyBootstrapper.php index e81617aba..46143cef2 100644 --- a/src/Bootstrappers/BatchTenancyBootstrapper.php +++ b/src/Bootstrappers/BatchTenancyBootstrapper.php @@ -1,11 +1,12 @@ previousConnection) { - // Access the resolved batch repository instance and replace its connection with the previously replaced one + // Access the resolved batch repository instance and replace its connection with the previously replaced one $batchRepository = app(BatchRepository::class); $batchRepository->setConnection($this->previousConnection); $this->previousConnection = null; } } -} \ No newline at end of file +} diff --git a/tests/BatchTest.php b/tests/BatchTest.php index 66ebaec50..06f41401c 100644 --- a/tests/BatchTest.php +++ b/tests/BatchTest.php @@ -1,13 +1,7 @@ app->singleton(BatchTenancyBootstrapper::class); - - config([ - 'tenancy.bootstrappers' => [ - DatabaseTenancyBootstrapper::class, - BatchTenancyBootstrapper::class, - ], - ]); +beforeEach(function () { + $this->app->singleton(BatchTenancyBootstrapper::class); - Event::listen(TenancyInitialized::class, BootstrapTenancy::class); - Event::listen(TenancyEnded::class, RevertToCentralContext::class); - } + config([ + 'tenancy.bootstrappers' => [ + DatabaseTenancyBootstrapper::class, + BatchTenancyBootstrapper::class, + ], + ]); - /** @test */ - public function batch_repository_is_set_to_tenant_connection_and_reverted() - { - if (! version_compare(app()->version(), '8.0', '>=')) { - $this->markTestSkipped('Job batches are only supported in Laravel 8+'); - } + Event::listen(TenancyInitialized::class, BootstrapTenancy::class); + Event::listen(TenancyEnded::class, RevertToCentralContext::class); +}); - $tenant = Tenant::create(); +test('batch repository is set to tenant connection and reverted', function () { + $tenant = Tenant::create(); - $this->assertEquals('central', $this->getBatchRepositoryConnectionName(), 'Expected initial connection to be central'); + expect(getBatchRepositoryConnectionName())->toBe('central'); - tenancy()->initialize($tenant); + tenancy()->initialize($tenant); - $this->assertEquals('tenant', $this->getBatchRepositoryConnectionName(), 'Expected tenant connection to be tenant'); + expect(getBatchRepositoryConnectionName())->toBe('tenant'); - tenancy()->end(); + tenancy()->end(); - $this->assertEquals('central', $this->getBatchRepositoryConnectionName(), 'Expected the reverted connection to be central'); - } + expect(getBatchRepositoryConnectionName())->toBe('central'); +})->skip(fn() => version_compare(app()->version(), '8.0', '<'), 'Job batches are only supported in Laravel 8+'); - - private function getBatchRepositoryConnectionName(): string - { - $batchRepository = app(BatchRepository::class); - - $batchRepositoryReflection = new ReflectionClass($batchRepository); - $connectionProperty = $batchRepositoryReflection->getProperty('connection'); - $connectionProperty->setAccessible(true); - $connection = $connectionProperty->getValue($batchRepository); - - return $connection->getName(); - } - -} +function getBatchRepositoryConnectionName() +{ + return app(BatchRepository::class)->getConnection()->getName(); +} \ No newline at end of file From fafe7cf27839b8662b3b8bbf9dfbaf016e94be74 Mon Sep 17 00:00:00 2001 From: Abrar Ahmad Date: Wed, 31 Aug 2022 17:13:57 +0500 Subject: [PATCH 08/18] bottom space --- tests/BatchTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/BatchTest.php b/tests/BatchTest.php index 06f41401c..1329d7819 100644 --- a/tests/BatchTest.php +++ b/tests/BatchTest.php @@ -41,4 +41,4 @@ function getBatchRepositoryConnectionName() { return app(BatchRepository::class)->getConnection()->getName(); -} \ No newline at end of file +} From 48ed21313c9257bfb4ba5ecda1fcc5e03ee4c845 Mon Sep 17 00:00:00 2001 From: Abrar Ahmad Date: Mon, 5 Sep 2022 11:37:17 +0500 Subject: [PATCH 09/18] singleton regis in TestCase --- tests/BatchTest.php | 2 -- tests/TestCase.php | 26 +++++++++++++++++--------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/tests/BatchTest.php b/tests/BatchTest.php index 1329d7819..a401dbc79 100644 --- a/tests/BatchTest.php +++ b/tests/BatchTest.php @@ -11,8 +11,6 @@ use Stancl\Tenancy\Tests\Etc\Tenant; beforeEach(function () { - $this->app->singleton(BatchTenancyBootstrapper::class); - config([ 'tenancy.bootstrappers' => [ DatabaseTenancyBootstrapper::class, diff --git a/tests/TestCase.php b/tests/TestCase.php index 1c6c6d8ab..697c48e58 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -4,8 +4,15 @@ namespace Stancl\Tenancy\Tests; +use Dotenv\Dotenv; +use Illuminate\Foundation\Application; use Illuminate\Support\Facades\Redis; use PDO; +use Stancl\Tenancy\Bootstrappers\BatchTenancyBootstrapper; +use Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper; +use Stancl\Tenancy\Facades\GlobalCache; +use Stancl\Tenancy\Facades\Tenancy; +use Stancl\Tenancy\TenancyServiceProvider; use Stancl\Tenancy\Tests\Etc\Tenant; abstract class TestCase extends \Orchestra\Testbench\TestCase @@ -42,13 +49,13 @@ protected function setUp(): void /** * Define environment setup. * - * @param \Illuminate\Foundation\Application $app + * @param Application $app * @return void */ protected function getEnvironmentSetUp($app) { if (file_exists(__DIR__ . '/../.env')) { - \Dotenv\Dotenv::createImmutable(__DIR__ . '/..')->load(); + Dotenv::createImmutable(__DIR__ . '/..')->load(); } $app['config']->set([ @@ -96,7 +103,7 @@ protected function getEnvironmentSetUp($app) '--realpath' => true, '--force' => true, ], - 'tenancy.bootstrappers.redis' => \Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper::class, + 'tenancy.bootstrappers.redis' => RedisTenancyBootstrapper::class, 'queue.connections.central' => [ 'driver' => 'sync', 'central' => true, @@ -105,28 +112,29 @@ protected function getEnvironmentSetUp($app) 'tenancy.tenant_model' => Tenant::class, // Use test tenant w/ DBs & domains ]); - $app->singleton(\Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper::class); + $app->singleton(RedisTenancyBootstrapper::class); + $app->singleton(BatchTenancyBootstrapper::class); // todo (Samuel) use proper approach eg config for singleton registration } protected function getPackageProviders($app) { return [ - \Stancl\Tenancy\TenancyServiceProvider::class, + TenancyServiceProvider::class, ]; } protected function getPackageAliases($app) { return [ - 'Tenancy' => \Stancl\Tenancy\Facades\Tenancy::class, - 'GlobalCache' => \Stancl\Tenancy\Facades\GlobalCache::class, + 'Tenancy' => Tenancy::class, + 'GlobalCache' => GlobalCache::class, ]; } /** * Resolve application HTTP Kernel implementation. * - * @param \Illuminate\Foundation\Application $app + * @param Application $app * @return void */ protected function resolveApplicationHttpKernel($app) @@ -137,7 +145,7 @@ protected function resolveApplicationHttpKernel($app) /** * Resolve application Console Kernel implementation. * - * @param \Illuminate\Foundation\Application $app + * @param Application $app * @return void */ protected function resolveApplicationConsoleKernel($app) From df172ffc2316c8138d9e632e34c7a902c70c28d2 Mon Sep 17 00:00:00 2001 From: Abrar Ahmad Date: Tue, 13 Sep 2022 11:16:04 +0500 Subject: [PATCH 10/18] Update src/Bootstrappers/BatchTenancyBootstrapper.php MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Samuel Ć tancl --- src/Bootstrappers/BatchTenancyBootstrapper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Bootstrappers/BatchTenancyBootstrapper.php b/src/Bootstrappers/BatchTenancyBootstrapper.php index 46143cef2..bc378f363 100644 --- a/src/Bootstrappers/BatchTenancyBootstrapper.php +++ b/src/Bootstrappers/BatchTenancyBootstrapper.php @@ -13,7 +13,7 @@ class BatchTenancyBootstrapper implements TenancyBootstrapper { /** - * The database previous connection instance. + * The previous database connection instance. * * @var \Illuminate\Database\Connection */ From 8211ddb38a9c0a8e15634108cde676f8533a1fbc Mon Sep 17 00:00:00 2001 From: Abrar Ahmad Date: Tue, 13 Sep 2022 11:24:59 +0500 Subject: [PATCH 11/18] convert batch class resolution to property level --- .../BatchTenancyBootstrapper.php | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/Bootstrappers/BatchTenancyBootstrapper.php b/src/Bootstrappers/BatchTenancyBootstrapper.php index bc378f363..ce891955b 100644 --- a/src/Bootstrappers/BatchTenancyBootstrapper.php +++ b/src/Bootstrappers/BatchTenancyBootstrapper.php @@ -19,23 +19,24 @@ class BatchTenancyBootstrapper implements TenancyBootstrapper */ protected $previousConnection = null; - public function bootstrap(Tenant $tenant) + public function __construct(protected BatchRepository $batchRepository) { - $batchRepository = app(BatchRepository::class); + } - if ($batchRepository instanceof DatabaseBatchRepository) { - // Access the resolved batch repository instance and update its connection to use the tenant connection - $this->previousConnection = $batchRepository->getConnection(); - $batchRepository->setConnection(DB::connection('tenant')); + public function bootstrap(Tenant $tenant) + { + if ($this->batchRepository instanceof DatabaseBatchRepository) { + // Update batch repository connection to use the tenant connection + $this->previousConnection = $this->batchRepository->getConnection(); + $this->batchRepository->setConnection(DB::connection('tenant')); } } public function revert() { if ($this->previousConnection) { - // Access the resolved batch repository instance and replace its connection with the previously replaced one - $batchRepository = app(BatchRepository::class); - $batchRepository->setConnection($this->previousConnection); + // Replace batch repository connection with the previously replaced one + $this->batchRepository->setConnection($this->previousConnection); $this->previousConnection = null; } } From 8e919b810149eb66e65734e04f7c5932dd428cbb Mon Sep 17 00:00:00 2001 From: Abrar Ahmad Date: Tue, 20 Sep 2022 11:38:07 +0500 Subject: [PATCH 12/18] enabled BatchTenancyBootstrapper by default --- assets/config.php | 2 +- tests/TestCase.php | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/assets/config.php b/assets/config.php index 13b274dab..d9d21eaab 100644 --- a/assets/config.php +++ b/assets/config.php @@ -33,7 +33,7 @@ Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper::class, Stancl\Tenancy\Bootstrappers\QueueTenancyBootstrapper::class, // Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper::class, // Note: phpredis is needed - // Stancl\Tenancy\Bootstrappers\BatchTenancyBootstrapper::class, + Stancl\Tenancy\Bootstrappers\BatchTenancyBootstrapper::class, ], /** diff --git a/tests/TestCase.php b/tests/TestCase.php index 853826dfe..f7f8b9ad2 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -112,8 +112,7 @@ protected function getEnvironmentSetUp($app) 'tenancy.tenant_model' => Tenant::class, // Use test tenant w/ DBs & domains ]); - $app->singleton(RedisTenancyBootstrapper::class); - $app->singleton(BatchTenancyBootstrapper::class); // todo (Samuel) use proper approach eg config for singleton registration + $app->singleton(RedisTenancyBootstrapper::class); // todo (Samuel) use proper approach eg config for singleton registration } protected function getPackageProviders($app) From 8bbf92aa9b91245cf541a1d0b20f20243c0ca7d0 Mon Sep 17 00:00:00 2001 From: Abrar Ahmad Date: Thu, 22 Sep 2022 11:16:14 +0500 Subject: [PATCH 13/18] typehint DatabaseBatchRepository --- src/Bootstrappers/BatchTenancyBootstrapper.php | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/Bootstrappers/BatchTenancyBootstrapper.php b/src/Bootstrappers/BatchTenancyBootstrapper.php index ce891955b..7f04d62be 100644 --- a/src/Bootstrappers/BatchTenancyBootstrapper.php +++ b/src/Bootstrappers/BatchTenancyBootstrapper.php @@ -4,7 +4,6 @@ namespace Stancl\Tenancy\Bootstrappers; -use Illuminate\Bus\BatchRepository; use Illuminate\Bus\DatabaseBatchRepository; use Illuminate\Support\Facades\DB; use Stancl\Tenancy\Contracts\TenancyBootstrapper; @@ -19,24 +18,22 @@ class BatchTenancyBootstrapper implements TenancyBootstrapper */ protected $previousConnection = null; - public function __construct(protected BatchRepository $batchRepository) + public function __construct(protected DatabaseBatchRepository $databaseBatchRepository) { } public function bootstrap(Tenant $tenant) { - if ($this->batchRepository instanceof DatabaseBatchRepository) { - // Update batch repository connection to use the tenant connection - $this->previousConnection = $this->batchRepository->getConnection(); - $this->batchRepository->setConnection(DB::connection('tenant')); - } + // Update batch repository connection to use the tenant connection + $this->previousConnection = $this->databaseBatchRepository->getConnection(); + $this->databaseBatchRepository->setConnection(DB::connection('tenant')); } public function revert() { if ($this->previousConnection) { // Replace batch repository connection with the previously replaced one - $this->batchRepository->setConnection($this->previousConnection); + $this->databaseBatchRepository->setConnection($this->previousConnection); $this->previousConnection = null; } } From d6947f805882387a3744f69efcc3439448abe03c Mon Sep 17 00:00:00 2001 From: Abrar Ahmad Date: Fri, 23 Sep 2022 10:57:54 +0500 Subject: [PATCH 14/18] refactore name --- src/Bootstrappers/BatchTenancyBootstrapper.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Bootstrappers/BatchTenancyBootstrapper.php b/src/Bootstrappers/BatchTenancyBootstrapper.php index 7f04d62be..915f69b4a 100644 --- a/src/Bootstrappers/BatchTenancyBootstrapper.php +++ b/src/Bootstrappers/BatchTenancyBootstrapper.php @@ -5,6 +5,7 @@ namespace Stancl\Tenancy\Bootstrappers; use Illuminate\Bus\DatabaseBatchRepository; +use Illuminate\Database\ConnectionInterface; use Illuminate\Support\Facades\DB; use Stancl\Tenancy\Contracts\TenancyBootstrapper; use Stancl\Tenancy\Contracts\Tenant; @@ -18,22 +19,22 @@ class BatchTenancyBootstrapper implements TenancyBootstrapper */ protected $previousConnection = null; - public function __construct(protected DatabaseBatchRepository $databaseBatchRepository) + public function __construct(protected DatabaseBatchRepository $batchRepository) { } public function bootstrap(Tenant $tenant) { // Update batch repository connection to use the tenant connection - $this->previousConnection = $this->databaseBatchRepository->getConnection(); - $this->databaseBatchRepository->setConnection(DB::connection('tenant')); + $this->previousConnection = $this->batchRepository->getConnection(); + $this->batchRepository->setConnection(DB::connection('tenant')); } public function revert() { if ($this->previousConnection) { // Replace batch repository connection with the previously replaced one - $this->databaseBatchRepository->setConnection($this->previousConnection); + $this->batchRepository->setConnection($this->previousConnection); $this->previousConnection = null; } } From 5617c035a16dea72c0bd7e6a692970bb85c9feff Mon Sep 17 00:00:00 2001 From: Abrar Ahmad Date: Fri, 23 Sep 2022 11:01:16 +0500 Subject: [PATCH 15/18] DI DB manager --- src/Bootstrappers/BatchTenancyBootstrapper.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Bootstrappers/BatchTenancyBootstrapper.php b/src/Bootstrappers/BatchTenancyBootstrapper.php index 915f69b4a..4df62fe6d 100644 --- a/src/Bootstrappers/BatchTenancyBootstrapper.php +++ b/src/Bootstrappers/BatchTenancyBootstrapper.php @@ -5,8 +5,7 @@ namespace Stancl\Tenancy\Bootstrappers; use Illuminate\Bus\DatabaseBatchRepository; -use Illuminate\Database\ConnectionInterface; -use Illuminate\Support\Facades\DB; +use Illuminate\Database\DatabaseManager; use Stancl\Tenancy\Contracts\TenancyBootstrapper; use Stancl\Tenancy\Contracts\Tenant; @@ -19,15 +18,17 @@ class BatchTenancyBootstrapper implements TenancyBootstrapper */ protected $previousConnection = null; - public function __construct(protected DatabaseBatchRepository $batchRepository) - { + public function __construct( + protected DatabaseBatchRepository $batchRepository, + protected DatabaseManager $databaseManager + ) { } public function bootstrap(Tenant $tenant) { // Update batch repository connection to use the tenant connection $this->previousConnection = $this->batchRepository->getConnection(); - $this->batchRepository->setConnection(DB::connection('tenant')); + $this->batchRepository->setConnection($this->databaseManager->connection('tenant')); } public function revert() From 360643ba574361a4cec0fe6b8ceba8dc3a15b115 Mon Sep 17 00:00:00 2001 From: Abrar Ahmad Date: Mon, 26 Sep 2022 11:22:22 +0500 Subject: [PATCH 16/18] typehint --- src/Bootstrappers/BatchTenancyBootstrapper.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Bootstrappers/BatchTenancyBootstrapper.php b/src/Bootstrappers/BatchTenancyBootstrapper.php index 4df62fe6d..ccd1c00aa 100644 --- a/src/Bootstrappers/BatchTenancyBootstrapper.php +++ b/src/Bootstrappers/BatchTenancyBootstrapper.php @@ -5,6 +5,7 @@ namespace Stancl\Tenancy\Bootstrappers; use Illuminate\Bus\DatabaseBatchRepository; +use Illuminate\Database\Connection; use Illuminate\Database\DatabaseManager; use Stancl\Tenancy\Contracts\TenancyBootstrapper; use Stancl\Tenancy\Contracts\Tenant; @@ -13,10 +14,8 @@ class BatchTenancyBootstrapper implements TenancyBootstrapper { /** * The previous database connection instance. - * - * @var \Illuminate\Database\Connection */ - protected $previousConnection = null; + protected ?Connection $previousConnection = null; public function __construct( protected DatabaseBatchRepository $batchRepository, From 989e2368ca7838bcbd801a5311bf86efdad07d01 Mon Sep 17 00:00:00 2001 From: Abrar Ahmad Date: Mon, 26 Sep 2022 11:22:43 +0500 Subject: [PATCH 17/18] Update config.php --- assets/config.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assets/config.php b/assets/config.php index d9d21eaab..68a954403 100644 --- a/assets/config.php +++ b/assets/config.php @@ -32,8 +32,8 @@ Stancl\Tenancy\Bootstrappers\CacheTenancyBootstrapper::class, Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper::class, Stancl\Tenancy\Bootstrappers\QueueTenancyBootstrapper::class, - // Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper::class, // Note: phpredis is needed Stancl\Tenancy\Bootstrappers\BatchTenancyBootstrapper::class, + // Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper::class, // Note: phpredis is needed ], /** From 395d8597bb0b041ce13f637b5d8b915c42b80695 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Mon, 26 Sep 2022 14:02:48 +0200 Subject: [PATCH 18/18] use initialize() twice without end()ing tenancy to assert that previousConnection logic works correctly --- tests/BatchTest.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/BatchTest.php b/tests/BatchTest.php index a401dbc79..a168deb2b 100644 --- a/tests/BatchTest.php +++ b/tests/BatchTest.php @@ -24,15 +24,17 @@ test('batch repository is set to tenant connection and reverted', function () { $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'); })->skip(fn() => version_compare(app()->version(), '8.0', '<'), 'Job batches are only supported in Laravel 8+');