From a17ef6f18234910c87cc89eed967e3001c5ce0e8 Mon Sep 17 00:00:00 2001 From: Xin Wang Date: Thu, 12 Feb 2026 14:29:47 +0800 Subject: [PATCH] Remove db migration code to init --- api/app/main.py | 27 --------------------------- api/init_db.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 27 deletions(-) diff --git a/api/app/main.py b/api/app/main.py index 36b2f18..a193ff9 100644 --- a/api/app/main.py +++ b/api/app/main.py @@ -2,42 +2,15 @@ from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from contextlib import asynccontextmanager import os -from sqlalchemy import inspect, text from .db import Base, engine from .routers import assistants, voices, workflows, history, knowledge, llm, asr, tools -def _ensure_assistant_columns() -> None: - """Best-effort SQLite schema evolution for assistant flags.""" - inspector = inspect(engine) - if "assistants" not in inspector.get_table_names(): - return - - columns = {col["name"] for col in inspector.get_columns("assistants")} - alter_statements = [] - if "generated_opener_enabled" not in columns: - alter_statements.append( - "ALTER TABLE assistants ADD COLUMN generated_opener_enabled BOOLEAN DEFAULT 0" - ) - if "bot_cannot_be_interrupted" not in columns: - alter_statements.append( - "ALTER TABLE assistants ADD COLUMN bot_cannot_be_interrupted BOOLEAN DEFAULT 0" - ) - - if not alter_statements: - return - - with engine.begin() as conn: - for stmt in alter_statements: - conn.execute(text(stmt)) - - @asynccontextmanager async def lifespan(app: FastAPI): # 启动时创建表 Base.metadata.create_all(bind=engine) - _ensure_assistant_columns() yield diff --git a/api/init_db.py b/api/init_db.py index 21b3dd8..fe8d342 100644 --- a/api/init_db.py +++ b/api/init_db.py @@ -3,6 +3,7 @@ import argparse import os import sys +from sqlalchemy import inspect, text # 添加路径 sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) @@ -23,6 +24,35 @@ def init_db(): print("✅ 数据库表创建完成") +def migrate_db_schema(): + """对现有数据库执行非破坏性 schema 迁移。""" + inspector = inspect(engine) + table_names = set(inspector.get_table_names()) + if "assistants" not in table_names: + print("ℹ️ assistants 表不存在,跳过增量迁移") + return + + columns = {col["name"] for col in inspector.get_columns("assistants")} + alter_statements = [] + if "generated_opener_enabled" not in columns: + alter_statements.append( + "ALTER TABLE assistants ADD COLUMN generated_opener_enabled BOOLEAN DEFAULT 0" + ) + if "bot_cannot_be_interrupted" not in columns: + alter_statements.append( + "ALTER TABLE assistants ADD COLUMN bot_cannot_be_interrupted BOOLEAN DEFAULT 0" + ) + + if not alter_statements: + print("✅ Schema 迁移检查完成(无需变更)") + return + + with engine.begin() as conn: + for stmt in alter_statements: + conn.execute(text(stmt)) + print(f"✅ Schema 迁移完成(应用 {len(alter_statements)} 条 ALTER)") + + def rebuild_vector_store(reset_doc_status: bool = True): """重建知识库向量集合(按 DB 中的 KB 列表重建 collection 壳)。""" from app.db import SessionLocal @@ -455,6 +485,8 @@ if __name__ == "__main__": if args.rebuild_db: init_db() + else: + migrate_db_schema() if args.recreate_tool_db: init_default_tools(recreate=True)