#!/usr/bin/env python3 """ 数据库迁移:将 template_id 改为可选字段 """ import os from sqlalchemy import create_engine, text # 数据库连接 DATABASE_URL = "postgresql://taiji:By%40123456.@taijipda.postgres.database.azure.com:5432/taijiagnet" print("🔄 开始数据库迁移...") print(f"数据库: {DATABASE_URL.split('@')[1]}") engine = create_engine(DATABASE_URL) try: with engine.connect() as conn: # 修改 template_id 列为可空 print("\n1️⃣ 修改 template_id 为可空字段...") conn.execute(text(""" ALTER TABLE agents ALTER COLUMN template_id DROP NOT NULL; """)) conn.commit() print("✅ template_id 已改为可空") # 验证更改 print("\n2️⃣ 验证表结构...") result = conn.execute(text(""" SELECT column_name, is_nullable, data_type FROM information_schema.columns WHERE table_name = 'agents' AND column_name = 'template_id'; """)) for row in result: print(f"✅ {row[0]}: nullable={row[1]}, type={row[2]}") print("\n✅ 数据库迁移成功完成!") print("\n现在可以创建没有 template_id 的 Agent 了。") except Exception as e: print(f"\n❌ 迁移失败: {e}") import traceback traceback.print_exc()