-- Migration 008: Task-commit linking
-- Links tasks to git commits via SHA references (Gitea MCP owns full commit data)

CREATE TABLE IF NOT EXISTS task_commits (
    id SERIAL PRIMARY KEY,
    task_id TEXT NOT NULL REFERENCES tasks(id) ON DELETE CASCADE,
    commit_sha TEXT NOT NULL,
    repo TEXT NOT NULL,
    source TEXT DEFAULT 'manual' CHECK (source IN ('manual', 'parsed', 'pr_merge')),
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    UNIQUE(task_id, commit_sha)
);

CREATE INDEX IF NOT EXISTS idx_task_commits_task ON task_commits(task_id);
CREATE INDEX IF NOT EXISTS idx_task_commits_sha ON task_commits(commit_sha);
CREATE INDEX IF NOT EXISTS idx_task_commits_repo ON task_commits(repo);
