From 9381adb4b5d8a743e609adb6b9f6f159d0e60495 Mon Sep 17 00:00:00 2001 From: aaron <> Date: Tue, 24 Feb 2026 22:05:30 +0800 Subject: [PATCH] 1 --- backend/app/models/signal.py | 1 + scripts/migrate_add_entry_zone.py | 77 +++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 scripts/migrate_add_entry_zone.py diff --git a/backend/app/models/signal.py b/backend/app/models/signal.py index e2be392..8c7116a 100644 --- a/backend/app/models/signal.py +++ b/backend/app/models/signal.py @@ -25,6 +25,7 @@ class TradingSignal(Base): # 价格信息 entry_price = Column(Float, nullable=True) + entry_zone = Column(Float, nullable=True) # 挂单价格 stop_loss = Column(Float, nullable=True) take_profit = Column(Float, nullable=True) current_price = Column(Float, nullable=True) # 信号生成时的当前价格 diff --git a/scripts/migrate_add_entry_zone.py b/scripts/migrate_add_entry_zone.py new file mode 100644 index 0000000..ae46ec6 --- /dev/null +++ b/scripts/migrate_add_entry_zone.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python3 +""" +数据库迁移脚本:添加 entry_zone 字段到 trading_signals 表 + +使用方法: + python scripts/migrate_add_entry_zone.py + +或者在服务器上直接执行 SQL: + sqlite3 backend/stock_agent.db "ALTER TABLE trading_signals ADD COLUMN entry_zone FLOAT;" +""" +import sqlite3 +import os +from pathlib import Path + + +def migrate_add_entry_zone(): + """添加 entry_zone 字段""" + + # 数据库路径 + db_path = Path(__file__).parent.parent / "backend" / "stock_agent.db" + + if not db_path.exists(): + print(f"❌ 数据库文件不存在: {db_path}") + return False + + try: + # 连接数据库 + conn = sqlite3.connect(str(db_path)) + cursor = conn.cursor() + + # 检查字段是否已存在 + cursor.execute("PRAGMA table_info(trading_signals)") + columns = [col[1] for col in cursor.fetchall()] + + if 'entry_zone' in columns: + print("✅ entry_zone 字段已存在,无需迁移") + conn.close() + return True + + # 添加字段 + print(f"📝 正在添加 entry_zone 字段到 {db_path}...") + cursor.execute("ALTER TABLE trading_signals ADD COLUMN entry_zone FLOAT") + conn.commit() + + # 验证 + cursor.execute("PRAGMA table_info(trading_signals)") + columns = [col[1] for col in cursor.fetchall()] + + if 'entry_zone' in columns: + print("✅ entry_zone 字段添加成功") + conn.close() + return True + else: + print("❌ 字段添加失败") + conn.close() + return False + + except Exception as e: + print(f"❌ 迁移失败: {e}") + return False + + +if __name__ == "__main__": + print("=" * 60) + print("数据库迁移:添加 entry_zone 字段") + print("=" * 60) + + success = migrate_add_entry_zone() + + if success: + print("\n✅ 迁移完成!") + print("\n请重启服务以使更改生效:") + print(" pm2 restart stock-agent") + else: + print("\n❌ 迁移失败!") + print("\n如果自动迁移失败,可以手动执行 SQL:") + print(" sqlite3 backend/stock_agent.db \"ALTER TABLE trading_signals ADD COLUMN entry_zone FLOAT;\"")