ALTER TABLE attachments DROP CONSTRAINT attachments_status_check; ALTER TABLE attachments ALTER COLUMN status SET DEFAULT 'queued'; ALTER TABLE attachments ADD CONSTRAINT attachments_status_check CHECK (status IN ('queued','processing','ready','failed')); ALTER TABLE attachments ADD COLUMN variants JSONB NOT NULL DEFAULT '[]'; ALTER TABLE attachments ADD COLUMN error_code TEXT; CREATE TABLE attachment_jobs ( job_id UUID PRIMARY KEY, attachment_id UUID NOT NULL UNIQUE REFERENCES attachments(id), pipeline_version INTEGER NOT NULL DEFAULT 1, attempts INTEGER NOT NULL DEFAULT 0, next_publish TIMESTAMPTZ NOT NULL DEFAULT now(), created_at TIMESTAMPTZ NOT NULL DEFAULT now() ); CREATE INDEX attachment_jobs_due ON attachment_jobs(next_publish);