ALTER TABLE attachments ADD COLUMN status text NOT NULL DEFAULT 'ready' CHECK (status IN ('queued','processing','ready','failed')); ALTER TABLE attachments ADD COLUMN error_code text; ALTER TABLE attachments ADD COLUMN staging_deleted boolean NOT NULL DEFAULT false; CREATE TABLE attachment_jobs ( id uuid PRIMARY KEY, attachment_id uuid NOT NULL UNIQUE REFERENCES attachments(id), pipeline_version integer NOT NULL DEFAULT 1 CHECK (pipeline_version = 1), staging_key text NOT NULL UNIQUE, attempt integer NOT NULL DEFAULT 0, next_attempt_at timestamptz NOT NULL DEFAULT clock_timestamp(), published_at timestamptz, created_at timestamptz NOT NULL DEFAULT clock_timestamp() ); CREATE TABLE attachment_variants ( attachment_id uuid NOT NULL REFERENCES attachments(id), pipeline_version integer NOT NULL DEFAULT 1, name text NOT NULL CHECK (name = 'thumbnail'), content_type text NOT NULL, width integer NOT NULL, height integer NOT NULL, size_bytes integer NOT NULL, sha256 char(64) NOT NULL, object_key text NOT NULL UNIQUE, PRIMARY KEY (attachment_id,pipeline_version,name) );