ALTER TABLE attachments ADD COLUMN status text NOT NULL DEFAULT 'receiving' CHECK (status IN ('receiving','queued','processing','ready','failed')); UPDATE attachments SET status='ready' WHERE ready; ALTER TABLE attachments ADD COLUMN variants jsonb NOT NULL DEFAULT '[]'; ALTER TABLE attachments ADD COLUMN error_code text; CREATE TABLE jobs ( id uuid PRIMARY KEY DEFAULT gen_random_uuid(), attachment_id uuid NOT NULL UNIQUE REFERENCES attachments(id), attempt integer NOT NULL DEFAULT 0, next_attempt timestamptz NOT NULL DEFAULT now(), published_at timestamptz, lease_until timestamptz, finished boolean NOT NULL DEFAULT false ); CREATE TABLE job_replays ( id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, job_id uuid NOT NULL REFERENCES jobs(id), replayed_at timestamptz NOT NULL DEFAULT now() );