把建表语句移到迁移函数之前执行,并把 agent_user、agent_user_tokens、bot_shares 也加入基础建表列表。

This commit is contained in:
朱潮 2026-05-12 20:49:16 +08:00
parent 9984b5c39f
commit 81d0ebdce7

View File

@ -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")