Remove db migration code to init
This commit is contained in:
@@ -2,42 +2,15 @@ from fastapi import FastAPI
|
|||||||
from fastapi.middleware.cors import CORSMiddleware
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
from contextlib import asynccontextmanager
|
from contextlib import asynccontextmanager
|
||||||
import os
|
import os
|
||||||
from sqlalchemy import inspect, text
|
|
||||||
|
|
||||||
from .db import Base, engine
|
from .db import Base, engine
|
||||||
from .routers import assistants, voices, workflows, history, knowledge, llm, asr, tools
|
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
|
@asynccontextmanager
|
||||||
async def lifespan(app: FastAPI):
|
async def lifespan(app: FastAPI):
|
||||||
# 启动时创建表
|
# 启动时创建表
|
||||||
Base.metadata.create_all(bind=engine)
|
Base.metadata.create_all(bind=engine)
|
||||||
_ensure_assistant_columns()
|
|
||||||
yield
|
yield
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
import argparse
|
import argparse
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
from sqlalchemy import inspect, text
|
||||||
|
|
||||||
# 添加路径
|
# 添加路径
|
||||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||||
@@ -23,6 +24,35 @@ def init_db():
|
|||||||
print("✅ 数据库表创建完成")
|
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):
|
def rebuild_vector_store(reset_doc_status: bool = True):
|
||||||
"""重建知识库向量集合(按 DB 中的 KB 列表重建 collection 壳)。"""
|
"""重建知识库向量集合(按 DB 中的 KB 列表重建 collection 壳)。"""
|
||||||
from app.db import SessionLocal
|
from app.db import SessionLocal
|
||||||
@@ -455,6 +485,8 @@ if __name__ == "__main__":
|
|||||||
|
|
||||||
if args.rebuild_db:
|
if args.rebuild_db:
|
||||||
init_db()
|
init_db()
|
||||||
|
else:
|
||||||
|
migrate_db_schema()
|
||||||
|
|
||||||
if args.recreate_tool_db:
|
if args.recreate_tool_db:
|
||||||
init_default_tools(recreate=True)
|
init_default_tools(recreate=True)
|
||||||
|
|||||||
Reference in New Issue
Block a user