-- Returning to the stricter 0004 shape requires clearing retry reasons first. UPDATE attachment_jobs SET error_code = NULL WHERE status IN ('queued', 'processing'); UPDATE attachments SET error_code = NULL WHERE status IN ('queued', 'processing'); ALTER TABLE attachments DROP CONSTRAINT IF EXISTS attachments_error_state; ALTER TABLE attachments ADD CONSTRAINT attachments_error_state CHECK ( (status IN ('queued', 'processing', 'ready') AND error_code IS NULL) OR (status = 'failed' AND error_code IS NOT NULL) ); ALTER TABLE attachment_jobs DROP CONSTRAINT IF EXISTS attachment_jobs_error_state; ALTER TABLE attachment_jobs ADD CONSTRAINT attachment_jobs_error_state CHECK ( (status IN ('queued', 'processing', 'ready') AND error_code IS NULL) OR (status = 'failed' AND error_code IS NOT NULL) );