Files
mtusi2026lab3/migrations/versions/2026_10_01_0135-16c279725564_added_messenger_tables.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

128 lines
4.2 KiB
Python

"""added messenger tables
Revision ID: 16c279725564
Revises:
Create Date: 2026-10-01 01:35:56.145855
"""
from collections.abc import Sequence
import sqlalchemy as sa
from alembic import op
# revision identifiers, used by Alembic.
revision: str = "16c279725564"
down_revision: str | Sequence[str] | None = None
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None
def upgrade() -> None:
# CHECK записаны литералами, а не константами src: применённая
# ревизия не должна меняться вместе с кодом
op.create_table(
"chats",
sa.Column("id", sa.Uuid(), nullable=False),
sa.Column("title", sa.Text(), nullable=False),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.CheckConstraint(
"char_length(title) BETWEEN 1 AND 100",
name=op.f("ck_chats_title_length"),
),
sa.PrimaryKeyConstraint("id", name=op.f("pk_chats")),
)
op.create_table(
"users",
sa.Column("id", sa.Uuid(), nullable=False),
sa.Column("username", sa.Text(), nullable=False),
sa.Column("display_name", sa.Text(), nullable=False),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.CheckConstraint(
"username ~ '^[a-z0-9_]{3,32}$'",
name=op.f("ck_users_username_format"),
),
sa.CheckConstraint(
"char_length(display_name) BETWEEN 1 AND 100",
name=op.f("ck_users_display_name_length"),
),
sa.PrimaryKeyConstraint("id", name=op.f("pk_users")),
sa.UniqueConstraint("username", name=op.f("uq_users_username")),
)
op.create_table(
"chat_members",
sa.Column("chat_id", sa.Uuid(), nullable=False),
sa.Column("user_id", sa.Uuid(), nullable=False),
sa.Column("position", sa.SmallInteger(), nullable=False),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.CheckConstraint(
"position >= 0",
name=op.f("ck_chat_members_position_non_negative"),
),
sa.ForeignKeyConstraint(
["chat_id"],
["chats.id"],
name=op.f("fk_chat_members_chat_id_chats"),
),
sa.ForeignKeyConstraint(
["user_id"],
["users.id"],
name=op.f("fk_chat_members_user_id_users"),
),
sa.PrimaryKeyConstraint(
"chat_id",
"user_id",
name=op.f("pk_chat_members"),
),
sa.UniqueConstraint(
"chat_id",
"position",
name=op.f("uq_chat_members_chat_id_position"),
),
)
op.create_index(
op.f("ix_chat_members_user_id_created_at_chat_id"),
"chat_members",
["user_id", "created_at", "chat_id"],
unique=False,
)
op.create_table(
"messages",
sa.Column("id", sa.Uuid(), nullable=False),
sa.Column("chat_id", sa.Uuid(), nullable=False),
sa.Column("sender_id", sa.Uuid(), nullable=False),
sa.Column("text", sa.Text(), nullable=False),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.CheckConstraint(
"char_length(text) BETWEEN 1 AND 4000",
name=op.f("ck_messages_text_length"),
),
sa.ForeignKeyConstraint(
["chat_id", "sender_id"],
["chat_members.chat_id", "chat_members.user_id"],
name=op.f("fk_messages_chat_id_sender_id_chat_members"),
),
sa.PrimaryKeyConstraint("id", name=op.f("pk_messages")),
)
op.create_index(
op.f("ix_messages_chat_id_created_at_id"),
"messages",
["chat_id", "created_at", "id"],
unique=False,
)
def downgrade() -> None:
op.drop_index(
op.f("ix_messages_chat_id_created_at_id"),
table_name="messages",
)
op.drop_table("messages")
op.drop_index(
op.f("ix_chat_members_user_id_created_at_chat_id"),
table_name="chat_members",
)
op.drop_table("chat_members")
op.drop_table("users")
op.drop_table("chats")