migrate_db_phase4.py•2.17 kB
#!/usr/bin/env python3
"""
Database migration script for Phase 4: Usage Context
Adds columns for usage patterns, test coverage, and examples
"""
import sqlite3
import sys
from pathlib import Path
def migrate_database(db_path: Path):
"""Add new columns for usage context tracking"""
if not db_path.exists():
print(f"Database not found at {db_path}")
return False
try:
conn = sqlite3.connect(str(db_path))
cursor = conn.cursor()
# Check if columns already exist
cursor.execute("PRAGMA table_info(symbols)")
columns = [col[1] for col in cursor.fetchall()]
new_columns = [
('usage_frequency', 'INTEGER DEFAULT 0'), # How often the symbol is used
('test_files', 'TEXT'), # JSON array of test files testing this symbol
('example_usage', 'TEXT'), # Example code showing how to use this symbol
('common_patterns', 'TEXT'), # JSON array of common usage patterns
('test_coverage', 'REAL DEFAULT 0.0') # Percentage of code covered by tests
]
for col_name, col_type in new_columns:
if col_name not in columns:
print(f"Adding column: {col_name}")
cursor.execute(f"ALTER TABLE symbols ADD COLUMN {col_name} {col_type}")
else:
print(f"Column {col_name} already exists")
conn.commit()
print("Migration completed successfully")
# Show updated schema
cursor.execute("PRAGMA table_info(symbols)")
print("\nUpdated schema:")
for col in cursor.fetchall():
print(f" {col[1]} ({col[2]})")
conn.close()
return True
except Exception as e:
print(f"Migration failed: {e}")
return False
if __name__ == "__main__":
project_root = Path(__file__).parent.parent
db_path = project_root / ".claude-symbols" / "search.db"
print(f"Migrating database at: {db_path}")
success = migrate_database(db_path)
sys.exit(0 if success else 1)