forked from mtusi2026/mtusi2026lab3
80 lines
3.2 KiB
Python
80 lines
3.2 KiB
Python
"""create initial database schema
|
|
|
|
Revision ID: 21ad89656327
|
|
Revises:
|
|
Create Date: 2026-11-04 17:31:39.336368
|
|
|
|
"""
|
|
from typing import Sequence, Union
|
|
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
|
|
|
|
# revision identifiers, used by Alembic.
|
|
revision: str = '21ad89656327'
|
|
down_revision: Union[str, None] = None
|
|
branch_labels: Union[str, Sequence[str], None] = None
|
|
depends_on: Union[str, Sequence[str], None] = None
|
|
|
|
|
|
def upgrade() -> None:
|
|
# ### commands auto generated by Alembic - please adjust! ###
|
|
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('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.String(length=255), nullable=False),
|
|
sa.Column('created_at', sa.DateTime(timezone=True), nullable=False),
|
|
sa.PrimaryKeyConstraint('id')
|
|
)
|
|
op.create_index(op.f('ix_users_username'), 'users', ['username'], unique=True)
|
|
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_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.ForeignKeyConstraint(['chat_id'], ['chats.id'], ondelete='CASCADE'),
|
|
sa.ForeignKeyConstraint(['sender_id'], ['users.id'], ),
|
|
sa.PrimaryKeyConstraint('id')
|
|
)
|
|
op.create_index('ix_messages_chat_created_id', 'messages', ['chat_id', 'created_at', 'id'], unique=False)
|
|
op.create_table('sessions',
|
|
sa.Column('id', sa.UUID(), nullable=False),
|
|
sa.Column('token_hash', sa.String(length=64), nullable=False),
|
|
sa.Column('user_id', sa.UUID(), nullable=False),
|
|
sa.Column('expires_at', sa.DateTime(timezone=True), nullable=False),
|
|
sa.Column('created_at', sa.DateTime(timezone=True), nullable=False),
|
|
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'),
|
|
sa.PrimaryKeyConstraint('id')
|
|
)
|
|
op.create_index(op.f('ix_sessions_token_hash'), 'sessions', ['token_hash'], unique=True)
|
|
# ### end Alembic commands ###
|
|
|
|
|
|
def downgrade() -> None:
|
|
# ### commands auto generated by Alembic - please adjust! ###
|
|
op.drop_index(op.f('ix_sessions_token_hash'), table_name='sessions')
|
|
op.drop_table('sessions')
|
|
op.drop_index('ix_messages_chat_created_id', table_name='messages')
|
|
op.drop_table('messages')
|
|
op.drop_table('chat_members')
|
|
op.drop_index(op.f('ix_users_username'), table_name='users')
|
|
op.drop_table('users')
|
|
op.drop_table('chats')
|
|
# ### end Alembic commands ###
|