Files
ds-chat/backend/alembic/versions/5b1a142dc271_message_file_attachments.py
ksmithandClaude Sonnet 5 c78d7454b6 Add generic file attachments to chat messages
Messages can now carry an arbitrary file (MessageFile), parallel to the
existing MessageImage feature rather than a refactor of it. Files serve
with Content-Disposition: attachment to force a download and prevent an
uploaded HTML/SVG from executing same-origin. No content-type allowlist,
same 8MB cap as images for now (a separate size-limit redesign is tracked
as its own issue).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-15 08:02:55 -06:00

55 lines
2.4 KiB
Python

"""message file attachments
Revision ID: 5b1a142dc271
Revises: 456cd78ca571
Create Date: 2026-08-14 21:53:05.055721
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = '5b1a142dc271'
down_revision: Union[str, Sequence[str], None] = '456cd78ca571'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
"""Upgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('message_files',
sa.Column('id', sa.Uuid(), nullable=False),
sa.Column('room_id', sa.Uuid(), nullable=False),
sa.Column('uploaded_by', sa.Uuid(), nullable=False),
sa.Column('storage_filename', sa.String(length=64), nullable=False),
sa.Column('original_filename', sa.String(length=255), nullable=False),
sa.Column('content_type', sa.String(length=150), nullable=False),
sa.Column('size_bytes', sa.BigInteger(), nullable=False),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
sa.ForeignKeyConstraint(['room_id'], ['rooms.id'], ),
sa.ForeignKeyConstraint(['uploaded_by'], ['users.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_message_files_room_id'), 'message_files', ['room_id'], unique=False)
op.add_column('messages', sa.Column('file_id', sa.Uuid(), nullable=True))
op.create_foreign_key('messages_file_id_fkey', 'messages', 'message_files', ['file_id'], ['id'])
op.drop_constraint(op.f('messages_content_or_image_required'), 'messages', type_='check')
op.create_check_constraint('messages_content_or_attachment_required', 'messages', 'content IS NOT NULL OR image_id IS NOT NULL OR file_id IS NOT NULL')
# ### end Alembic commands ###
def downgrade() -> None:
"""Downgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint('messages_content_or_attachment_required', 'messages', type_='check')
op.create_check_constraint(op.f('messages_content_or_image_required'), 'messages', 'content IS NOT NULL OR image_id IS NOT NULL')
op.drop_constraint('messages_file_id_fkey', 'messages', type_='foreignkey')
op.drop_column('messages', 'file_id')
op.drop_index(op.f('ix_message_files_room_id'), table_name='message_files')
op.drop_table('message_files')
# ### end Alembic commands ###