ALTER TABLE attachments DROP CONSTRAINT attachments_status_check; ALTER TABLE attachments ALTER COLUMN status SET DEFAULT 'queued'; ALTER TABLE attachments ADD CONSTRAINT attachments_status_check CHECK (status IN ('queued','processing','ready','failed')); ALTER TABLE attachments ADD COLUMN error_code text; ALTER TABLE attachments ADD COLUMN variants jsonb NOT NULL DEFAULT '[]'; CREATE TABLE attachment_jobs ( id uuid PRIMARY KEY, attachment_id uuid NOT NULL UNIQUE REFERENCES attachments(id) ON DELETE CASCADE, pipeline_version integer NOT NULL DEFAULT 1 CHECK (pipeline_version=1), staging_key text NOT NULL UNIQUE, state text NOT NULL DEFAULT 'queued' CHECK (state IN ('queued','processing','ready','failed')), attempts integer NOT NULL DEFAULT 0, max_attempts integer NOT NULL DEFAULT 5 CHECK (max_attempts BETWEEN 1 AND 20), next_at timestamptz NOT NULL DEFAULT clock_timestamp(), lease_until timestamptz, lease_token uuid, error_code text, staging_deleted_at timestamptz, created_at timestamptz NOT NULL DEFAULT clock_timestamp(), updated_at timestamptz NOT NULL DEFAULT clock_timestamp() ); CREATE INDEX attachment_jobs_due_idx ON attachment_jobs(state,next_at); -- Same transaction as attachment + job; repeated publication is intentional. CREATE TABLE attachment_outbox ( job_id uuid PRIMARY KEY REFERENCES attachment_jobs(id) ON DELETE CASCADE, published_at timestamptz, publications integer NOT NULL DEFAULT 0 ); CREATE TABLE attachment_replays ( id bigserial PRIMARY KEY, job_id uuid NOT NULL REFERENCES attachment_jobs(id), previous_error text NOT NULL, replayed_at timestamptz NOT NULL DEFAULT clock_timestamp() ); -- Internal CLI-only, one-shot crash injection. No HTTP endpoint exposes it. CREATE TABLE pipeline_faults (point text PRIMARY KEY, armed boolean NOT NULL DEFAULT true, hit_at timestamptz);