migrate_db_phase3.py•2.11 kB
#!/usr/bin/env python3
"""
Database migration script for Phase 3: Dependency Graph
Adds columns for imports, calls, inheritance, and relationships
"""
import sqlite3
import sys
from pathlib import Path
def migrate_database(db_path: Path):
"""Add new columns for dependency graph information"""
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 = [
('imports', 'TEXT'), # JSON array of imported modules/symbols
('calls', 'TEXT'), # JSON array of functions this symbol calls
('called_by', 'TEXT'), # JSON array of functions that call this symbol
('inherits_from', 'TEXT'), # Parent class(es) for inheritance
('inherited_by', 'TEXT') # JSON array of child classes
]
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)