-- 211_md_columns.sql
-- Phase 1A: documents 테이블에 markdown canonical layer 컬럼 13개 추가.
-- Plan: ~/.claude/plans/plan-idempotent-sundae.md (round 3)
--
-- asyncpg exec_driver_sql 단일 prepared statement 제약 — ALTER TABLE 다중 ADD COLUMN
-- 절은 단일 statement 라 허용. 인덱스/CHECK/CREATE TABLE 은 별도 파일 (212~219).
-- PG 16.13: ADD COLUMN ... DEFAULT <constant> 는 fast path (table rewrite 없음).

ALTER TABLE documents
    ADD COLUMN IF NOT EXISTS md_content                   TEXT,
    ADD COLUMN IF NOT EXISTS md_frontmatter               JSONB        NOT NULL DEFAULT '{}'::jsonb,
    ADD COLUMN IF NOT EXISTS md_format_version            TEXT         NOT NULL DEFAULT '1.0',
    ADD COLUMN IF NOT EXISTS md_status                    TEXT         NOT NULL DEFAULT 'pending'
        CHECK (md_status IN ('pending','processing','success','partial','failed','skipped')),
    ADD COLUMN IF NOT EXISTS md_extraction_engine         TEXT,
    ADD COLUMN IF NOT EXISTS md_extraction_engine_version TEXT,
    ADD COLUMN IF NOT EXISTS md_extraction_quality        JSONB,
    ADD COLUMN IF NOT EXISTS md_extraction_error          TEXT,
    ADD COLUMN IF NOT EXISTS md_content_hash              TEXT,
    ADD COLUMN IF NOT EXISTS md_source_hash               TEXT,
    ADD COLUMN IF NOT EXISTS md_generated_at              TIMESTAMPTZ,
    ADD COLUMN IF NOT EXISTS content_origin               TEXT         NOT NULL DEFAULT 'extracted'
        CHECK (content_origin IN ('extracted','manual','ai_drafted','imported')),
    ADD COLUMN IF NOT EXISTS md_draft_status              TEXT
        CHECK (md_draft_status IS NULL OR md_draft_status IN ('draft','pending_review','approved','revised','rejected'));
