57 lines
1.9 KiB
Python
57 lines
1.9 KiB
Python
"""add attachments table
|
|
|
|
Revision ID: c3f18af4ec56
|
|
Revises: 2803f3a0e579
|
|
Create Date: <оставь>
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from alembic import op
|
|
|
|
revision = "c3f18af4ec56"
|
|
down_revision = "2803f3a0e579"
|
|
branch_labels = None
|
|
depends_on = None
|
|
|
|
|
|
def upgrade() -> None:
|
|
op.execute("""
|
|
CREATE TABLE attachments (
|
|
id UUID PRIMARY KEY,
|
|
chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
|
|
owner_id UUID NOT NULL REFERENCES users(id),
|
|
filename TEXT NOT NULL,
|
|
content_type TEXT NOT NULL CHECK (content_type IN ('image/png','image/jpeg')),
|
|
size_bytes INTEGER NOT NULL CHECK (size_bytes > 0 AND size_bytes <= 10485760),
|
|
sha256 TEXT NOT NULL CHECK (sha256 ~ '^[a-f0-9]{64}$'),
|
|
s3_key TEXT NOT NULL UNIQUE,
|
|
status TEXT NOT NULL DEFAULT 'ready',
|
|
error_code TEXT,
|
|
created_at TIMESTAMPTZ NOT NULL,
|
|
updated_at TIMESTAMPTZ NOT NULL
|
|
)
|
|
""")
|
|
op.execute("""
|
|
CREATE INDEX attachments_chat_idx ON attachments (chat_id)
|
|
""")
|
|
op.execute("""
|
|
CREATE INDEX attachments_owner_idx ON attachments (owner_id)
|
|
""")
|
|
op.execute("""
|
|
CREATE TABLE message_attachments (
|
|
message_id UUID NOT NULL REFERENCES messages(id) ON DELETE CASCADE,
|
|
attachment_id UUID NOT NULL REFERENCES attachments(id) ON DELETE CASCADE,
|
|
position INTEGER NOT NULL,
|
|
PRIMARY KEY (message_id, attachment_id)
|
|
)
|
|
""")
|
|
op.execute("""
|
|
CREATE INDEX message_attachments_attachment_idx ON message_attachments (attachment_id)
|
|
""")
|
|
|
|
|
|
def downgrade() -> None:
|
|
op.execute("DROP TABLE IF EXISTS message_attachments")
|
|
op.execute("DROP INDEX IF EXISTS attachments_owner_idx")
|
|
op.execute("DROP INDEX IF EXISTS attachments_chat_idx")
|
|
op.execute("DROP TABLE IF EXISTS attachments") |