Files
awoooi/apps/api/migrations/adr071_notification_lifecycle.sql
OG T 325b3851b5
Some checks failed
CD Pipeline / build-and-deploy (push) Has been cancelled
Type Sync Check / check-type-sync (push) Failing after 1m7s
feat(adr-071): 告警通知四類型第一批 B/C/E/F/G/H 全實作
ADR-071-B: classify_notification() — 五型分類器 (TYPE-1/2/3/4/4D)
ADR-071-C: send_info_notification() — TYPE-1 純資訊無按鈕卡片
ADR-071-E: _build_inline_keyboard() — 依 alert_category 動態組合 TYPE-3 按鈕
ADR-071-F: send_drift_card() — TYPE-4D Config Drift 卡片 + Diff 截斷
ADR-071-G: km_conversion_service.py — Incident RESOLVED 自動轉 KM
ADR-071-H: handle_manual_fix_done() — TYPE-4 手動修復 Bot 對話閉環

前批已完成: ADR-071-A (DB Migration) + ADR-071-D (狀態機守衛)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-11 02:24:20 +08:00

96 lines
3.5 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- ADR-071-A: 告警通知四類型 + 全生命週期 DB 記錄
-- 建立時間: 2026-04-11 (台北時區)
-- 建立者: Claude Sonnet 4.6 — ADR-071 第一批
--
-- 設計說明:
-- 在現有表上補充欄位,不新建表
-- PgEnum ADD VALUE 必須在獨立 transaction 執行(不能在同一 tx 內使用新值)
--
-- 執行順序:
-- Step 1: PgEnum 新增值(獨立 transaction
-- Step 2: incidents 表新增 7 個欄位
-- Step 3: 驗收查詢
-- ============================================================================
-- Step 1: alert_event_type PgEnum 新增 5 個值
-- 注意: ADD VALUE IF NOT EXISTS 是 idempotent重複執行安全
-- 注意: 每個 ADD VALUE 必須在獨立 transaction不能批次
-- ============================================================================
-- 分類通知事件
ALTER TYPE alert_event_type ADD VALUE IF NOT EXISTS 'NOTIFICATION_CLASSIFIED';
-- 手動修復記錄
ALTER TYPE alert_event_type ADD VALUE IF NOT EXISTS 'MANUAL_FIX_RECORDED';
-- KM 轉換完成
ALTER TYPE alert_event_type ADD VALUE IF NOT EXISTS 'KM_CONVERTED';
-- Playbook 草稿建立
ALTER TYPE alert_event_type ADD VALUE IF NOT EXISTS 'PLAYBOOK_DRAFT_CREATED';
-- 狀態機守衛攔截
ALTER TYPE alert_event_type ADD VALUE IF NOT EXISTS 'STATE_GUARD_BLOCKED';
-- ============================================================================
-- Step 2: incidents 表新增 7 個欄位
-- 注意: ADD COLUMN IF NOT EXISTS 是 idempotent重複執行安全
-- ============================================================================
-- 通知類型記錄 (TYPE-1/2/3/4/4D)
ALTER TABLE incidents
ADD COLUMN IF NOT EXISTS notification_type VARCHAR(10);
-- 告警類別(決定 TYPE-3 按鈕組合)
ALTER TABLE incidents
ADD COLUMN IF NOT EXISTS alert_category VARCHAR(50);
-- MCP 情報收集快照執行前Sprint A 完成後由 MCP Phase 2 填充)
ALTER TABLE incidents
ADD COLUMN IF NOT EXISTS context_bundle JSONB;
-- 指標快照執行前Prometheus MCP 採集)— ADR-071-I 使用
ALTER TABLE incidents
ADD COLUMN IF NOT EXISTS metrics_before JSONB;
-- 指標快照執行後Prometheus MCP 採集)— ADR-071-I 使用
ALTER TABLE incidents
ADD COLUMN IF NOT EXISTS metrics_after JSONB;
-- 執行驗證結果K8s MCP watch_rollout 結果)— ADR-071-J 使用
ALTER TABLE incidents
ADD COLUMN IF NOT EXISTS verification_result JSONB;
-- 手動修復步驟TYPE-4 使用者輸入)
ALTER TABLE incidents
ADD COLUMN IF NOT EXISTS manual_fix_steps TEXT;
ALTER TABLE incidents
ADD COLUMN IF NOT EXISTS manual_fix_by VARCHAR(100);
-- ============================================================================
-- Step 3: 驗收查詢(執行後確認欄位存在)
-- ============================================================================
-- 確認 incidents 新欄位
SELECT column_name, data_type
FROM information_schema.columns
WHERE table_name = 'incidents'
AND column_name IN (
'notification_type', 'alert_category', 'context_bundle',
'metrics_before', 'metrics_after', 'verification_result',
'manual_fix_steps', 'manual_fix_by'
)
ORDER BY column_name;
-- 確認 alert_event_type 新值
SELECT enumlabel
FROM pg_enum
JOIN pg_type ON pg_enum.enumtypid = pg_type.oid
WHERE pg_type.typname = 'alert_event_type'
AND enumlabel IN (
'NOTIFICATION_CLASSIFIED', 'MANUAL_FIX_RECORDED',
'KM_CONVERTED', 'PLAYBOOK_DRAFT_CREATED', 'STATE_GUARD_BLOCKED'
)
ORDER BY enumlabel;