From 81d0ebdce7f1614a176c63b12501df4748cb9291 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E6=BD=AE?= Date: Tue, 12 May 2026 20:49:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=8A=E5=BB=BA=E8=A1=A8=E8=AF=AD=E5=8F=A5?= =?UTF-8?q?=E7=A7=BB=E5=88=B0=E8=BF=81=E7=A7=BB=E5=87=BD=E6=95=B0=E4=B9=8B?= =?UTF-8?q?=E5=89=8D=E6=89=A7=E8=A1=8C=EF=BC=8C=E5=B9=B6=E6=8A=8A=20agent?= =?UTF-8?q?=5Fuser=E3=80=81agent=5Fuser=5Ftokens=E3=80=81bot=5Fshares=20?= =?UTF-8?q?=E4=B9=9F=E5=8A=A0=E5=85=A5=E5=9F=BA=E7=A1=80=E5=BB=BA=E8=A1=A8?= =?UTF-8?q?=E5=88=97=E8=A1=A8=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- routes/bot_manager.py | 66 ++++++++++++++++++++++++++++++------------- 1 file changed, 47 insertions(+), 19 deletions(-) diff --git a/routes/bot_manager.py b/routes/bot_manager.py index 89dec1f..f553552 100644 --- a/routes/bot_manager.py +++ b/routes/bot_manager.py @@ -1303,20 +1303,26 @@ async def init_bot_manager_tables(): """ pool = get_db_pool_manager().pool - # 首先执行迁移(如果需要) - # 1. Bot settings 迁移 - await migrate_bot_settings_to_jsonb() - # 2. User 和 shares 迁移 - await migrate_bot_owner_and_shares() - # 3. Marketplace 字段迁移 - await migrate_add_marketplace_fields() - # 4. Single Agent Mode 字段迁移 - await migrate_single_agent_mode() - # 5. Subaccount Support 字段迁移 - await migrate_subaccount_support() - - # SQL 表创建语句 + # Step 1: Create base tables first (order matters due to FK references) tables_sql = [ + # agent_user 表 + """ + CREATE TABLE IF NOT EXISTS agent_user ( + id UUID PRIMARY KEY DEFAULT gen_random_uuid(), + username VARCHAR(255) UNIQUE NOT NULL, + email VARCHAR(255) UNIQUE, + password_hash VARCHAR(255) NOT NULL, + created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(), + updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(), + last_login TIMESTAMP WITH TIME ZONE, + is_active BOOLEAN DEFAULT TRUE, + is_admin BOOLEAN DEFAULT FALSE + ) + """, + "CREATE INDEX IF NOT EXISTS idx_agent_user_username ON agent_user(username)", + "CREATE INDEX IF NOT EXISTS idx_agent_user_email ON agent_user(email)", + "CREATE INDEX IF NOT EXISTS idx_agent_user_is_active ON agent_user(is_active)", + # admin_tokens 表(用于存储登录 token) """ CREATE TABLE IF NOT EXISTS agent_admin_tokens ( @@ -1327,12 +1333,9 @@ async def init_bot_manager_tables(): created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() ) """, - # admin_tokens 索引 "CREATE INDEX IF NOT EXISTS idx_agent_admin_tokens_token ON agent_admin_tokens(token)", "CREATE INDEX IF NOT EXISTS idx_agent_admin_tokens_expires ON agent_admin_tokens(expires_at)", - # agent_models 表已废弃,模型管理已迁移到 New API - # bots 表(合并 settings 为 JSONB 字段) """ CREATE TABLE IF NOT EXISTS agent_bots ( @@ -1349,9 +1352,22 @@ async def init_bot_manager_tables(): updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() ) """, - # bots 索引 "CREATE INDEX IF NOT EXISTS idx_agent_bots_bot_id ON agent_bots(bot_id)", + # agent_user_tokens 表 + """ + CREATE TABLE IF NOT EXISTS agent_user_tokens ( + id UUID PRIMARY KEY DEFAULT gen_random_uuid(), + user_id UUID NOT NULL REFERENCES agent_user(id) ON DELETE CASCADE, + token VARCHAR(255) NOT NULL UNIQUE, + expires_at TIMESTAMP WITH TIME ZONE NOT NULL, + created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() + ) + """, + "CREATE INDEX IF NOT EXISTS idx_agent_user_tokens_token ON agent_user_tokens(token)", + "CREATE INDEX IF NOT EXISTS idx_agent_user_tokens_user_id ON agent_user_tokens(user_id)", + "CREATE INDEX IF NOT EXISTS idx_agent_user_tokens_expires ON agent_user_tokens(expires_at)", + # mcp_servers 表 """ CREATE TABLE IF NOT EXISTS agent_mcp_servers ( @@ -1365,7 +1381,6 @@ async def init_bot_manager_tables(): updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() ) """, - # mcp_servers 索引 "CREATE INDEX IF NOT EXISTS idx_agent_mcp_servers_bot_id ON agent_mcp_servers(bot_id)", "CREATE INDEX IF NOT EXISTS idx_agent_mcp_servers_enabled ON agent_mcp_servers(enabled)", @@ -1379,7 +1394,6 @@ async def init_bot_manager_tables(): updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() ) """, - # chat_sessions 索引 "CREATE INDEX IF NOT EXISTS idx_agent_chat_sessions_bot_id ON agent_chat_sessions(bot_id)", "CREATE INDEX IF NOT EXISTS idx_agent_chat_sessions_created ON agent_chat_sessions(created_at DESC)", ] @@ -1390,6 +1404,20 @@ async def init_bot_manager_tables(): await cursor.execute(sql) await conn.commit() + logger.info("Bot Manager base tables created successfully") + + # Step 2: Run migrations (add columns, create additional tables, etc.) + # 1. Bot settings 迁移 + await migrate_bot_settings_to_jsonb() + # 2. User 和 shares 迁移 + await migrate_bot_owner_and_shares() + # 3. Marketplace 字段迁移 + await migrate_add_marketplace_fields() + # 4. Single Agent Mode 字段迁移 + await migrate_single_agent_mode() + # 5. Subaccount Support 字段迁移 + await migrate_subaccount_support() + logger.info("Bot Manager tables initialized successfully")