diff --git a/apps/coder/src/schema.sql b/apps/coder/src/schema.sql index ffa5621..adf7c93 100644 --- a/apps/coder/src/schema.sql +++ b/apps/coder/src/schema.sql @@ -205,6 +205,27 @@ DO $$ BEGIN END IF; END $$; +-- P1.5-b follow-up: converge agent_sessions.session_id FK CASCADE → SET NULL. +-- The re-key block above re-adds session_id_fkey as SET NULL, but it is guarded on +-- chat_id_fkey's ABSENCE — so a DB already re-keyed to (chat_id, agent) while +-- session_id_fkey was still ON DELETE CASCADE never re-enters that block and stays +-- 'c'. This standalone guard flips it to SET NULL ('n'), matching worktree_id. +-- Idempotent (mirrors the session_worktrees defang's confdeltype check): only fires +-- while the FK is still CASCADE — a no-op on a fresh deploy (already 'n' from the +-- re-key block) and on every re-run thereafter. +DO $$ BEGIN + IF EXISTS ( + SELECT 1 FROM pg_constraint + WHERE conname = 'agent_sessions_session_id_fkey' + AND confdeltype = 'c' + ) THEN + ALTER TABLE agent_sessions ALTER COLUMN session_id DROP NOT NULL; + ALTER TABLE agent_sessions DROP CONSTRAINT agent_sessions_session_id_fkey; + ALTER TABLE agent_sessions ADD CONSTRAINT agent_sessions_session_id_fkey + FOREIGN KEY (session_id) REFERENCES sessions(id) ON DELETE SET NULL; + END IF; +END $$; + -- v2.6: attribution for DiffPanel badges (Phase 1 UX reads this). ALTER TABLE pending_changes ADD COLUMN IF NOT EXISTS agent TEXT;