Files
mtusi2026lab3/src/infra/db/models/chat.py
T
ricoromaandClaude Opus 5.5 af4bca08c9 feat: carry lab 2 over and add X-Instance-Id and idempotency keys
- carry the lab 2 solution over and plan lab 3 (PLAN.md, OpenSpec
  change instance-id-idempotency)
- rename the compose project to mtusi-lab3 and the image to :lab3
- add X-Instance-Id to every response and instance_id to every log
  record ([app] instance_id, hostname by default)
- require Idempotency-Key on POST messages: keys table in Postgres,
  key and message in one transaction, replay returns the same 201,
  another body gets 409, TTL from [idempotency] ttl_seconds
- send the key from student-tests, k6 scenarios and the skill driver;
  add idempotency, X-Instance-Id, key race and fault tests

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 19:42:36 +03:00

56 lines
1.6 KiB
Python

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)