CREATE TABLE IF NOT EXISTS 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, object_key text UNIQUE NOT NULL, content_type text NOT NULL, size_bytes bigint NOT NULL, sha256 text NOT NULL, status text NOT NULL DEFAULT 'ready', error_code text, created_at timestamptz NOT NULL DEFAULT clock_timestamp(), updated_at timestamptz NOT NULL DEFAULT clock_timestamp() ); ALTER TABLE messages ADD COLUMN IF NOT EXISTS attachment_ids uuid[] NOT NULL DEFAULT '{}'; CREATE INDEX IF NOT EXISTS attachments_chat ON attachments(chat_id);