feat: 添加启动时自动执行数据库迁移

添加 migrate_single_agent_mode 函数,在系统启动时自动添加
single_agent_bot_id 字段到 agent_user 表

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
朱潮 2026-02-26 08:13:36 +08:00
parent 2e928cbc9c
commit 1d82ca9ba8

View File

@ -1041,6 +1041,39 @@ async def migrate_add_marketplace_fields():
logger.info("Marketplace fields migration completed")
async def migrate_single_agent_mode():
"""
添加单智能体模式相关字段到 agent_user
"""
pool = get_db_pool_manager().pool
async with pool.connection() as conn:
async with conn.cursor() as cursor:
# 检查 single_agent_bot_id 字段是否存在
await cursor.execute("""
SELECT column_name
FROM information_schema.columns
WHERE table_name = 'agent_user' AND column_name = 'single_agent_bot_id'
""")
has_single_agent_bot_id = await cursor.fetchone()
if not has_single_agent_bot_id:
logger.info("Adding single_agent_bot_id column to agent_user table")
await cursor.execute("""
ALTER TABLE agent_user
ADD COLUMN single_agent_bot_id UUID
""")
await cursor.execute("""
CREATE INDEX IF NOT EXISTS idx_agent_user_single_agent_bot
ON agent_user(single_agent_bot_id) WHERE single_agent_bot_id IS NOT NULL
""")
logger.info("Single agent mode migration completed")
else:
logger.info("single_agent_bot_id column already exists")
await conn.commit()
async def init_bot_manager_tables():
"""
初始化 Bot Manager 相关的所有数据库表
@ -1054,6 +1087,8 @@ async def init_bot_manager_tables():
await migrate_bot_owner_and_shares()
# 3. Marketplace 字段迁移
await migrate_add_marketplace_fields()
# 4. Single Agent Mode 字段迁移
await migrate_single_agent_mode()
# SQL 表创建语句
tables_sql = [