104 lines
4.0 KiB
Python
104 lines
4.0 KiB
Python
"""Начальная схема: пользователи, чаты, участники и сообщения
|
|
|
|
Revision ID: 0001_initial_schema
|
|
Revises:
|
|
Create Date: 2026-10-11
|
|
|
|
DDL написан вручную и повторяет src/messenger/schema.py: имена ограничений
|
|
и индексов совпадают, поэтому `alembic check` не находит расхождений между
|
|
схемой в базе и описанием, по которому работают запросы.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import sqlalchemy as sa
|
|
from alembic import op
|
|
from sqlalchemy.dialects import postgresql
|
|
|
|
revision = "0001_initial_schema"
|
|
down_revision = None
|
|
branch_labels = None
|
|
depends_on = None
|
|
|
|
|
|
def upgrade() -> None:
|
|
op.create_table(
|
|
"users",
|
|
sa.Column("id", postgresql.UUID(as_uuid=True), nullable=False),
|
|
sa.Column("username", sa.Text(), nullable=False),
|
|
sa.Column("display_name", sa.Text(), nullable=False),
|
|
sa.Column("password_hash", sa.Text(), nullable=False),
|
|
sa.Column(
|
|
"created_at",
|
|
postgresql.TIMESTAMP(timezone=True),
|
|
server_default=sa.text("now()"),
|
|
nullable=False,
|
|
),
|
|
sa.CheckConstraint(
|
|
"char_length(display_name) BETWEEN 1 AND 100", name="ck_users_display_name_length"
|
|
),
|
|
sa.CheckConstraint(r"username ~ '^[a-z0-9_]{3,32}$'", name="ck_users_username_format"),
|
|
sa.PrimaryKeyConstraint("id"),
|
|
sa.UniqueConstraint("username", name="uq_users_username"),
|
|
)
|
|
|
|
op.create_table(
|
|
"chats",
|
|
sa.Column("id", postgresql.UUID(as_uuid=True), nullable=False),
|
|
sa.Column("title", sa.Text(), nullable=False),
|
|
sa.Column(
|
|
"created_at",
|
|
postgresql.TIMESTAMP(timezone=True),
|
|
server_default=sa.text("now()"),
|
|
nullable=False,
|
|
),
|
|
sa.CheckConstraint("char_length(title) BETWEEN 1 AND 100", name="ck_chats_title_length"),
|
|
sa.PrimaryKeyConstraint("id"),
|
|
)
|
|
|
|
op.create_table(
|
|
"chat_members",
|
|
sa.Column("chat_id", postgresql.UUID(as_uuid=True), nullable=False),
|
|
sa.Column("user_id", postgresql.UUID(as_uuid=True), nullable=False),
|
|
sa.ForeignKeyConstraint(
|
|
["chat_id"], ["chats.id"], name="fk_chat_members_chat", ondelete="CASCADE"
|
|
),
|
|
sa.ForeignKeyConstraint(
|
|
["user_id"], ["users.id"], name="fk_chat_members_user", ondelete="RESTRICT"
|
|
),
|
|
sa.PrimaryKeyConstraint("chat_id", "user_id"),
|
|
)
|
|
op.create_index("ix_chat_members_user_id_chat_id", "chat_members", ["user_id", "chat_id"])
|
|
|
|
op.create_table(
|
|
"messages",
|
|
sa.Column("id", postgresql.UUID(as_uuid=True), nullable=False),
|
|
sa.Column("chat_id", postgresql.UUID(as_uuid=True), nullable=False),
|
|
sa.Column("sender_id", postgresql.UUID(as_uuid=True), nullable=False),
|
|
sa.Column("text", sa.Text(), nullable=False),
|
|
sa.Column(
|
|
"created_at",
|
|
postgresql.TIMESTAMP(timezone=True),
|
|
server_default=sa.text("now()"),
|
|
nullable=False,
|
|
),
|
|
sa.CheckConstraint(
|
|
"text <> '' AND char_length(text) <= 4000", name="ck_messages_text_length"
|
|
),
|
|
sa.ForeignKeyConstraint(["chat_id"], ["chats.id"], name="fk_messages_chat", ondelete="CASCADE"),
|
|
sa.ForeignKeyConstraint(
|
|
["sender_id"], ["users.id"], name="fk_messages_sender", ondelete="RESTRICT"
|
|
),
|
|
sa.PrimaryKeyConstraint("id"),
|
|
)
|
|
# Порядок истории (created_at, id) и keyset-предикат обслуживает один индекс.
|
|
op.create_index("ix_messages_chat_id_created_at_id", "messages", ["chat_id", "created_at", "id"])
|
|
|
|
|
|
def downgrade() -> None:
|
|
op.drop_index("ix_messages_chat_id_created_at_id", table_name="messages")
|
|
op.drop_table("messages")
|
|
op.drop_index("ix_chat_members_user_id_chat_id", table_name="chat_members")
|
|
op.drop_table("chat_members")
|
|
op.drop_table("chats")
|
|
op.drop_table("users")
|