Add the jobs, outbox and variants schema as an additive migration

Migration 0004 widens the attachment states to queued, processing and
failed, adds the staging and original keys, the published pipeline
version and the error code, and moves the lab 4 object key into them
while leaving the old column nullable until a contract step. New tables
hold the variants under the effect key (attachment, pipeline version,
name), the jobs with attempt, claims and lease, the outbox rows the
relay publishes and the audit of job transitions. Partial indexes serve
the relay scan of unpublished rows, the search for expired leases and
the staging cleanup.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Artyom
2026-10-29 00:43:34 +03:00
co-authored by Claude Opus 5.5
parent d9bcc7324b
commit 64fdaa03f2
3 changed files with 160 additions and 4 deletions
+70
View File
@@ -0,0 +1,70 @@
ALTER TABLE attachments
DROP CONSTRAINT attachments_status_check,
ADD CONSTRAINT attachments_status_check
CHECK (status IN ('pending', 'queued', 'processing', 'ready', 'failed')),
ADD COLUMN staging_key text COLLATE "C" CONSTRAINT attachments_staging_key_key UNIQUE,
ADD COLUMN original_key text COLLATE "C" CONSTRAINT attachments_original_key_key UNIQUE,
ADD COLUMN published_version integer NOT NULL DEFAULT 0 CHECK (published_version >= 0),
ADD COLUMN error_code text
CHECK (error_code IN ('invalid_image', 'processing_failed', 'retry_exhausted')),
ALTER COLUMN object_key DROP NOT NULL;
UPDATE attachments SET original_key = object_key, published_version = 1 WHERE status = 'ready';
UPDATE attachments SET staging_key = object_key WHERE status = 'pending';
CREATE INDEX attachments_staged_idx ON attachments (updated_at) WHERE staging_key IS NOT 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 COLLATE "C" NOT NULL CONSTRAINT attachment_variants_object_key_key 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 ~ '^[0-9a-f]{64}$'),
created_at timestamptz NOT NULL,
PRIMARY KEY (attachment_id, pipeline_version, name)
);
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 CHECK (status IN ('queued', 'processing', 'done', 'failed', 'superseded')),
attempt integer NOT NULL CHECK (attempt >= 1),
claims integer NOT NULL DEFAULT 0 CHECK (claims >= 0),
lease_token uuid,
lease_until timestamptz,
next_attempt_at timestamptz,
last_error text,
created_at timestamptz NOT NULL,
updated_at timestamptz NOT NULL,
CONSTRAINT jobs_effect_key UNIQUE (attachment_id, pipeline_version)
);
CREATE INDEX jobs_lease_idx ON jobs (lease_until) WHERE status = 'processing';
CREATE TABLE outbox (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
job_id uuid NOT NULL REFERENCES jobs (job_id),
payload jsonb NOT NULL,
available_at timestamptz NOT NULL,
published_at timestamptz
);
CREATE INDEX outbox_pending_idx ON outbox (available_at, id) WHERE published_at IS NULL;
CREATE INDEX outbox_job_idx ON outbox (job_id);
CREATE TABLE job_events (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
job_id uuid NOT NULL,
attachment_id uuid NOT NULL,
kind text NOT NULL,
attempt integer,
detail jsonb NOT NULL DEFAULT '{}',
at timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX job_events_attachment_idx ON job_events (attachment_id, id);