ALTER TABLE attachments DROP CONSTRAINT attachments_status, DROP CONSTRAINT attachments_ready_at; ALTER TABLE attachments ADD COLUMN pipeline_version INTEGER NOT NULL DEFAULT 1, ADD COLUMN error_code TEXT, ADD COLUMN staging_key TEXT; UPDATE attachments SET staging_key = object_key WHERE status = 'pending'; ALTER TABLE attachments ADD CONSTRAINT attachments_status CHECK ( status IN ('pending', 'queued', 'processing', 'ready', 'failed', 'deleting') ), ADD CONSTRAINT attachments_pipeline_version CHECK (pipeline_version >= 1), ADD CONSTRAINT attachments_error_code CHECK ( (status = 'failed' AND error_code IS NOT NULL AND char_length(error_code) BETWEEN 1 AND 64) OR (status <> 'failed' AND error_code IS NULL) ), ADD CONSTRAINT attachments_ready_at CHECK ( (status = 'ready' AND ready_at IS NOT NULL) OR (status <> 'ready') ), ADD CONSTRAINT attachments_staging_key_length CHECK ( staging_key IS NULL OR char_length(staging_key) BETWEEN 1 AND 1024 ); CREATE INDEX attachments_status_updated_at_idx ON attachments (status, updated_at, id); CREATE TABLE attachment_jobs ( id UUID PRIMARY KEY, attachment_id UUID NOT NULL REFERENCES attachments (id) ON DELETE CASCADE, pipeline_version INTEGER NOT NULL, attempt INTEGER NOT NULL DEFAULT 1, status TEXT NOT NULL DEFAULT 'queued', available_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(), lease_token UUID, lease_owner TEXT, lease_until TIMESTAMPTZ, error_code TEXT, created_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(), updated_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(), CONSTRAINT attachment_jobs_effect_unique UNIQUE (attachment_id, pipeline_version), CONSTRAINT attachment_jobs_pipeline_version CHECK (pipeline_version >= 1), CONSTRAINT attachment_jobs_attempt CHECK (attempt >= 1), CONSTRAINT attachment_jobs_status CHECK (status IN ('queued', 'processing', 'ready', 'failed')), CONSTRAINT attachment_jobs_lease CHECK ( (status = 'processing' AND lease_token IS NOT NULL AND lease_owner IS NOT NULL AND lease_until IS NOT NULL) OR (status <> 'processing' AND lease_token IS NULL AND lease_owner IS NULL AND lease_until IS NULL) ), CONSTRAINT attachment_jobs_error_code CHECK ( error_code IS NULL OR char_length(error_code) BETWEEN 1 AND 64 ) ); CREATE INDEX attachment_jobs_due_idx ON attachment_jobs (available_at, id) WHERE status = 'queued'; CREATE INDEX attachment_jobs_expired_lease_idx ON attachment_jobs (lease_until, id) WHERE status = 'processing'; CREATE TABLE attachment_variants ( attachment_id UUID NOT NULL REFERENCES attachments (id) ON DELETE CASCADE, pipeline_version INTEGER NOT NULL, name TEXT NOT NULL, content_type TEXT NOT NULL, size_bytes BIGINT NOT NULL, sha256 TEXT NOT NULL, width INTEGER NOT NULL, height INTEGER NOT NULL, object_key TEXT NOT NULL, created_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(), CONSTRAINT attachment_variants_pk PRIMARY KEY (attachment_id, pipeline_version, name), CONSTRAINT attachment_variants_object_key_unique UNIQUE (object_key), CONSTRAINT attachment_variants_pipeline_version CHECK (pipeline_version >= 1), CONSTRAINT attachment_variants_name CHECK (name IN ('original', 'thumbnail', 'crop', 'optimized')), CONSTRAINT attachment_variants_content_type CHECK (content_type IN ('image/png', 'image/jpeg', 'image/webp')), CONSTRAINT attachment_variants_size CHECK (size_bytes >= 1), CONSTRAINT attachment_variants_sha256 CHECK (sha256 ~ '^[a-f0-9]{64}$'), CONSTRAINT attachment_variants_dimensions CHECK (width >= 1 AND height >= 1), CONSTRAINT attachment_variants_object_key_length CHECK (char_length(object_key) BETWEEN 1 AND 1024) ); CREATE TABLE outbox_events ( id UUID PRIMARY KEY, topic TEXT NOT NULL, partition_key TEXT NOT NULL, payload JSONB NOT NULL, job_id UUID NOT NULL REFERENCES attachment_jobs (id) ON DELETE CASCADE, attempt INTEGER NOT NULL, available_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(), publish_tries INTEGER NOT NULL DEFAULT 0, claim_token UUID, claim_until TIMESTAMPTZ, last_error_code TEXT, published_at TIMESTAMPTZ, created_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(), CONSTRAINT outbox_events_job_attempt_unique UNIQUE (job_id, attempt), CONSTRAINT outbox_events_attempt CHECK (attempt >= 1), CONSTRAINT outbox_events_publish_tries CHECK (publish_tries >= 0), CONSTRAINT outbox_events_claim CHECK ( (claim_token IS NULL AND claim_until IS NULL) OR (claim_token IS NOT NULL AND claim_until IS NOT NULL) ), CONSTRAINT outbox_events_last_error_code CHECK ( last_error_code IS NULL OR char_length(last_error_code) BETWEEN 1 AND 64 ) ); CREATE INDEX outbox_events_due_idx ON outbox_events (available_at, id) WHERE published_at IS NULL; CREATE TABLE attachment_replay_audit ( id UUID PRIMARY KEY, job_id UUID NOT NULL REFERENCES attachment_jobs (id) ON DELETE CASCADE, attachment_id UUID NOT NULL REFERENCES attachments (id) ON DELETE CASCADE, previous_attempt INTEGER NOT NULL, new_attempt INTEGER NOT NULL, actor TEXT NOT NULL, reason TEXT NOT NULL, created_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(), CONSTRAINT attachment_replay_attempts CHECK (previous_attempt >= 1 AND new_attempt = previous_attempt + 1), CONSTRAINT attachment_replay_actor_length CHECK (char_length(actor) BETWEEN 1 AND 128), CONSTRAINT attachment_replay_reason_length CHECK (char_length(reason) BETWEEN 1 AND 512) ); CREATE INDEX attachment_replay_audit_job_created_at_idx ON attachment_replay_audit (job_id, created_at);