Files
mtusi2026lab4/migrations/003_attachments.sql
T
2026-11-12 14:37:43 +03:00

13 lines
825 B
SQL

ALTER TABLE messages DROP CONSTRAINT IF EXISTS messages_text_check;
ALTER TABLE messages ADD COLUMN IF NOT EXISTS attachment_ids uuid[] NOT NULL DEFAULT '{}';
ALTER TABLE messages ADD CONSTRAINT messages_content_check CHECK (length(trim(text))>0 OR cardinality(attachment_ids)>0);
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), filename varchar(255) NOT NULL,
content_type varchar(20) NOT NULL, size_bytes integer NOT NULL,
sha256 char(64) NOT NULL, status varchar(20) NOT NULL DEFAULT 'ready',
object_key text NOT NULL UNIQUE, created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX IF NOT EXISTS idx_attachments_chat ON attachments(chat_id);