-- Migration 000: Migration History Tracking
-- Description: Creates table to track which migrations have been applied
-- Date: 2026-01-25
-- Author: Claude Code

-- ============================================================
-- Table: migration_history
-- Purpose: Track applied database migrations
-- ============================================================

CREATE TABLE IF NOT EXISTS migration_history (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  migration_name TEXT NOT NULL UNIQUE,      -- e.g., "002_add_composite_indexes"
  applied_at TEXT NOT NULL DEFAULT (datetime('now')),
  execution_time_ms INTEGER,                -- Time taken to execute
  success INTEGER NOT NULL DEFAULT 1,       -- 0=failed, 1=succeeded
  error_message TEXT,                       -- Error details if failed
  checksum TEXT                             -- Future: file content hash for validation
);

-- Index for quick lookup of applied migrations
CREATE INDEX IF NOT EXISTS idx_migration_history_name
  ON migration_history(migration_name);

-- Index for status queries
CREATE INDEX IF NOT EXISTS idx_migration_history_success
  ON migration_history(success);

-- ============================================================
-- Notes
-- ============================================================
-- This table is created first (000) to track all subsequent migrations.
-- Safe to re-run: uses IF NOT EXISTS for idempotency.
-- Migrations are tracked by filename without .sql extension.
