mirror of
https://github.com/archtechx/tenancy.git
synced 2026-05-06 18:04:03 +00:00
If harden throws an exception, revert connection back to central
This commit is contained in:
parent
f5f5f1d4aa
commit
52f6857302
2 changed files with 17 additions and 1 deletions
|
|
@ -51,7 +51,16 @@ class DatabaseTenancyBootstrapper implements TenancyBootstrapper
|
||||||
|
|
||||||
$this->database->connectToTenant($tenant);
|
$this->database->connectToTenant($tenant);
|
||||||
|
|
||||||
if (static::$harden) $this->harden($tenant);
|
try {
|
||||||
|
if (static::$harden) {
|
||||||
|
$this->harden($tenant);
|
||||||
|
}
|
||||||
|
} catch (RuntimeException $e) {
|
||||||
|
// Revert connection back to central
|
||||||
|
$this->revert();
|
||||||
|
|
||||||
|
throw $e;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function revert(): void
|
public function revert(): void
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@ use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||||
|
|
||||||
use function Stancl\Tenancy\Tests\pest;
|
use function Stancl\Tenancy\Tests\pest;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
$cleanup = function () {
|
$cleanup = function () {
|
||||||
DatabaseTenancyBootstrapper::$harden = false;
|
DatabaseTenancyBootstrapper::$harden = false;
|
||||||
|
|
@ -46,6 +47,9 @@ test('harden prevents tenants from using the central database', function () {
|
||||||
|
|
||||||
// Harden blocks initialization for tenants that use central database
|
// Harden blocks initialization for tenants that use central database
|
||||||
expect(fn () => tenancy()->initialize($tenant))->toThrow(RuntimeException::class);
|
expect(fn () => tenancy()->initialize($tenant))->toThrow(RuntimeException::class);
|
||||||
|
|
||||||
|
// Connection should be reverted back to central
|
||||||
|
expect(DB::connection()->getName())->toBe('central');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('harden prevents tenants from using a database of another tenant', function () {
|
test('harden prevents tenants from using a database of another tenant', function () {
|
||||||
|
|
@ -71,6 +75,9 @@ test('harden prevents tenants from using a database of another tenant', function
|
||||||
|
|
||||||
// Harden blocks initialization for tenants that use a database of another tenant
|
// Harden blocks initialization for tenants that use a database of another tenant
|
||||||
expect(fn () => tenancy()->initialize($tenant))->toThrow(RuntimeException::class);
|
expect(fn () => tenancy()->initialize($tenant))->toThrow(RuntimeException::class);
|
||||||
|
|
||||||
|
// Connection should be reverted back to central
|
||||||
|
expect(DB::connection()->getName())->toBe('central');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('database tenancy bootstrapper throws an exception if DATABASE_URL is set', function (string|null $databaseUrl) {
|
test('database tenancy bootstrapper throws an exception if DATABASE_URL is set', function (string|null $databaseUrl) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue