113 lines
4.3 KiB
Python
113 lines
4.3 KiB
Python
|
|
"""初始数据库模式
|
||
|
|
|
||
|
|
Revision ID: 001
|
||
|
|
Revises:
|
||
|
|
Create Date: 2024-01-01 00:00:00
|
||
|
|
|
||
|
|
"""
|
||
|
|
from alembic import op
|
||
|
|
import sqlalchemy as sa
|
||
|
|
from sqlalchemy.dialects import mysql
|
||
|
|
|
||
|
|
# Alembic 使用的版本标识符
|
||
|
|
revision = '001'
|
||
|
|
down_revision = None
|
||
|
|
branch_labels = None
|
||
|
|
depends_on = None
|
||
|
|
|
||
|
|
|
||
|
|
def upgrade() -> None:
|
||
|
|
# 创建 models 表
|
||
|
|
op.create_table(
|
||
|
|
'models',
|
||
|
|
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
|
||
|
|
sa.Column('name', sa.String(length=128), nullable=False),
|
||
|
|
sa.Column('type', sa.String(length=32), nullable=False),
|
||
|
|
sa.Column('config', sa.JSON(), nullable=False),
|
||
|
|
sa.Column('is_default', sa.Boolean(), nullable=True),
|
||
|
|
sa.Column('status', sa.String(length=32), nullable=True),
|
||
|
|
sa.Column('created_at', sa.DateTime(), nullable=True),
|
||
|
|
sa.PrimaryKeyConstraint('id'),
|
||
|
|
sa.UniqueConstraint('name')
|
||
|
|
)
|
||
|
|
op.create_index('idx_models_type', 'models', ['type'])
|
||
|
|
op.create_index('idx_models_status', 'models', ['status'])
|
||
|
|
|
||
|
|
# 创建 knowledge_bases 表
|
||
|
|
op.create_table(
|
||
|
|
'knowledge_bases',
|
||
|
|
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
|
||
|
|
sa.Column('name', sa.String(length=128), nullable=False),
|
||
|
|
sa.Column('description', sa.Text(), nullable=True),
|
||
|
|
sa.Column('created_at', sa.DateTime(), nullable=True),
|
||
|
|
sa.PrimaryKeyConstraint('id'),
|
||
|
|
sa.UniqueConstraint('name')
|
||
|
|
)
|
||
|
|
|
||
|
|
# 创建 documents 表
|
||
|
|
op.create_table(
|
||
|
|
'documents',
|
||
|
|
sa.Column('id', sa.BigInteger(), autoincrement=True, nullable=False),
|
||
|
|
sa.Column('kb_id', sa.Integer(), nullable=False),
|
||
|
|
sa.Column('title', sa.String(length=512), nullable=True),
|
||
|
|
sa.Column('content', sa.Text(), nullable=False),
|
||
|
|
sa.Column('source', sa.String(length=512), nullable=True),
|
||
|
|
sa.Column('doc_metadata', sa.JSON(), nullable=True),
|
||
|
|
sa.Column('embedding_id', sa.String(length=128), nullable=True),
|
||
|
|
sa.Column('created_at', sa.DateTime(), nullable=True),
|
||
|
|
sa.ForeignKeyConstraint(['kb_id'], ['knowledge_bases.id'], ),
|
||
|
|
sa.PrimaryKeyConstraint('id')
|
||
|
|
)
|
||
|
|
op.create_index('idx_documents_kb_id', 'documents', ['kb_id'])
|
||
|
|
|
||
|
|
# 创建 conversations 表
|
||
|
|
op.create_table(
|
||
|
|
'conversations',
|
||
|
|
sa.Column('id', sa.BigInteger(), autoincrement=True, nullable=False),
|
||
|
|
sa.Column('user_id', sa.Integer(), nullable=True),
|
||
|
|
sa.Column('title', sa.String(length=255), nullable=True),
|
||
|
|
sa.Column('created_at', sa.DateTime(), nullable=True),
|
||
|
|
sa.PrimaryKeyConstraint('id')
|
||
|
|
)
|
||
|
|
|
||
|
|
# 创建 messages 表
|
||
|
|
op.create_table(
|
||
|
|
'messages',
|
||
|
|
sa.Column('id', sa.BigInteger(), autoincrement=True, nullable=False),
|
||
|
|
sa.Column('conversation_id', sa.BigInteger(), nullable=False),
|
||
|
|
sa.Column('role', sa.String(length=32), nullable=False),
|
||
|
|
sa.Column('content', sa.Text(), nullable=False),
|
||
|
|
sa.Column('msg_metadata', sa.JSON(), nullable=True),
|
||
|
|
sa.Column('created_at', sa.DateTime(), nullable=True),
|
||
|
|
sa.ForeignKeyConstraint(['conversation_id'], ['conversations.id'], ),
|
||
|
|
sa.PrimaryKeyConstraint('id')
|
||
|
|
)
|
||
|
|
op.create_index('idx_messages_conv_id', 'messages', ['conversation_id'])
|
||
|
|
|
||
|
|
# 创建 tool_calls 表
|
||
|
|
op.create_table(
|
||
|
|
'tool_calls',
|
||
|
|
sa.Column('id', sa.BigInteger(), autoincrement=True, nullable=False),
|
||
|
|
sa.Column('agent_id', sa.String(length=128), nullable=False),
|
||
|
|
sa.Column('tool_name', sa.String(length=128), nullable=False),
|
||
|
|
sa.Column('call_input', sa.JSON(), nullable=True),
|
||
|
|
sa.Column('call_output', sa.JSON(), nullable=True),
|
||
|
|
sa.Column('created_at', sa.DateTime(), nullable=True),
|
||
|
|
sa.PrimaryKeyConstraint('id')
|
||
|
|
)
|
||
|
|
op.create_index('idx_tool_calls_agent_id', 'tool_calls', ['agent_id'])
|
||
|
|
|
||
|
|
|
||
|
|
def downgrade() -> None:
|
||
|
|
op.drop_index('idx_tool_calls_agent_id', table_name='tool_calls')
|
||
|
|
op.drop_table('tool_calls')
|
||
|
|
op.drop_index('idx_messages_conv_id', table_name='messages')
|
||
|
|
op.drop_table('messages')
|
||
|
|
op.drop_table('conversations')
|
||
|
|
op.drop_index('idx_documents_kb_id', table_name='documents')
|
||
|
|
op.drop_table('documents')
|
||
|
|
op.drop_table('knowledge_bases')
|
||
|
|
op.drop_index('idx_models_status', table_name='models')
|
||
|
|
op.drop_index('idx_models_type', table_name='models')
|
||
|
|
op.drop_table('models')
|