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
+13 -14
View File
@@ -1,10 +1,11 @@
import time
from uuid import uuid4
from uuid import UUID, uuid4
import asyncpg
from httpx import AsyncClient
from sqlalchemy import select
from conftest import DATABASE, actor, keyed, make_chat, register
from conftest import actor, keyed, make_chat, register, sql_connection
from messenger.messaging.infrastructure.postgres import ChatRow
async def test_outsider_gets_404_on_chat(client: AsyncClient) -> None:
@@ -106,16 +107,14 @@ async def test_outsider_does_not_wait_for_members_lock(client: AsyncClient) -> N
alice = await register(client, "alice")
eve = await register(client, "eve")
chat = await make_chat(client, alice)
holder = await asyncpg.connect(DATABASE["url"])
try:
async with holder.transaction():
await holder.execute("SELECT 1 FROM chats WHERE id = $1 FOR UPDATE", chat["id"])
started = time.perf_counter()
response = await client.post(
f"/api/v1/chats/{chat['id']}/messages", json={"text": "чужое"}, headers=keyed(eve)
)
elapsed = time.perf_counter() - started
finally:
await holder.close()
async with sql_connection() as holder:
await holder.execute(
select(ChatRow.id).where(ChatRow.id == UUID(chat["id"])).with_for_update()
)
started = time.perf_counter()
response = await client.post(
f"/api/v1/chats/{chat['id']}/messages", json={"text": "чужое"}, headers=keyed(eve)
)
elapsed = time.perf_counter() - started
assert response.status_code == 404
assert elapsed < 0.5