ALTER TABLE attachments ADD COLUMN target_pipeline_version integer NOT NULL DEFAULT 1 CHECK (target_pipeline_version >= 1), ADD COLUMN published_pipeline_version integer CHECK (published_pipeline_version >= 1), ADD CHECK (published_pipeline_version <= target_pipeline_version); UPDATE attachments SET published_pipeline_version = 1 WHERE state = 'ready'; ALTER TABLE attachment_jobs DROP CONSTRAINT attachment_jobs_attachment_id_key, DROP CONSTRAINT attachment_jobs_staging_key_key, DROP CONSTRAINT attachment_jobs_state_check, ADD COLUMN pipeline_version integer NOT NULL DEFAULT 1 CHECK (pipeline_version >= 1), ADD COLUMN settled_at timestamptz, ADD CHECK (state IN ('queued', 'processing', 'ready', 'failed', 'superseded')), ADD UNIQUE (attachment_id, pipeline_version); CREATE INDEX attachment_jobs_staging ON attachment_jobs(staging_key); ALTER TABLE attachment_variants ADD COLUMN pipeline_version integer NOT NULL DEFAULT 1; ALTER TABLE attachment_variants DROP CONSTRAINT attachment_variants_pkey; ALTER TABLE attachment_variants ADD PRIMARY KEY (attachment_id, pipeline_version, name);