-- Level 4: the job row is the outbox. published_at is set only after a broker confirm. -- available_at holds the next backoff instant. runs counts entries into processing so a -- crash before ack cannot loop forever. Replay resets both counters. ALTER TABLE attachment_jobs ADD COLUMN available_at TIMESTAMPTZ; ALTER TABLE attachment_jobs ADD COLUMN last_error TEXT; ALTER TABLE attachment_jobs ADD COLUMN runs INTEGER NOT NULL DEFAULT 0; ALTER TABLE attachment_jobs ADD CONSTRAINT attachment_jobs_runs CHECK (runs >= 0); ALTER TABLE attachment_jobs ADD CONSTRAINT attachment_jobs_last_error CHECK (last_error IS NULL OR (char_length(last_error) >= 1 AND char_length(last_error) <= 64)); CREATE INDEX attachment_jobs_lease_idx ON attachment_jobs (lease_until) WHERE lease_until IS NOT NULL; -- Registry of terminal failures. The broker DLQ is a copy of the same fact. CREATE TABLE attachment_job_failures ( job_id UUID NOT NULL REFERENCES attachment_jobs (id), attempt INTEGER NOT NULL, error_code TEXT NOT NULL, permanent BOOLEAN NOT NULL, created_at TIMESTAMPTZ NOT NULL, PRIMARY KEY (job_id, attempt, error_code), CONSTRAINT attachment_job_failures_code CHECK (char_length(error_code) BETWEEN 1 AND 64) ); -- One row per operator replay decision, including a refusal to run an already ready job. CREATE TABLE attachment_job_replays ( id UUID PRIMARY KEY, job_id UUID NOT NULL REFERENCES attachment_jobs (id), attachment_id UUID NOT NULL REFERENCES attachments (id), outcome TEXT NOT NULL, created_at TIMESTAMPTZ NOT NULL, CONSTRAINT attachment_job_replays_outcome CHECK (outcome IN ('requeued', 'already_ready', 'not_failed')) ); -- Demo switches. Empty unless an operator runs `python -m app.fault`. Not a public API. CREATE TABLE lab_faults ( name TEXT PRIMARY KEY, armed_at TIMESTAMPTZ NOT NULL, CONSTRAINT lab_faults_name CHECK (name IN ( 'pause-outbox', 'commit-before-publish', 'exit-after-put', 'transient-once', 'permanent-once' )) );