Adopt async SQLAlchemy storage and verified Alembic migrations

Keep the message and idempotency receipt in one Core CTE statement with chat-first locking. Preserve rollback, bounded dependency failures, and domain ports.

Replace the SQL runner with Alembic. Adopt legacy databases only after validating their journal and PostgreSQL schema, retain all data, and verify migrations on fresh and populated databases.
This commit is contained in:
Artyom
2026-11-05 21:38:12 +03:00
parent c376882ce8
commit 71c643c6d1
29 changed files with 1542 additions and 409 deletions
+11 -12
View File
@@ -5,6 +5,7 @@ import socket
import subprocess
import sys
import time
from pathlib import Path
import httpx
@@ -36,15 +37,9 @@ async def test_draining_replica_is_not_ready_but_alive(client: httpx.AsyncClient
assert (await client.get("/health/ready")).status_code == 200
def free_port() -> int:
with socket.socket() as probe:
probe.bind(("127.0.0.1", 0))
return probe.getsockname()[1]
def status(port: int, path: str) -> int:
try:
return httpx.get(f"http://127.0.0.1:{port}{path}", timeout=1).status_code
return httpx.get(f"http://127.0.0.1:{port}{path}", timeout=1, trust_env=False).status_code
except httpx.TransportError:
return 0
@@ -71,8 +66,8 @@ def begin_registration(port: int) -> tuple[socket.socket, bytes]:
return connection, body[8:]
def test_sigterm_leaves_rotation_then_closes_and_cancels_stuck_requests() -> None:
port = free_port()
def test_sigterm_leaves_rotation_then_closes_and_cancels_stuck_requests(tmp_path: Path) -> None:
port = 18083
settings = {
"HOST": "127.0.0.1",
"PORT": str(port),
@@ -83,16 +78,18 @@ def test_sigterm_leaves_rotation_then_closes_and_cancels_stuck_requests() -> Non
"DRAIN_SECONDS": str(DRAIN),
"SHUTDOWN_TIMEOUT_SECONDS": str(SHUTDOWN),
}
log_path = tmp_path / "gunicorn.log"
log = log_path.open("w")
server = subprocess.Popen(
[sys.executable, *GUNICORN],
env={**os.environ, **settings},
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
stdout=log,
stderr=log,
)
try:
deadline = time.monotonic() + 30
while status(port, "/health/ready") != 200:
assert time.monotonic() < deadline, "gunicorn не стал готов"
assert server.poll() is None and time.monotonic() < deadline, log_path.read_text()
time.sleep(0.2)
pending, rest = begin_registration(port)
stuck, _ = begin_registration(port)
@@ -112,6 +109,8 @@ def test_sigterm_leaves_rotation_then_closes_and_cancels_stuck_requests() -> Non
finally:
if server.poll() is None:
server.kill()
server.wait(timeout=5)
log.close()
print(f"окно: ready/live {during}, порт закрыт через {closed:.2f} с, выход {exited:.2f} с")
print(f"запрос, принятый до SIGTERM и дописанный после закрытия порта: {answer}")
print(f"запрос, тело которого так и не пришло: {cancelled or 'соединение закрыто без ответа'}")