From 40111b0ce161232cd045411c0d1775901f148e0e Mon Sep 17 00:00:00 2001 From: Abrar Ahmad Date: Wed, 31 Aug 2022 12:37:56 +0500 Subject: [PATCH 1/7] add BatchTenancyBootstrapper and tests --- .../BatchTenancyBootstrapper.php | 40 +++++++++++++++++ tests/BatchTest.php | 44 +++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 src/Bootstrappers/BatchTenancyBootstrapper.php create mode 100644 tests/BatchTest.php diff --git a/src/Bootstrappers/BatchTenancyBootstrapper.php b/src/Bootstrappers/BatchTenancyBootstrapper.php new file mode 100644 index 000000000..5d6afdf8a --- /dev/null +++ b/src/Bootstrappers/BatchTenancyBootstrapper.php @@ -0,0 +1,40 @@ +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 + $batchRepository = app(BatchRepository::class); + $batchRepository->setConnection($this->previousConnection); + $this->previousConnection = null; + } + } +} diff --git a/tests/BatchTest.php b/tests/BatchTest.php new file mode 100644 index 000000000..06f41401c --- /dev/null +++ b/tests/BatchTest.php @@ -0,0 +1,44 @@ +app->singleton(BatchTenancyBootstrapper::class); + + config([ + 'tenancy.bootstrappers' => [ + DatabaseTenancyBootstrapper::class, + BatchTenancyBootstrapper::class, + ], + ]); + + Event::listen(TenancyInitialized::class, BootstrapTenancy::class); + Event::listen(TenancyEnded::class, RevertToCentralContext::class); +}); + +test('batch repository is set to tenant connection and reverted', function () { + $tenant = Tenant::create(); + + expect(getBatchRepositoryConnectionName())->toBe('central'); + + tenancy()->initialize($tenant); + + 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+'); + +function getBatchRepositoryConnectionName() +{ + return app(BatchRepository::class)->getConnection()->getName(); +} \ No newline at end of file From 7f2e46334c5bb99d823ad5e6107a3b5202f05052 Mon Sep 17 00:00:00 2001 From: Abrar Ahmad Date: Wed, 31 Aug 2022 12:38:30 +0500 Subject: [PATCH 2/7] Update BatchTest.php --- 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 e7048725648287df67b46010f278d79436fe3e42 Mon Sep 17 00:00:00 2001 From: Abrar Ahmad Date: Wed, 31 Aug 2022 12:40:36 +0500 Subject: [PATCH 3/7] Update ci.yml --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f0fe927ad..eeb589784 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -8,7 +8,7 @@ on: push: branches: [ master ] pull_request: - branches: [ master ] + branches: [ master 3.x] # todo@1 remove 3.x before merge jobs: tests: From 7e421875005b3cf1ed815bdef0ed445824381302 Mon Sep 17 00:00:00 2001 From: Abrar Ahmad Date: Wed, 31 Aug 2022 12:41:21 +0500 Subject: [PATCH 4/7] Update ci.yml --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index eeb589784..19b9487d8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -6,7 +6,7 @@ env: on: push: - branches: [ master ] + branches: [ master 3.x] pull_request: branches: [ master 3.x] # todo@1 remove 3.x before merge From 8cf10950f45ff897d23fe9bdefee8787560476d5 Mon Sep 17 00:00:00 2001 From: Abrar Ahmad Date: Wed, 31 Aug 2022 12:44:51 +0500 Subject: [PATCH 5/7] Update config.php --- assets/config.php | 1 + 1 file changed, 1 insertion(+) diff --git a/assets/config.php b/assets/config.php index e1c82e6be..2abb2491b 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, ], /** From 0f0427b42d279c607345dc5543fb860aa65e17a4 Mon Sep 17 00:00:00 2001 From: Abrar Ahmad Date: Wed, 31 Aug 2022 12:52:34 +0500 Subject: [PATCH 6/7] Update ci.yml --- .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 19b9487d8..aa04a3a16 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -6,9 +6,9 @@ env: on: push: - branches: [ master 3.x] + branches: [ master, 3.x] pull_request: - branches: [ master 3.x] # todo@1 remove 3.x before merge + branches: [ master, 3.x] # todo@1 remove 3.x before merge jobs: tests: From 50594ecd79516a35bb07b871646cb49ad3986390 Mon Sep 17 00:00:00 2001 From: PHP CS Fixer Date: Wed, 31 Aug 2022 07:53:08 +0000 Subject: [PATCH 7/7] Fix code style (php-cs-fixer) --- src/Bootstrappers/BatchTenancyBootstrapper.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Bootstrappers/BatchTenancyBootstrapper.php b/src/Bootstrappers/BatchTenancyBootstrapper.php index 5d6afdf8a..46143cef2 100644 --- a/src/Bootstrappers/BatchTenancyBootstrapper.php +++ b/src/Bootstrappers/BatchTenancyBootstrapper.php @@ -1,5 +1,7 @@