diff --git a/src/blackboard/db.py b/src/blackboard/db.py index a59ac73..ee0a27f 100644 --- a/src/blackboard/db.py +++ b/src/blackboard/db.py @@ -21,6 +21,31 @@ def init_db(db_path: Path) -> None: conn.close() +def _migrate_v261(conn: sqlite3.Connection) -> None: + """v2.6.1 数据迁移:为旧 DB 添加新字段/新表(安全 ALTER)""" + # tasks 表新增字段 + _safe_add_column(conn, "tasks", "current_agent", "TEXT") + _safe_add_column(conn, "tasks", "previous_agent", "TEXT") + _safe_add_column(conn, "tasks", "next_capability", "TEXT") + # routing_decisions 表(如果不存在,CREATE TABLE IF NOT EXISTS 已处理) + # idx_tasks_current_agent 索引 + try: + conn.execute( + "CREATE INDEX IF NOT EXISTS idx_tasks_current_agent ON tasks(current_agent)") + except sqlite3.OperationalError: + pass + + +def _safe_add_column(conn: sqlite3.Connection, table: str, + column: str, col_type: str) -> None: + """安全添加列(已存在则跳过)""" + try: + conn.execute(f"ALTER TABLE {table} ADD COLUMN {column} {col_type}") + except sqlite3.OperationalError as e: + if "duplicate column" not in str(e).lower(): + raise + + def get_connection(db_path: Path) -> sqlite3.Connection: """获取数据库连接(WAL + busy_timeout + foreign_keys)""" return _connect(db_path)