import psycopg2
from config import Config

def migrate():
    # 从 URI 解析连接参数
    # postgresql://user:pass@host:port/dbname
    uri = Config.SQLALCHEMY_DATABASE_URI
    print(f"正在手动连接数据库进行迁移...")
    
    try:
        conn = psycopg2.connect(uri)
        cur = conn.cursor()
        
        # 添加 api_key 字段
        cur.execute("ALTER TABLE users ADD COLUMN IF NOT EXISTS api_key VARCHAR(255);")
        
        conn.commit()
        cur.close()
        conn.close()
        print("✅ 数据库字段 users.api_key 手动添加成功")
    except Exception as e:
        print(f"❌ 迁移失败: {e}")

if __name__ == "__main__":
    migrate()
