-- +goose Up 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 CHECK(char_length(filename) BETWEEN 1 AND 255), 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 CHECK(sha256 ~ '^[a-f0-9]{64}$'), status text NOT NULL CHECK(status IN ('uploading','ready','missing','abandoned')), created_at timestamptz NOT NULL DEFAULT clock_timestamp(), updated_at timestamptz NOT NULL DEFAULT clock_timestamp(), FOREIGN KEY(chat_id,owner_id) REFERENCES chat_members(chat_id,user_id) ); CREATE INDEX attachments_owner ON attachments(owner_id,id); CREATE TABLE message_attachments ( message_id uuid NOT NULL REFERENCES messages(id), attachment_id uuid NOT NULL REFERENCES attachments(id), ordinal integer NOT NULL CHECK(ordinal BETWEEN 0 AND 9), PRIMARY KEY(message_id,ordinal), UNIQUE(message_id,attachment_id) ); CREATE INDEX attachment_references ON message_attachments(attachment_id); -- +goose Down DROP TABLE message_attachments; DROP TABLE attachments;