From 1de4b2d688ea04e8657c15ff6c8b1a631722c034 Mon Sep 17 00:00:00 2001 From: Bohan Jiang <52446949+Bohan-J@users.noreply.github.com> Date: Mon, 13 Jul 2026 15:33:52 +0800 Subject: [PATCH] fix(migrations): make 164_attachment_task_id idempotent to self-heal #5307 renumber drift (#5324) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #5307 renamed 161_attachment_task_id -> 164 (and 162_..._index -> 165) to resolve a prefix collision. schema_migrations keys on the full stem, so any DB that applied the migration under its old 161 number does not have "164" in the ledger and the runner re-applies the renamed file. The bare `ADD COLUMN task_id` then aborts with 42701 ("column already exists"), blocking every later migration — this is exactly what crashed the dev deploy of main (bf288349f) at container startup, before it ever reached 166_project_dates. Add `IF NOT EXISTS` so the re-run is a harmless no-op on already-migrated DBs (dev/staging/prod) with no manual schema_migrations surgery, while staying identical on a fresh DB. Sibling 165 already uses CREATE INDEX ... IF NOT EXISTS for the same reason; this brings 164 in line. The down file already uses DROP COLUMN IF EXISTS. Verified on a throwaway DB reproducing the dev drift (forget 164/165 in the ledger, keep the column+index): old file reproduces the 42701 crash, the fixed file self-heals and completes. Migration lint + concurrent migrate tests pass. Refs #5307 Co-authored-by: J Co-authored-by: multica-agent --- server/migrations/164_attachment_task_id.up.sql | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/server/migrations/164_attachment_task_id.up.sql b/server/migrations/164_attachment_task_id.up.sql index d67b73cd0..6b6b244b8 100644 --- a/server/migrations/164_attachment_task_id.up.sql +++ b/server/migrations/164_attachment_task_id.up.sql @@ -16,8 +16,14 @@ -- ON DELETE action here would never fire in practice; adding an FK on the -- hot attachment table would only add write overhead and a cascade -- dependency the app does not rely on. +-- IF NOT EXISTS makes this self-heal on databases that applied it under its +-- pre-#5307 number (161). The renumber to 164 changed the schema_migrations +-- key, so the runner re-applies the renamed file on those DBs; without the +-- guard the bare ADD COLUMN aborts with 42701 ("column already exists") and +-- blocks every later migration (GH #5307's note; the dev deploy that hit it). +-- Sibling 165 already uses CREATE INDEX ... IF NOT EXISTS for the same reason. ALTER TABLE attachment - ADD COLUMN task_id UUID; + ADD COLUMN IF NOT EXISTS task_id UUID; -- The task_id lookup index is built CONCURRENTLY in the next migration. -- CREATE INDEX CONCURRENTLY cannot share a transaction or multi-command