Files
mtusi2026lab4/migrations/003_attachments.sql

35 lines
1.0 KiB
SQL

CREATE TABLE IF NOT EXISTS attachments (
id UUID PRIMARY KEY,
chat_id UUID NOT NULL REFERENCES chats(id),
uploader_id UUID NOT NULL REFERENCES users(id),
filename TEXT NOT NULL,
content_type TEXT NOT NULL,
size_bytes BIGINT NOT NULL,
sha256 CHAR(64) NOT NULL,
object_key TEXT NOT NULL UNIQUE,
width INTEGER NOT NULL,
height INTEGER NOT NULL,
status TEXT NOT NULL DEFAULT 'ready',
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
CONSTRAINT attachment_size_valid
CHECK (size_bytes BETWEEN 1 AND 10485760),
CONSTRAINT attachment_dimensions_valid
CHECK (width > 0 AND height > 0),
CONSTRAINT attachment_status_valid
CHECK (status IN ('ready', 'failed'))
);
CREATE INDEX IF NOT EXISTS idx_attachments_chat
ON attachments(chat_id);
CREATE TABLE IF NOT EXISTS message_attachments (
message_id UUID NOT NULL REFERENCES messages(id),
attachment_id UUID NOT NULL REFERENCES attachments(id),
PRIMARY KEY (message_id, attachment_id)
);