fix(db): split migration 153 for asyncpg single-statement limit

asyncpg exec_driver_sql 이 prepared statement 로 multiple commands 를
허용하지 않아 배포 시 PostgresSyntaxError: cannot insert multiple commands
into a prepared statement 로 init_db() 실패.

153 를 단일 ALTER TABLE (10 ADD COLUMN) 로 축소하고 2 partial index 를
154/155 로 분리:

- 153_analyze_events_shadow.sql: ALTER TABLE ADD COLUMN (단일 statement)
- 154_analyze_events_shadow_idx_ts.sql: idx_analyze_events_shadow_ts
- 155_analyze_events_policy_violation_idx.sql: idx_analyze_events_policy_violation

배포 test: GPU fastapi 컨테이너 재빌드 후 init_db 가 153/154/155 세 파일을
순차 적용 (asyncpg prepared statement 1 파일 1 문).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Hyungi Ahn
2026-04-24 09:38:40 +09:00
parent ba97766d45
commit c9e8dd0ba1
3 changed files with 18 additions and 10 deletions
+4 -10
View File
@@ -1,12 +1,14 @@
-- 153_analyze_events_shadow.sql
-- AI Policy Layer (PR-A) — shadow 관측용 컬럼 추가. 아무도 쓰지 않음.
-- AI Policy Layer (PR-A) — shadow 관측용 컬럼 추가. 아무도 쓰지 않음.
-- PR-B 가 DBShadowLogger 구현 후 writer 추가 예정.
--
-- plan: ~/.claude/plans/wise-gliding-hippo.md
-- axis 원칙: feedback_category_vs_ai_domain_axis.md (subject_domain 은 정책 축,
-- documents.category 매칭 키로 쓰지 않음)
--
-- 주의: _run_migrations 는 단일 outer 트랜잭션으로 실행 BEGIN/COMMIT 금지.
-- 주의: _run_migrations 는 asyncpg exec_driver_sql 로 단일 prepared statement 실행 —
-- 한 파일에 단일 SQL statement 만 허용 (multi-statement 시 PostgresSyntaxError).
-- index 는 migrations 154/155 로 분리.
ALTER TABLE analyze_events
ADD COLUMN IF NOT EXISTS subject_domain TEXT,
@@ -19,11 +21,3 @@ ALTER TABLE analyze_events
ADD COLUMN IF NOT EXISTS policy_violation_ids TEXT[],
ADD COLUMN IF NOT EXISTS shadow_would_route_to TEXT,
ADD COLUMN IF NOT EXISTS policy_version TEXT;
CREATE INDEX IF NOT EXISTS idx_analyze_events_shadow_ts
ON analyze_events (created_at)
WHERE shadow_would_route_to IS NOT NULL;
CREATE INDEX IF NOT EXISTS idx_analyze_events_policy_violation
ON analyze_events (created_at)
WHERE policy_violation = true;
@@ -0,0 +1,7 @@
-- 154_analyze_events_shadow_idx_ts.sql
-- PR-A: shadow_would_route_to 기반 조회 인덱스 (migration 153 분할).
-- shadow 기간 대시보드/분석 쿼리용 partial index.
CREATE INDEX IF NOT EXISTS idx_analyze_events_shadow_ts
ON analyze_events (created_at)
WHERE shadow_would_route_to IS NOT NULL;
@@ -0,0 +1,7 @@
-- 155_analyze_events_policy_violation_idx.sql
-- PR-A: policy_violation=true 행 추출 인덱스 (migration 153 분할).
-- under_escalation 재처리 큐 + 야간 sweep 감사용 partial index.
CREATE INDEX IF NOT EXISTS idx_analyze_events_policy_violation
ON analyze_events (created_at)
WHERE policy_violation = true;