CREATE TABLE attachments ( id uuid PRIMARY KEY, chat_id uuid NOT NULL REFERENCES chats(id), owner_id uuid NOT NULL, filename text NOT NULL, content_type text NOT NULL CHECK (content_type IN ('image/png', 'image/jpeg')), size_bytes integer NOT NULL CHECK (size_bytes BETWEEN 1 AND 10485760), sha256 text NOT NULL, status text NOT NULL DEFAULT 'ready' CHECK (status IN ('ready', 'failed')), variants jsonb NOT NULL DEFAULT '[]'::jsonb, error_code text, 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_recent ON attachments(created_at, id); ALTER TABLE messages ADD COLUMN attachment_ids uuid[] NOT NULL DEFAULT ARRAY[]::uuid[];