forked from mtusi2026/mtusi2026lab4
70 lines
2.6 KiB
Python
70 lines
2.6 KiB
Python
"""Add S3 attachment metadata and message links for lab 4.
|
|
|
|
Revision ID: 20261030_0003
|
|
Revises: 20261023_0002
|
|
Create Date: 2026-10-30
|
|
"""
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
|
|
|
|
revision = '20261030_0003'
|
|
down_revision = '20261023_0002'
|
|
branch_labels = None
|
|
depends_on = None
|
|
|
|
|
|
def upgrade() -> None:
|
|
op.create_table(
|
|
'attachments',
|
|
sa.Column('id', sa.Uuid(), nullable=False),
|
|
sa.Column('chat_id', sa.Uuid(), nullable=False),
|
|
sa.Column('owner_id', sa.Uuid(), nullable=False),
|
|
sa.Column('filename', sa.String(length=255), nullable=False),
|
|
sa.Column('object_key', sa.String(length=255), nullable=False),
|
|
sa.Column('content_type', sa.String(length=32), nullable=False),
|
|
sa.Column('size_bytes', sa.BigInteger(), nullable=False),
|
|
sa.Column('sha256', sa.String(length=64), nullable=False),
|
|
sa.Column('width', sa.Integer(), nullable=False),
|
|
sa.Column('height', sa.Integer(), nullable=False),
|
|
sa.Column('status', sa.String(length=16), nullable=False),
|
|
sa.Column('error_code', sa.String(length=64), nullable=True),
|
|
sa.Column('created_at', sa.DateTime(timezone=True), nullable=False),
|
|
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=False),
|
|
sa.ForeignKeyConstraint(['chat_id'], ['chats.id'], ondelete='CASCADE'),
|
|
sa.ForeignKeyConstraint(['owner_id'], ['users.id']),
|
|
sa.PrimaryKeyConstraint('id'),
|
|
sa.UniqueConstraint('object_key', name='attachments_object_key_key'),
|
|
)
|
|
op.create_index(
|
|
'attachments_chat_created_idx',
|
|
'attachments',
|
|
['chat_id', 'created_at', 'id'],
|
|
)
|
|
op.create_table(
|
|
'message_attachments',
|
|
sa.Column('message_id', sa.Uuid(), nullable=False),
|
|
sa.Column('attachment_id', sa.Uuid(), nullable=False),
|
|
sa.Column('position', sa.Integer(), nullable=False),
|
|
sa.ForeignKeyConstraint(['attachment_id'], ['attachments.id'], ondelete='CASCADE'),
|
|
sa.ForeignKeyConstraint(['message_id'], ['messages.id'], ondelete='CASCADE'),
|
|
sa.PrimaryKeyConstraint('message_id', 'attachment_id'),
|
|
sa.UniqueConstraint(
|
|
'message_id',
|
|
'position',
|
|
name='message_attachments_message_position_key',
|
|
),
|
|
)
|
|
op.create_index(
|
|
'message_attachments_attachment_idx',
|
|
'message_attachments',
|
|
['attachment_id'],
|
|
)
|
|
|
|
|
|
def downgrade() -> None:
|
|
op.drop_index('message_attachments_attachment_idx', table_name='message_attachments')
|
|
op.drop_table('message_attachments')
|
|
op.drop_index('attachments_chat_created_idx', table_name='attachments')
|
|
op.drop_table('attachments')
|