-- ADR-104 T4: Playbook versioning / lineage schema
-- 2026-04-30 Codex: LLM-generated Playbooks must preserve lineage instead of
-- overwriting prior operational knowledge.

ALTER TABLE playbooks
    ADD COLUMN IF NOT EXISTS version INTEGER NOT NULL DEFAULT 1,
    ADD COLUMN IF NOT EXISTS parent_playbook_id VARCHAR(36),
    ADD COLUMN IF NOT EXISTS supersedes_playbook_id VARCHAR(36),
    ADD COLUMN IF NOT EXISTS version_reason TEXT;

UPDATE playbooks
SET parent_playbook_id = playbook_id
WHERE parent_playbook_id IS NULL;

CREATE INDEX IF NOT EXISTS ix_playbook_lineage
    ON playbooks(parent_playbook_id, version);

CREATE INDEX IF NOT EXISTS ix_playbook_supersedes
    ON playbooks(supersedes_playbook_id)
    WHERE supersedes_playbook_id IS NOT NULL;
