-- Apply after 026, independently to every project business database. -- Preserve job identity, deduplication and audit metadata, including legacy results. ALTER TABLE workbench.calculation_jobs ADD COLUMN IF NOT EXISTS started_at timestamptz, ADD COLUMN IF NOT EXISTS finished_at timestamptz, ADD COLUMN IF NOT EXISTS queue_expires_at timestamptz, ADD COLUMN IF NOT EXISTS result_expires_at timestamptz; UPDATE workbench.calculation_jobs SET queue_expires_at=created_at+interval '1 hour' WHERE queue_expires_at IS NULL; UPDATE workbench.calculation_jobs SET finished_at=updated_at WHERE finished_at IS NULL AND status IN ('succeeded','failed','cancelled','timed_out','interrupted'); -- Give pre-deployment results a complete retention window, even on reruns. UPDATE workbench.calculation_jobs SET result_expires_at=clock_timestamp()+interval '7 days' WHERE result IS NOT NULL AND result_expires_at IS NULL; ALTER TABLE workbench.calculation_jobs ALTER COLUMN queue_expires_at SET DEFAULT (clock_timestamp()+interval '1 hour'), ALTER COLUMN queue_expires_at SET NOT NULL; CREATE INDEX IF NOT EXISTS calculation_jobs_queue_expiry ON workbench.calculation_jobs(project_id,queue_expires_at) WHERE status='queued'; CREATE INDEX IF NOT EXISTS calculation_jobs_result_expiry ON workbench.calculation_jobs(project_id,result_expires_at) WHERE result IS NOT NULL;