79 lines
2.4 KiB
Python
79 lines
2.4 KiB
Python
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
|
|
|
|
revision = "c8f42a19e6b0"
|
|
down_revision = "b3a91c0e7d42"
|
|
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("uploader_id", sa.Uuid(), nullable=False),
|
|
sa.Column("filename", sa.String(length=255), nullable=False),
|
|
sa.Column("content_type", sa.String(length=100), nullable=False),
|
|
sa.Column("size_bytes", sa.BigInteger(), nullable=False),
|
|
sa.Column("s3_key", sa.String(length=512), nullable=False),
|
|
sa.Column("status", sa.String(length=20), nullable=False),
|
|
sa.Column(
|
|
"created_at",
|
|
sa.DateTime(timezone=True),
|
|
server_default=sa.text("CURRENT_TIMESTAMP"),
|
|
nullable=False,
|
|
),
|
|
sa.ForeignKeyConstraint(
|
|
["chat_id"],
|
|
["chats.id"],
|
|
ondelete="CASCADE",
|
|
),
|
|
sa.ForeignKeyConstraint(
|
|
["uploader_id"],
|
|
["users.id"],
|
|
ondelete="RESTRICT",
|
|
),
|
|
sa.PrimaryKeyConstraint("id"),
|
|
sa.UniqueConstraint("s3_key", name="uq_attachments_s3_key"),
|
|
sa.CheckConstraint(
|
|
"size_bytes > 0 AND size_bytes <= 10485760",
|
|
name="ck_attachments_size",
|
|
),
|
|
sa.CheckConstraint(
|
|
"status IN ('pending', 'ready')",
|
|
name="ck_attachments_status",
|
|
),
|
|
)
|
|
|
|
op.create_index(
|
|
"ix_attachments_chat_id",
|
|
"attachments",
|
|
["chat_id"],
|
|
)
|
|
|
|
op.create_table(
|
|
"message_attachments",
|
|
sa.Column("message_id", sa.Uuid(), nullable=False),
|
|
sa.Column("attachment_id", sa.Uuid(), nullable=False),
|
|
sa.ForeignKeyConstraint(
|
|
["message_id"],
|
|
["messages.id"],
|
|
ondelete="CASCADE",
|
|
),
|
|
sa.ForeignKeyConstraint(
|
|
["attachment_id"],
|
|
["attachments.id"],
|
|
ondelete="RESTRICT",
|
|
),
|
|
sa.PrimaryKeyConstraint("message_id", "attachment_id"),
|
|
)
|
|
|
|
|
|
def downgrade() -> None:
|
|
op.drop_table("message_attachments")
|
|
op.drop_index("ix_attachments_chat_id", table_name="attachments")
|
|
op.drop_table("attachments")
|