feat: store messenger data in PostgreSQL
Replace the in-memory store with PostgreSQL 18.6: SQLAlchemy 2.1 async engine on asyncpg, alembic migrations with an async env.py, repos on AsyncSession and services that own the transaction. Username uniqueness is enforced by the database (ON CONFLICT), a chat and its members are created in one transaction, member order is kept in chat_members.position, and a composite foreign key makes the sender a member of the chat. History and chat lists use keyset pagination on (chat_id, created_at, id) and (user_id, created_at, chat_id). The compose stand runs postgres, a one-shot migrate service on the API image and the API. All Postgres addresses come from .env without literals; only the 127.0.0.1 publish address stays literal. make run_dev starts Postgres and applies migrations before the server, and make env_setup generates a random Postgres password. Unavailable PostgreSQL yields 503: get_db_session bounds the request with asyncio and classifies driver errors. BoundedCloseConnection limits asyncpg's graceful close, which otherwise waited without limit on a frozen server and held the request until it was resumed. Add scripts/restart.sh for make test-persistence and make test_isolated, which runs the image on a throwaway test database. Remove make run_no_compose. X-User-Id identification is unchanged until the session-auth change. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
from uuid import UUID
|
||||
|
||||
from sqlalchemy import (
|
||||
CheckConstraint,
|
||||
ForeignKey,
|
||||
Index,
|
||||
SmallInteger,
|
||||
Text,
|
||||
UniqueConstraint,
|
||||
)
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
from src.infra.db.models.base import Base
|
||||
from src.utils.constants import CHAT_TITLE_MAX_LENGTH, CHAT_TITLE_MIN_LENGTH
|
||||
|
||||
|
||||
class Chat(Base):
|
||||
__tablename__ = "chats"
|
||||
__table_args__ = (
|
||||
CheckConstraint(
|
||||
"char_length(title) BETWEEN "
|
||||
f"{CHAT_TITLE_MIN_LENGTH} AND {CHAT_TITLE_MAX_LENGTH}",
|
||||
name="title_length",
|
||||
),
|
||||
)
|
||||
|
||||
id: Mapped[UUID] = mapped_column(primary_key=True)
|
||||
title: Mapped[str] = mapped_column(Text)
|
||||
|
||||
|
||||
class ChatMember(Base):
|
||||
"""Участник чата.
|
||||
|
||||
`position` — место в `member_ids` из запроса: контракт отдаёт их в
|
||||
порядке создания. `created_at` — копия времени создания чата: по ней
|
||||
список чатов пользователя идёт keyset-ом по индексу, без сортировки
|
||||
всех его чатов.
|
||||
"""
|
||||
|
||||
__tablename__ = "chat_members"
|
||||
__table_args__ = (
|
||||
UniqueConstraint("chat_id", "position"),
|
||||
CheckConstraint("position >= 0", name="position_non_negative"),
|
||||
Index(None, "user_id", "created_at", "chat_id"),
|
||||
)
|
||||
|
||||
chat_id: Mapped[UUID] = mapped_column(
|
||||
ForeignKey("chats.id"),
|
||||
primary_key=True,
|
||||
)
|
||||
user_id: Mapped[UUID] = mapped_column(
|
||||
ForeignKey("users.id"),
|
||||
primary_key=True,
|
||||
)
|
||||
position: Mapped[int] = mapped_column(SmallInteger)
|
||||
Reference in New Issue
Block a user