diff --git a/app/app/Http/Middleware/UseAdminConnection.php b/app/app/Http/Middleware/UseAdminConnection.php new file mode 100644 index 00000000..d769ae87 --- /dev/null +++ b/app/app/Http/Middleware/UseAdminConnection.php @@ -0,0 +1,40 @@ +handle( + Request::create('/api/admin/tenants'), + function () use (&$seen) { + $seen = DB::getDefaultConnection(); + + return response('ok'); + } + ); + + expect($seen)->toBe('pgsql_admin'); + expect($response->getContent())->toBe('ok'); + expect(DB::getDefaultConnection())->toBe($original); // восстановлено +}); + +it('restores the default connection even when downstream throws', function () { + $original = DB::getDefaultConnection(); + + $call = fn () => (new UseAdminConnection())->handle( + Request::create('/api/admin/tenants'), + function () { + throw new RuntimeException('boom'); + } + ); + + expect($call)->toThrow(RuntimeException::class); + expect(DB::getDefaultConnection())->toBe($original); +});