47 lines
2.4 KiB
SQL
47 lines
2.4 KiB
SQL
-- Preserve earlier migration checksums; transition lab4 storage to versioned jobs.
|
|
ALTER TABLE attachments DROP CONSTRAINT attachments_storage_state_check;
|
|
ALTER TABLE attachments ADD CHECK (storage_state IN ('queued','processing','ready','failed')) NOT VALID;
|
|
UPDATE attachments SET storage_state='queued' WHERE storage_state IN ('pending','missing');
|
|
ALTER TABLE attachments ADD COLUMN pipeline_version integer NOT NULL DEFAULT 1 CHECK (pipeline_version > 0);
|
|
ALTER TABLE attachments ADD COLUMN variants jsonb NOT NULL DEFAULT '[]' CHECK (jsonb_typeof(variants)='array');
|
|
ALTER TABLE attachments ADD COLUMN error_code text;
|
|
CREATE TABLE attachment_jobs (
|
|
job_id uuid PRIMARY KEY,
|
|
attachment_id uuid NOT NULL REFERENCES attachments(id) ON DELETE CASCADE,
|
|
pipeline_version integer NOT NULL CHECK (pipeline_version > 0),
|
|
source_key text NOT NULL,
|
|
state text NOT NULL DEFAULT 'queued' CHECK (state IN ('queued','processing','ready','failed','superseded')),
|
|
attempt integer NOT NULL DEFAULT 0 CHECK (attempt >= 0),
|
|
fence uuid,
|
|
lease_until timestamptz,
|
|
next_attempt_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
|
error_code text,
|
|
source_expired boolean NOT NULL DEFAULT false,
|
|
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
|
updated_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
|
UNIQUE (attachment_id,pipeline_version)
|
|
);
|
|
CREATE INDEX jobs_recovery ON attachment_jobs(state,lease_until,next_attempt_at);
|
|
CREATE TABLE job_outbox (
|
|
job_id uuid PRIMARY KEY REFERENCES attachment_jobs(job_id) ON DELETE CASCADE,
|
|
due_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
|
published_at timestamptz,
|
|
stream_id text
|
|
);
|
|
CREATE INDEX outbox_due ON job_outbox(due_at) WHERE published_at IS NULL;
|
|
CREATE TABLE pipeline_audit (
|
|
sequence bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
|
job_id uuid NOT NULL REFERENCES attachment_jobs(job_id) ON DELETE CASCADE,
|
|
event text NOT NULL,
|
|
details jsonb NOT NULL DEFAULT '{}',
|
|
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
|
|
);
|
|
-- Only an operator with DB access can configure faults; production ignores this table.
|
|
CREATE TABLE pipeline_faults (
|
|
job_id uuid REFERENCES attachment_jobs(job_id) ON DELETE CASCADE,
|
|
phase text NOT NULL,
|
|
action text NOT NULL CHECK (action IN ('crash','hang','transient','permanent')),
|
|
remaining integer NOT NULL DEFAULT 1 CHECK (remaining >= 0),
|
|
PRIMARY KEY(job_id,phase)
|
|
);
|