65 lines
2.3 KiB
Python
65 lines
2.3 KiB
Python
"""Initial PostgreSQL schema for lab 2.
|
|
|
|
Revision ID: 20261019_0001
|
|
Revises:
|
|
Create Date: 2026-10-19
|
|
"""
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
|
|
|
|
revision = '20261019_0001'
|
|
down_revision = None
|
|
branch_labels = None
|
|
depends_on = None
|
|
|
|
|
|
def upgrade() -> None:
|
|
op.create_table(
|
|
'users',
|
|
sa.Column('id', sa.Uuid(), nullable=False),
|
|
sa.Column('username', sa.String(length=32), nullable=False),
|
|
sa.Column('display_name', sa.String(length=100), nullable=False),
|
|
sa.Column('password_hash', sa.Text(), nullable=False),
|
|
sa.Column('created_at', sa.DateTime(timezone=True), nullable=False),
|
|
sa.PrimaryKeyConstraint('id'),
|
|
sa.UniqueConstraint('username'),
|
|
)
|
|
op.create_table(
|
|
'chats',
|
|
sa.Column('id', sa.Uuid(), nullable=False),
|
|
sa.Column('title', sa.String(length=100), nullable=False),
|
|
sa.Column('created_at', sa.DateTime(timezone=True), nullable=False),
|
|
sa.PrimaryKeyConstraint('id'),
|
|
)
|
|
op.create_table(
|
|
'chat_members',
|
|
sa.Column('chat_id', sa.Uuid(), nullable=False),
|
|
sa.Column('user_id', sa.Uuid(), nullable=False),
|
|
sa.ForeignKeyConstraint(['chat_id'], ['chats.id'], ondelete='CASCADE'),
|
|
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'),
|
|
sa.PrimaryKeyConstraint('chat_id', 'user_id'),
|
|
)
|
|
op.create_index('chat_members_user_chat_idx', 'chat_members', ['user_id', 'chat_id'])
|
|
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.String(length=4000), nullable=False),
|
|
sa.Column('created_at', sa.DateTime(timezone=True), nullable=False),
|
|
sa.ForeignKeyConstraint(['chat_id'], ['chats.id'], ondelete='CASCADE'),
|
|
sa.ForeignKeyConstraint(['sender_id'], ['users.id']),
|
|
sa.PrimaryKeyConstraint('id'),
|
|
)
|
|
op.create_index('messages_history_idx', 'messages', ['chat_id', 'created_at', 'id'])
|
|
|
|
|
|
def downgrade() -> None:
|
|
op.drop_index('messages_history_idx', table_name='messages')
|
|
op.drop_table('messages')
|
|
op.drop_index('chat_members_user_chat_idx', table_name='chat_members')
|
|
op.drop_table('chat_members')
|
|
op.drop_table('chats')
|
|
op.drop_table('users')
|