Files
mtusi2026lab5/migrations/0005_async_attachments.sql
T

92 lines
4.8 KiB
SQL

-- PostgreSQL owns the logical job and its publication intent. The broker is a
-- durable delivery mechanism; it is never the only copy of accepted work.
ALTER TABLE attachments ADD COLUMN status text NOT NULL DEFAULT 'queued'
CHECK (status IN ('queued', 'processing', 'ready', 'failed'));
ALTER TABLE attachments ADD COLUMN pipeline_version integer NOT NULL DEFAULT 1 CHECK (pipeline_version >= 1);
ALTER TABLE attachments ADD COLUMN error_code text
CHECK (error_code IS NULL OR error_code ~ '^[a-z][a-z0-9_]{0,63}$');
ALTER TABLE attachments ADD COLUMN staging_key text;
UPDATE attachments SET staging_key = object_key;
ALTER TABLE attachments ALTER COLUMN staging_key SET NOT NULL;
ALTER TABLE attachments ADD COLUMN staging_cleanup_pending boolean NOT NULL DEFAULT false;
ALTER TABLE attachments ADD COLUMN staging_deleted_at timestamptz;
ALTER TABLE attachments ADD CONSTRAINT attachments_result_state CHECK (
(status <> 'ready' OR error_code IS NULL) AND
(status <> 'failed' OR error_code IS NOT NULL) AND
(NOT staging_cleanup_pending OR status = 'ready')
);
CREATE TABLE attachment_jobs (
id uuid PRIMARY KEY,
attachment_id uuid NOT NULL UNIQUE REFERENCES attachments (id),
pipeline_version integer NOT NULL DEFAULT 1 CHECK (pipeline_version >= 1),
status text NOT NULL DEFAULT 'queued' CHECK (status IN ('queued', 'processing', 'ready', 'failed')),
-- attempt is monotonic, including an explicit replay; its retry budget is separate.
attempt integer NOT NULL DEFAULT 0 CHECK (attempt >= 0),
run_attempts integer NOT NULL DEFAULT 0 CHECK (run_attempts >= 0 AND run_attempts <= attempt),
available_at timestamptz NOT NULL DEFAULT clock_timestamp(),
worker_id text,
lease_token uuid,
lease_until timestamptz,
error_code text CHECK (error_code IS NULL OR error_code ~ '^[a-z][a-z0-9_]{0,63}$'),
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
updated_at timestamptz NOT NULL DEFAULT clock_timestamp(),
CHECK ((status = 'processing') = (worker_id IS NOT NULL AND lease_token IS NOT NULL AND lease_until IS NOT NULL))
);
CREATE INDEX attachment_jobs_expired_idx ON attachment_jobs (lease_until) WHERE status = 'processing';
CREATE INDEX attachment_jobs_failed_idx ON attachment_jobs (updated_at) WHERE status = 'failed';
CREATE TABLE attachment_outbox (
id uuid PRIMARY KEY,
job_id uuid NOT NULL REFERENCES attachment_jobs (id),
attempt integer NOT NULL CHECK (attempt >= 1),
available_at timestamptz NOT NULL DEFAULT clock_timestamp(),
published_at timestamptz,
worker_id text,
lease_token uuid,
lease_until timestamptz,
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
UNIQUE (job_id, attempt),
CHECK ((lease_token IS NULL) = (lease_until IS NULL)),
CHECK ((lease_token IS NULL) = (worker_id IS NULL))
);
CREATE INDEX attachment_outbox_pending_idx ON attachment_outbox (available_at, created_at)
WHERE published_at IS NULL;
CREATE TABLE attachment_variants (
attachment_id uuid NOT NULL REFERENCES attachments (id),
pipeline_version integer NOT NULL CHECK (pipeline_version >= 1),
name text NOT NULL CHECK (name IN ('thumbnail', 'crop', 'optimized')),
object_key text NOT NULL UNIQUE,
content_type text NOT NULL CHECK (content_type IN ('image/jpeg', 'image/png', 'image/webp')),
size_bytes integer NOT NULL CHECK (size_bytes > 0),
width integer NOT NULL CHECK (width > 0),
height integer NOT NULL CHECK (height > 0),
sha256 text NOT NULL CHECK (sha256 ~ '^[a-f0-9]{64}$'),
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
PRIMARY KEY (attachment_id, pipeline_version, name)
);
CREATE INDEX attachment_staging_cleanup_idx ON attachments (updated_at) WHERE staging_cleanup_pending;
CREATE TABLE job_audit (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
job_id uuid NOT NULL REFERENCES attachment_jobs (id),
action text NOT NULL,
actor text NOT NULL CHECK (char_length(actor) BETWEEN 1 AND 120),
reason text NOT NULL CHECK (char_length(reason) BETWEEN 1 AND 500),
details jsonb NOT NULL DEFAULT '{}',
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
);
-- Explicit operator fault hooks are persisted and consumed once across restarts.
CREATE TABLE job_faults (
job_id uuid NOT NULL REFERENCES attachment_jobs (id),
action text NOT NULL CHECK (action IN ('crash_after_objects', 'crash_after_ready', 'transient', 'permanent', 'stall_processing')),
remaining integer NOT NULL CHECK (remaining BETWEEN 0 AND 100),
PRIMARY KEY (job_id, action)
);
-- An upgrade from lab4 also schedules its existing originals for the new pipeline.
INSERT INTO attachment_jobs (id, attachment_id) SELECT gen_random_uuid(), id FROM attachments;
INSERT INTO attachment_outbox (id, job_id, attempt) SELECT gen_random_uuid(), id, 1 FROM attachment_jobs;