1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 15:34:03 +00:00

deprecate JobBatchBootstrapper

This commit is contained in:
Samuel Štancl 2024-12-27 21:33:44 +01:00
parent 5777ff850f
commit eff41553d7
3 changed files with 15 additions and 67 deletions

View file

@ -148,9 +148,8 @@ return [
Bootstrappers\QueueTenancyBootstrapper::class, Bootstrappers\QueueTenancyBootstrapper::class,
// Bootstrappers\RedisTenancyBootstrapper::class, // Note: phpredis is needed // Bootstrappers\RedisTenancyBootstrapper::class, // Note: phpredis is needed
// Support for edge cases // Adds support for the database session driver
Bootstrappers\DatabaseSessionBootstrapper::class, Bootstrappers\DatabaseSessionBootstrapper::class,
Bootstrappers\JobBatchBootstrapper::class,
// Configurable bootstrappers // Configurable bootstrappers
// Bootstrappers\RootUrlBootstrapper::class, // Bootstrappers\RootUrlBootstrapper::class,

View file

@ -4,41 +4,36 @@ declare(strict_types=1);
namespace Stancl\Tenancy\Bootstrappers; namespace Stancl\Tenancy\Bootstrappers;
use Illuminate\Bus\DatabaseBatchRepository; use Exception;
use Illuminate\Database\Connection; use Illuminate\Contracts\Foundation\Application;
use Illuminate\Database\DatabaseManager;
use Stancl\Tenancy\Contracts\TenancyBootstrapper; use Stancl\Tenancy\Contracts\TenancyBootstrapper;
use Stancl\Tenancy\Contracts\Tenant; use Stancl\Tenancy\Contracts\Tenant;
/** /**
* Adds support for running queued tenant jobs in batches. * 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 class JobBatchBootstrapper implements TenancyBootstrapper
{ {
/**
* The previous database connection instance.
*/
protected ?Connection $previousConnection = null;
public function __construct( public function __construct(
protected DatabaseBatchRepository $batchRepository, protected Application $app,
protected DatabaseManager $databaseManager
) {} ) {}
public function bootstrap(Tenant $tenant): void public function bootstrap(Tenant $tenant): void
{ {
// todo@revisit $this->deprecatedNotice();
// Update batch repository connection to use the tenant connection }
$this->previousConnection = $this->batchRepository->getConnection();
$this->batchRepository->setConnection($this->databaseManager->connection('tenant')); 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 public function revert(): void
{ {
if ($this->previousConnection) { $this->deprecatedNotice();
// Replace batch repository connection with the previously replaced one
$this->batchRepository->setConnection($this->previousConnection);
$this->previousConnection = null;
}
} }
} }

View file

@ -1,46 +0,0 @@
<?php
use Illuminate\Bus\BatchRepository;
use Illuminate\Support\Facades\Event;
use Stancl\Tenancy\Bootstrappers\JobBatchBootstrapper;
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
use Stancl\Tenancy\Events\TenancyEnded;
use Stancl\Tenancy\Events\TenancyInitialized;
use Stancl\Tenancy\Listeners\BootstrapTenancy;
use Stancl\Tenancy\Listeners\RevertToCentralContext;
use Stancl\Tenancy\Tests\Etc\Tenant;
beforeEach(function () {
config([
'tenancy.bootstrappers' => [
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();
}