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')
|