H-60: harden Captain migration rollback and concurrency (#10)

Co-authored-by: Rogee <rogee@ipao.vip>
This commit is contained in:
Rogee
2026-08-13 17:13:17 +08:00
committed by GitHub
co-authored by rogee
parent 0540ae3791
commit fcfe5bd37c
4 changed files with 153 additions and 9 deletions
@@ -1,4 +1,7 @@
DROP INDEX IF EXISTS idx_captain_inboxes_active_inbox;
DROP INDEX IF EXISTS idx_agent_bot_inboxes_bot_inbox;
DROP INDEX IF EXISTS idx_agent_bots_captain_assistant;
ALTER TABLE agent_bots DROP COLUMN IF EXISTS captain_assistant_id;
-- The up migration merges duplicate bots/bindings and rewrites references.
-- Those deleted rows and original references cannot be reconstructed safely.
DO $$
BEGIN
RAISE EXCEPTION 'migration 000079 is irreversible: restore from backup instead of migrating down';
END
$$;
@@ -1,5 +1,12 @@
ALTER TABLE agent_bots ADD COLUMN IF NOT EXISTS captain_assistant_id BIGINT;
-- This data repair and its unique indexes must see a stable snapshot. These
-- locks block concurrent writes while allowing reads, so run this migration
-- in a maintenance window sized for the deduplication.
LOCK TABLE agent_bots, agent_bot_inboxes, captain_inboxes, conversations,
agent_bot_presence_events, bot_rules, bot_trigger_configs
IN SHARE ROW EXCLUSIVE MODE;
UPDATE agent_bots
SET captain_assistant_id = (config ->> 'assistant_id')::BIGINT
WHERE bot_type = 'captain'