Clean migrate code in init db
This commit is contained in:
@@ -4,7 +4,6 @@ import argparse
|
|||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
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__)))
|
||||||
@@ -57,39 +56,6 @@ 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 "first_turn_mode" not in columns:
|
|
||||||
alter_statements.append(
|
|
||||||
"ALTER TABLE assistants ADD COLUMN first_turn_mode VARCHAR(32) DEFAULT 'bot_first'"
|
|
||||||
)
|
|
||||||
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.vector_store import vector_store
|
from app.vector_store import vector_store
|
||||||
@@ -400,7 +366,7 @@ if __name__ == "__main__":
|
|||||||
if args.rebuild_db:
|
if args.rebuild_db:
|
||||||
init_db()
|
init_db()
|
||||||
else:
|
else:
|
||||||
migrate_db_schema()
|
print("ℹ️ 跳过数据库结构变更(未指定 --rebuild-db)")
|
||||||
|
|
||||||
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