Files
mtusi2026lab4/migrations/versions/2026_10_01_0135-16c279725564_added_messenger_tables.py
T
ricoromaandClaude Opus 5.5 5d5d8f9d2a chore: carry lab 3 over
- carry the lab 3 solution over from mtusi2026lab3-safronov
  (solution/bvt2301-safronov, ce89eba): src, migrations, student-tests,
  bench, openspec, .claude, infra/haproxy and infra/valkey, own scripts,
  Dockerfile, compose, pyproject and uv lock
- merge Makefile, .gitignore and .dockerignore (the lab 3 versions only
  add lines to the skeleton, which is the same in lab 4) and
  .env.example (lab 4 skeleton names listed in the header)
- rename the compose project to mtusi-lab4 and the image to :lab4 so
  the stand does not reuse the lab 3 containers and volumes
- update AGENTS.md for lab 4: current lab, origin commit, contract and
  test files of the new skeleton

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 17:32:33 +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")