feat: implement S3 attachments and presigned uploads
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
"""add attachment metadata"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from typing import Sequence, Union
|
||||
|
||||
revision: str = "4d9a1f2c7b10"
|
||||
down_revision: Union[str, None] = "8c3d9f4a21b7"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
def upgrade() -> None:
|
||||
op.add_column("messages", sa.Column("attachment_ids", sa.JSON(), nullable=False, server_default="[]"))
|
||||
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(255), nullable=False),
|
||||
sa.Column("content_type", sa.String(32), nullable=False),
|
||||
sa.Column("size_bytes", sa.Integer(), nullable=False),
|
||||
sa.Column("sha256", sa.String(64), nullable=False),
|
||||
sa.Column("object_key", sa.String(512), nullable=False),
|
||||
sa.Column("status", sa.String(16), nullable=False),
|
||||
sa.Column("error_code", sa.String(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"], ondelete="CASCADE"),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
sa.UniqueConstraint("object_key"),
|
||||
)
|
||||
op.create_index("ix_attachments_chat_id", "attachments", ["chat_id"])
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_index("ix_attachments_chat_id", table_name="attachments")
|
||||
op.drop_table("attachments")
|
||||
op.drop_column("messages", "attachment_ids")
|
||||
@@ -0,0 +1,78 @@
|
||||
"""add attachment uploads
|
||||
|
||||
Revision ID: a7c2e91d5f40
|
||||
Revises: 4d9a1f2c7b10
|
||||
"""
|
||||
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
revision: str = "a7c2e91d5f40"
|
||||
down_revision: Union[str, None] = "4d9a1f2c7b10"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.create_table(
|
||||
"attachment_uploads",
|
||||
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("content_type", sa.String(length=32), nullable=False),
|
||||
sa.Column("expected_size_bytes", sa.Integer(), nullable=False),
|
||||
sa.Column("expected_sha256", sa.String(length=64), nullable=False),
|
||||
sa.Column("object_key", sa.String(length=512), nullable=False),
|
||||
sa.Column("status", sa.String(length=16), nullable=False),
|
||||
sa.Column("expires_at", sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column("completed_at", sa.DateTime(timezone=True), nullable=True),
|
||||
sa.ForeignKeyConstraint(
|
||||
["chat_id"],
|
||||
["chats.id"],
|
||||
ondelete="CASCADE",
|
||||
),
|
||||
sa.ForeignKeyConstraint(
|
||||
["owner_id"],
|
||||
["users.id"],
|
||||
ondelete="CASCADE",
|
||||
),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
sa.UniqueConstraint("object_key"),
|
||||
)
|
||||
|
||||
op.create_index(
|
||||
"ix_attachment_uploads_chat_id",
|
||||
"attachment_uploads",
|
||||
["chat_id"],
|
||||
)
|
||||
op.create_index(
|
||||
"ix_attachment_uploads_owner_id",
|
||||
"attachment_uploads",
|
||||
["owner_id"],
|
||||
)
|
||||
op.create_index(
|
||||
"ix_attachment_uploads_expires_at",
|
||||
"attachment_uploads",
|
||||
["expires_at"],
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_index(
|
||||
"ix_attachment_uploads_expires_at",
|
||||
table_name="attachment_uploads",
|
||||
)
|
||||
op.drop_index(
|
||||
"ix_attachment_uploads_owner_id",
|
||||
table_name="attachment_uploads",
|
||||
)
|
||||
op.drop_index(
|
||||
"ix_attachment_uploads_chat_id",
|
||||
table_name="attachment_uploads",
|
||||
)
|
||||
op.drop_table("attachment_uploads")
|
||||
Reference in New Issue
Block a user