-- lab5: asynchronous processing. Upload = staging object + attachment(queued) + job + outbox row in ONE transaction. -- Lab4 'pending' rows were invisible intents of an unfinished upload; they have no lab5 meaning. DELETE FROM attachments WHERE status = 'pending'; ALTER TABLE attachments DROP CONSTRAINT attachments_status; ALTER TABLE attachments ALTER COLUMN status SET DEFAULT 'queued'; ALTER TABLE attachments ADD CONSTRAINT attachments_status CHECK (status IN ('queued', 'processing', 'ready', 'failed')); -- staging_key: private S3 prefix staging/ where the API puts the full original before 202. -- object_key: final key of the published original (written by the worker, served only when status = ready). -- pipeline_version: version whose variants are currently published (NULL until the first ready). ALTER TABLE attachments ADD COLUMN staging_key text; ALTER TABLE attachments ADD COLUMN staging_deleted_at timestamptz; ALTER TABLE attachments ADD COLUMN error_code text; ALTER TABLE attachments ADD COLUMN pipeline_version integer; ALTER TABLE attachments ADD CONSTRAINT attachments_error_code CHECK (error_code IS NULL OR error_code ~ '^[a-z_]{1,64}$'); DROP INDEX IF EXISTS attachments_pending_idx; CREATE INDEX attachments_staging_idx ON attachments (status, updated_at) WHERE staging_deleted_at IS NULL; -- One logical unit of work = (attachment, pipeline_version). job_id never changes on redelivery or retry. CREATE TABLE jobs ( job_id uuid PRIMARY KEY, attachment_id uuid NOT NULL REFERENCES attachments (id), pipeline_version integer NOT NULL CHECK (pipeline_version >= 1), status text NOT NULL DEFAULT 'queued', attempt integer NOT NULL DEFAULT 0, -- attempts already started (fencing token together with job_id) max_attempts integer NOT NULL, -- raised by an explicit operator replay next_attempt_at timestamptz NOT NULL DEFAULT clock_timestamp(), lease_owner text, lease_until timestamptz, last_error text, error_code text, replays integer NOT NULL DEFAULT 0, created_at timestamptz NOT NULL DEFAULT clock_timestamp(), updated_at timestamptz NOT NULL DEFAULT clock_timestamp(), CONSTRAINT jobs_effect_key UNIQUE (attachment_id, pipeline_version), CONSTRAINT jobs_status CHECK (status IN ('queued', 'processing', 'done', 'failed', 'superseded')), CONSTRAINT jobs_lease CHECK ((status = 'processing') = (lease_owner IS NOT NULL AND lease_until IS NOT NULL)), CONSTRAINT jobs_attempts CHECK (attempt >= 0 AND max_attempts >= 1) ); CREATE INDEX jobs_processing_lease_idx ON jobs (lease_until) WHERE status = 'processing'; CREATE INDEX jobs_queued_idx ON jobs (next_attempt_at) WHERE status = 'queued'; CREATE INDEX jobs_failed_idx ON jobs (updated_at) WHERE status = 'failed'; -- Transactional outbox: written in the same transaction as the state change, published by the relay. CREATE TABLE outbox ( id bigserial PRIMARY KEY, job_id uuid NOT NULL REFERENCES jobs (job_id), payload jsonb NOT NULL, -- exactly contracts/jobs.schema.json, no bytes/URLs/credentials available_at timestamptz NOT NULL DEFAULT clock_timestamp(), -- delayed retry (backoff + jitter) created_at timestamptz NOT NULL DEFAULT clock_timestamp(), published_at timestamptz, publish_count integer NOT NULL DEFAULT 0 ); CREATE INDEX outbox_unpublished_idx ON outbox (available_at, id) WHERE published_at IS NULL; -- Effect key (attachment_id, pipeline_version, name): one row per variant, never a second set. CREATE TABLE attachment_variants ( attachment_id uuid NOT NULL REFERENCES attachments (id), pipeline_version integer NOT NULL, 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 bigint NOT NULL CHECK (size_bytes >= 1), width integer NOT NULL CHECK (width >= 1), height integer NOT NULL CHECK (height >= 1), sha256 text NOT NULL CHECK (sha256 ~ '^[a-f0-9]{64}$'), job_id uuid NOT NULL REFERENCES jobs (job_id), attempt integer NOT NULL, created_at timestamptz NOT NULL DEFAULT clock_timestamp(), PRIMARY KEY (attachment_id, pipeline_version, name) ); -- Audit/observability log: claims, retries, duplicates, replays. Never contains secrets or bytes. CREATE TABLE job_events ( id bigserial PRIMARY KEY, job_id uuid, attachment_id uuid, event text NOT NULL, attempt integer, actor text NOT NULL, detail text, created_at timestamptz NOT NULL DEFAULT clock_timestamp() ); CREATE INDEX job_events_attachment_idx ON job_events (attachment_id, id); -- Fault injection for the track-5 tests; consulted only when FAULT_INJECTION=1 in the worker/relay environment. CREATE TABLE fault_injections ( id bigserial PRIMARY KEY, point text NOT NULL, action text NOT NULL, attachment_id uuid, remaining integer NOT NULL DEFAULT 1, created_at timestamptz NOT NULL DEFAULT clock_timestamp() );