- 创建 Bot 模型:id/name/display_name/avatar/description - 添加 owner_id/agent_id/token_hash/is_system 字段 - 添加 status/capabilities/config 字段 - 修改 Session 模型:添加 bot_id 关联 - 修改 Message 模型:添加 sender_name 和 bot_id - 创建数据库迁移脚本 技术方案实现: - Bot 一对一绑定 Agent - owner_id 区分所有权 - is_system 支持系统级 Bot - capabilities 存储能力标签
70 lines
2.6 KiB
Python
70 lines
2.6 KiB
Python
"""add bot model
|
|
|
|
Revision ID: add_bot_model
|
|
Revises: initial
|
|
Create Date: 2026-03-15 10:22:00.000000
|
|
|
|
"""
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
from sqlalchemy.dialects import postgresql
|
|
|
|
# revision identifiers, used by Alembic.
|
|
revision = 'add_bot_model'
|
|
down_revision = 'initial'
|
|
branch_labels = None
|
|
depends_on = None
|
|
|
|
|
|
def upgrade():
|
|
# 创建 bots 表
|
|
op.create_table('bots',
|
|
sa.Column('id', sa.String(36), primary_key=True),
|
|
sa.Column('name', sa.String(80), unique=True, nullable=False),
|
|
sa.Column('display_name', sa.String(80), nullable=True),
|
|
sa.Column('avatar', sa.String(256), nullable=True),
|
|
sa.Column('description', sa.Text(), nullable=True),
|
|
sa.Column('owner_id', sa.String(36), sa.ForeignKey('users.id'), nullable=False),
|
|
sa.Column('agent_id', sa.String(36), sa.ForeignKey('agents.id'), nullable=True),
|
|
sa.Column('token_hash', sa.String(256), nullable=True),
|
|
sa.Column('is_system', sa.Boolean(), default=False),
|
|
sa.Column('status', sa.String(20), default='offline'),
|
|
sa.Column('capabilities', postgresql.JSON(astext_type=sa.Text()), nullable=True),
|
|
sa.Column('config', postgresql.JSON(astext_type=sa.Text()), nullable=True),
|
|
sa.Column('created_at', sa.DateTime(), nullable=False),
|
|
sa.Column('last_active_at', sa.DateTime(), nullable=True),
|
|
)
|
|
|
|
# 为 sessions 表添加 bot_id 字段
|
|
op.add_column('sessions', sa.Column('bot_id', sa.String(36), sa.ForeignKey('bots.id'), nullable=True))
|
|
|
|
# 为 messages 表添加新字段
|
|
op.add_column('messages', sa.Column('sender_name', sa.String(80), nullable=True))
|
|
op.add_column('messages', sa.Column('bot_id', sa.String(36), sa.ForeignKey('bots.id'), nullable=True))
|
|
|
|
# 创建索引
|
|
op.create_index('ix_bots_name', 'bots', ['name'], unique=True)
|
|
op.create_index('ix_bots_owner_id', 'bots', ['owner_id'])
|
|
op.create_index('ix_bots_agent_id', 'bots', ['agent_id'])
|
|
op.create_index('ix_sessions_bot_id', 'sessions', ['bot_id'])
|
|
op.create_index('ix_messages_bot_id', 'messages', ['bot_id'])
|
|
|
|
|
|
def downgrade():
|
|
# 删除索引
|
|
op.drop_index('ix_messages_bot_id', 'messages')
|
|
op.drop_index('ix_sessions_bot_id', 'sessions')
|
|
op.drop_index('ix_bots_agent_id', 'bots')
|
|
op.drop_index('ix_bots_owner_id', 'bots')
|
|
op.drop_index('ix_bots_name', 'bots')
|
|
|
|
# 删除 messages 表的新字段
|
|
op.drop_column('messages', 'bot_id')
|
|
op.drop_column('messages', 'sender_name')
|
|
|
|
# 删除 sessions 表的 bot_id 字段
|
|
op.drop_column('sessions', 'bot_id')
|
|
|
|
# 删除 bots 表
|
|
op.drop_table('bots')
|