Files
mtusi2026lab4/migrations/003_attachments.sql
T

26 lines
1.0 KiB
SQL

CREATE TABLE IF NOT EXISTS attachments (
id UUID PRIMARY KEY,
chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
owner_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
filename VARCHAR(255) NOT NULL,
content_type VARCHAR(32) NOT NULL,
size_bytes INTEGER NOT NULL,
sha256 CHAR(64) NOT NULL,
object_key VARCHAR(255) NOT NULL UNIQUE,
status VARCHAR(16) NOT NULL DEFAULT 'ready',
error_code VARCHAR(64),
created_at TIMESTAMPTZ NOT NULL,
updated_at TIMESTAMPTZ NOT NULL,
CHECK (content_type IN ('image/png', 'image/jpeg')),
CHECK (size_bytes > 0 AND size_bytes <= 10485760),
CHECK (status IN ('ready', 'failed'))
);
CREATE INDEX IF NOT EXISTS attachments_chat_idx ON attachments(chat_id, created_at, id);
CREATE TABLE IF NOT EXISTS message_attachments (
message_id UUID NOT NULL REFERENCES messages(id) ON DELETE CASCADE,
attachment_id UUID NOT NULL REFERENCES attachments(id) ON DELETE RESTRICT,
PRIMARY KEY (message_id, attachment_id)
);