Files
taiji-AI-PAD/services/mcp-server/scripts/migrate_numeric_fields.py
T
2025-12-26 15:16:32 +00:00

60 lines
1.8 KiB
Python

#!/usr/bin/env python3
"""
数据库迁移脚本:修复 channels 表的 custom_agent_cpu 和 custom_agent_memory 字段精度
问题:原字段定义为 NUMERIC(5,2),最大只能存储 999.99
修复:将字段改为 NUMERIC(12,2),支持更大的值
使用方法:
docker exec -it taiji-mcp-server python scripts/migrate_numeric_fields.py
"""
import asyncio
import os
import sys
# 添加项目根目录到路径
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from sqlalchemy import text
from sqlalchemy.ext.asyncio import create_async_engine
async def migrate():
"""执行数据库迁移"""
database_url = os.getenv(
"DATABASE_URL",
"postgresql+asyncpg://postgres:postgres@db:5432/taiji_mcp"
)
print(f"连接数据库: {database_url.split('@')[1] if '@' in database_url else database_url}")
engine = create_async_engine(database_url, echo=True)
async with engine.begin() as conn:
print("\n=== 开始迁移 ===\n")
# 修改 custom_agent_cpu 字段精度
print("1. 修改 custom_agent_cpu 字段: NUMERIC(5,2) -> NUMERIC(12,2)")
await conn.execute(text("""
ALTER TABLE channels
ALTER COLUMN custom_agent_cpu TYPE NUMERIC(12, 2)
"""))
print(" ✓ custom_agent_cpu 已更新\n")
# 修改 custom_agent_memory 字段精度
print("2. 修改 custom_agent_memory 字段: NUMERIC(5,2) -> NUMERIC(12,2)")
await conn.execute(text("""
ALTER TABLE channels
ALTER COLUMN custom_agent_memory TYPE NUMERIC(12, 2)
"""))
print(" ✓ custom_agent_memory 已更新\n")
print("=== 迁移完成 ===")
await engine.dispose()
if __name__ == "__main__":
asyncio.run(migrate())