Files
mtusi2026lab4/migrations/004_attachments.sql
T

36 lines
1.4 KiB
SQL

CREATE TABLE attachments (
id uuid PRIMARY KEY,
chat_id uuid NOT NULL REFERENCES chats(id),
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 bigint NOT NULL CHECK (size_bytes BETWEEN 1 AND 10485760),
sha256 text NOT NULL,
status text NOT NULL CONSTRAINT attachments_status_check CHECK (status IN ('queued','processing','ready','failed')),
error_code text,
created_at timestamptz NOT NULL,
updated_at timestamptz NOT NULL,
object_key text NOT NULL UNIQUE,
UNIQUE(id,chat_id)
);
CREATE TABLE message_attachments (
message_id uuid NOT NULL REFERENCES messages(id),
attachment_id uuid NOT NULL REFERENCES attachments(id),
position integer NOT NULL CHECK (position BETWEEN 0 AND 9),
PRIMARY KEY(message_id,attachment_id),
UNIQUE(message_id,position)
);
CREATE INDEX message_attachments_attachment_idx ON message_attachments(attachment_id);
CREATE TABLE upload_intents (
id uuid PRIMARY KEY,
object_key text NOT NULL UNIQUE,
created_at timestamptz NOT NULL
);
CREATE INDEX upload_intents_created_idx ON upload_intents(created_at,id);
CREATE TABLE attachment_reconcile_audit (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
event text NOT NULL,
object_key text NOT NULL,
happened_at timestamptz NOT NULL DEFAULT now()
);