feat(админка): middleware UseAdminConnection — swap default на pgsql_admin

Меняет default-подключение на pgsql_admin на время admin-запроса и
восстанавливает прежнее в finally (важно для Pest: несколько запросов в
одном процессе). Ставится после saas-admin. Tests: swap+restore и
restore при исключении downstream.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Дмитрий
2026-06-27 06:39:27 +03:00
parent d5c972c3f2
commit 1c72f6dec2
2 changed files with 82 additions and 0 deletions
@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
use App\Http\Middleware\UseAdminConnection;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
uses(TestCase::class);
it('switches default connection to pgsql_admin during the request', function () {
$original = DB::getDefaultConnection();
$seen = null;
$response = (new UseAdminConnection())->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);
});