-- Migration 015: Add commit SHAs to merge_requests, closes_issues watermark,
-- and missing label_name index on resource_label_events.

-- Commit SHAs link MRs to actual git history (needed for Gate 4: file-history, Gate 5: trace)
ALTER TABLE merge_requests ADD COLUMN merge_commit_sha TEXT;
ALTER TABLE merge_requests ADD COLUMN squash_commit_sha TEXT;

-- Watermark for closes_issues sync (same pattern as resource_events_synced_for_updated_at)
-- Prevents re-fetching closes_issues for MRs that haven't changed since last sync
ALTER TABLE merge_requests ADD COLUMN closes_issues_synced_for_updated_at INTEGER;

-- Missing index from original spec: enables efficient label-name filtering in timeline queries
CREATE INDEX IF NOT EXISTS idx_label_events_label ON resource_label_events(label_name);

-- Update schema version
INSERT INTO schema_version (version, applied_at, description)
VALUES (15, strftime('%s', 'now') * 1000, 'Add commit SHAs, closes_issues watermark, and label event index');
