diff --git a/api/database.py b/api/database.py index 86aa34c..d1fe94c 100644 --- a/api/database.py +++ b/api/database.py @@ -203,6 +203,9 @@ def initialize_database(db_path: str = "data/jobs.db") -> None: ) """) + # Run schema migrations for existing databases + _migrate_schema(cursor) + # Create indexes for performance _create_indexes(cursor) @@ -210,6 +213,21 @@ def initialize_database(db_path: str = "data/jobs.db") -> None: conn.close() +def _migrate_schema(cursor: sqlite3.Cursor) -> None: + """Migrate existing database schema to latest version.""" + # Check if positions table exists and has simulation_run_id column + cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='positions'") + if cursor.fetchone(): + cursor.execute("PRAGMA table_info(positions)") + columns = [row[1] for row in cursor.fetchall()] + + if 'simulation_run_id' not in columns: + # Add simulation_run_id column to existing positions table + cursor.execute(""" + ALTER TABLE positions ADD COLUMN simulation_run_id TEXT + """) + + def _create_indexes(cursor: sqlite3.Cursor) -> None: """Create database indexes for query performance."""