-- +goose Up ALTER TABLE attachments ADD COLUMN target_version integer NOT NULL DEFAULT 1 CHECK (target_version >= 1), ADD COLUMN published_version integer CHECK (published_version BETWEEN 1 AND target_version); -- A queued row with no publish timestamp is an outbox entry for this attempt. -- attempt never decreases. The separate budget starts again only on operator replay. CREATE TABLE attachment_jobs ( job_id uuid PRIMARY KEY DEFAULT gen_random_uuid(), attachment_id uuid NOT NULL REFERENCES attachments(id), pipeline_version integer NOT NULL DEFAULT 1 CHECK (pipeline_version >= 1), state text NOT NULL DEFAULT 'queued' CHECK (state IN ('queued','processing','ready','failed','superseded')), source_key text NOT NULL, attempt integer NOT NULL DEFAULT 1 CHECK (attempt >= 1), budget_attempt integer NOT NULL DEFAULT 1 CHECK (budget_attempt BETWEEN 1 AND attempt), available_at timestamptz NOT NULL DEFAULT now(), published_at timestamptz, lease_token uuid, lease_until timestamptz, error_code text CHECK (error_code ~ '^[a-z][a-z0-9_]{0,63}$'), created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now(), UNIQUE (attachment_id, pipeline_version), CHECK ((state = 'processing') = (lease_token IS NOT NULL)), CHECK ((state = 'processing') = (lease_until IS NOT NULL)), CHECK (state <> 'failed' OR error_code IS NOT NULL) ); CREATE INDEX attachment_jobs_outbox_idx ON attachment_jobs (available_at, job_id) WHERE state='queued' AND published_at IS NULL; CREATE INDEX attachment_jobs_recover_idx ON attachment_jobs (lease_until, job_id) WHERE state='processing'; CREATE INDEX attachment_jobs_failed_idx ON attachment_jobs (updated_at, job_id) WHERE state='failed'; CREATE TABLE attachment_variants ( attachment_id uuid NOT NULL, pipeline_version integer NOT NULL, variant text NOT NULL CHECK (variant IN ('thumbnail', 'crop', 'optimized')), object_key text NOT NULL, mime_type text NOT NULL CHECK (mime_type = CASE WHEN variant='optimized' THEN 'image/webp' ELSE 'image/jpeg' END), width integer NOT NULL CHECK (width > 0), height integer NOT NULL CHECK (height > 0), size_bytes bigint NOT NULL CHECK (size_bytes > 0), sha256 bytea NOT NULL CHECK (octet_length(sha256)=32), PRIMARY KEY (attachment_id,pipeline_version,variant), FOREIGN KEY (attachment_id,pipeline_version) REFERENCES attachment_jobs(attachment_id,pipeline_version) ); CREATE TABLE attachment_replays ( job_id uuid NOT NULL REFERENCES attachment_jobs(job_id), attempt integer NOT NULL, actor text NOT NULL CHECK (length(actor) BETWEEN 1 AND 128), previous_error text NOT NULL, created_at timestamptz NOT NULL DEFAULT now(), PRIMARY KEY (job_id, attempt) ); -- Published Lab 4 objects are valid inputs. A recorded missing original -- stays diagnosable even before a worker is started. INSERT INTO attachment_jobs(attachment_id,source_key,state,error_code) SELECT a.id,'blobs/'||encode(f.hash,'hex'), CASE WHEN f.status='failed' THEN 'failed' ELSE 'queued' END, CASE WHEN f.status='failed' THEN 'source_missing' END FROM attachments a JOIN files f ON f.hash=a.file_hash; CREATE VIEW attachment_states AS SELECT a.id, CASE WHEN a.published_version IS NULL THEN j.state WHEN f.status='failed' THEN 'failed' ELSE 'ready' END AS status, CASE WHEN a.published_version IS NOT NULL AND f.status='failed' THEN 'source_missing' WHEN a.published_version IS NULL AND j.state='failed' THEN j.error_code END AS error_code, j.updated_at FROM attachments a JOIN files f ON f.hash=a.file_hash JOIN attachment_jobs j ON j.attachment_id=a.id AND j.pipeline_version=a.target_version; -- +goose Down DROP VIEW attachment_states; DROP TABLE attachment_replays; DROP TABLE attachment_variants; DROP TABLE attachment_jobs; ALTER TABLE attachments DROP COLUMN published_version, DROP COLUMN target_version;