diff --git a/src/cmdforge/registry/db.py b/src/cmdforge/registry/db.py index d0d8c11..374b19a 100644 --- a/src/cmdforge/registry/db.py +++ b/src/cmdforge/registry/db.py @@ -281,6 +281,39 @@ def connect_db(path: Path | None = None) -> sqlite3.Connection: def init_db(conn: sqlite3.Connection) -> None: conn.executescript(SCHEMA_SQL) conn.commit() + # Run migrations for existing databases + migrate_db(conn) + + +def migrate_db(conn: sqlite3.Connection) -> None: + """Add missing columns to existing tables (for schema updates).""" + # Get existing columns in tools table + cursor = conn.execute("PRAGMA table_info(tools)") + existing_cols = {row[1] for row in cursor.fetchall()} + + # Columns that may need to be added (column_name, type, default) + migrations = [ + ("scrutiny_status", "TEXT", "'pending'"), + ("scrutiny_report", "TEXT", "NULL"), + ("source", "TEXT", "NULL"), + ("source_url", "TEXT", "NULL"), + ("source_json", "TEXT", "NULL"), + ] + + for col_name, col_type, default in migrations: + if col_name not in existing_cols: + try: + conn.execute(f"ALTER TABLE tools ADD COLUMN {col_name} {col_type} DEFAULT {default}") + conn.commit() + except sqlite3.OperationalError: + pass # Column might already exist or other issue + + # Ensure indexes exist + try: + conn.execute("CREATE INDEX IF NOT EXISTS idx_tools_owner ON tools(owner)") + conn.commit() + except sqlite3.OperationalError: + pass def query_one(conn: sqlite3.Connection, sql: str, params: Iterable | None = None):