-- Migration 018: 補缺失外鍵索引 + agent_context TTL 清理函數 -- 建立日期:2026-04-22 -- 關聯 ADR: ADR-013 (AIOps AutoHeal)、ADR-017 (agent_context) -- ───────────────────────────────────────────────── -- 補缺失的外鍵索引(heal_logs / incidents) -- ───────────────────────────────────────────────── CREATE INDEX IF NOT EXISTS idx_incidents_playbook_id ON incidents(playbook_id); CREATE INDEX IF NOT EXISTS idx_heal_logs_playbook_id ON heal_logs(playbook_id); -- ───────────────────────────────────────────────── -- agent_context TTL 清理函數(配合 scheduler 定時呼叫) -- ───────────────────────────────────────────────── CREATE OR REPLACE FUNCTION cleanup_expired_agent_context() RETURNS void AS $$ DELETE FROM agent_context WHERE created_at + (ttl_minutes * interval '1 minute') < NOW(); $$ LANGUAGE sql;