alter table attachments add column status text not null default 'ready', add column error_code text, add column staging_key text, add column updated_at timestamptz; update attachments set updated_at = created_at; alter table attachments alter column status drop default, alter column updated_at set not null, alter column object_key drop not null; create table attachment_variants ( attachment_id uuid not null references attachments(id), pipeline_version int not null, name text not null, object_key text not null unique, content_type text not null, size_bytes bigint not null, width int not null, height int not null, sha256 text not null, primary key (attachment_id, pipeline_version, name) ); create table jobs ( id uuid primary key, attachment_id uuid not null unique references attachments(id), pipeline_version int not null, status text not null, attempt int not null, max_attempts int not null, next_attempt_at timestamptz not null, lease_owner text, lease_until timestamptz, last_error text, created_at timestamptz not null, updated_at timestamptz not null ); create index jobs_lease_idx on jobs (lease_until) where status = 'processing'; create table outbox ( id bigserial primary key, job_id uuid not null references jobs(id), attempt int not null, available_at timestamptz not null, published_at timestamptz, created_at timestamptz not null ); create index outbox_pending_idx on outbox (available_at) where published_at is null; create table job_replays ( id bigserial primary key, job_id uuid not null references jobs(id), previous_error text, replayed_at timestamptz not null );