Files
mtusi2026lab4/migrations/002_attachments.sql
T

19 lines
1.0 KiB
SQL

ALTER TABLE messages DROP CONSTRAINT messages_text_check;
ALTER TABLE messages ADD COLUMN attachment_ids uuid[] NOT NULL DEFAULT '{}';
ALTER TABLE messages ADD CHECK (length(trim(text))>0 OR cardinality(attachment_ids)>0);
CREATE TABLE idempotency (
user_id uuid REFERENCES users(id), chat_id uuid REFERENCES chats(id), key varchar(128),
body text NOT NULL, response jsonb NOT NULL,
expires_at timestamptz NOT NULL DEFAULT clock_timestamp()+interval '24 hours',
PRIMARY KEY(user_id,chat_id,key)
);
CREATE TABLE attachments (
id uuid PRIMARY KEY, chat_id uuid NOT NULL REFERENCES chats(id), owner_id uuid NOT NULL REFERENCES users(id),
filename varchar(255) NOT NULL, object_key text NOT NULL UNIQUE,
content_type text NOT NULL, size_bytes bigint NOT NULL, sha256 char(64) NOT NULL,
status text NOT NULL CHECK(status IN ('pending','ready')),
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
updated_at timestamptz NOT NULL DEFAULT clock_timestamp()
);
CREATE INDEX attachments_pending ON attachments(created_at) WHERE status='pending';