ALTER TABLE attachments ADD COLUMN status text NOT NULL DEFAULT 'queued' CHECK (status IN ('queued', 'processing', 'ready', 'failed')), ADD COLUMN variants jsonb NOT NULL DEFAULT '[]', ADD COLUMN error_code text, ADD CHECK ((status = 'failed') = (error_code IS NOT NULL)); CREATE TABLE attachment_jobs ( id uuid PRIMARY KEY, attachment_id uuid NOT NULL UNIQUE REFERENCES attachments(id), source_key text NOT NULL, attempts integer NOT NULL DEFAULT 0 CHECK (attempts >= 0), created_at timestamptz NOT NULL DEFAULT clock_timestamp(), publish_at timestamptz DEFAULT clock_timestamp() ); CREATE INDEX attachment_jobs_pending_idx ON attachment_jobs(publish_at, id) WHERE publish_at IS NOT NULL; -- Existing Lab 4 originals can be used as input without copying or deleting them. INSERT INTO attachment_jobs(id, attachment_id, source_key) SELECT gen_random_uuid(), id, object_key FROM attachments;