refactor: Improve NULL handling in unlink_trades_from_plan function

This commit is contained in:
Bobby (aider) 2025-02-11 21:06:37 -08:00
parent 84378e32f8
commit b35349c885

View File

@ -316,12 +316,24 @@ def unlink_trades_from_plan(plan_id: int) -> bool:
"""Unlink all trades from a trading plan"""
with create_client() as client:
try:
query = """
# First update the plan's metrics to NULL
plan_update_query = """
ALTER TABLE trading_plans
UPDATE
win_rate = NULL,
average_return_per_trade = NULL,
profit_factor = NULL
WHERE id = %(plan_id)s
"""
client.command(plan_update_query, {'plan_id': plan_id})
# Then unlink the trades
trades_update_query = """
ALTER TABLE stock_db.trades
UPDATE plan_id = NULL
WHERE plan_id = %(plan_id)s
"""
client.command(query, {'plan_id': plan_id})
client.command(trades_update_query, {'plan_id': plan_id})
return True
except Exception as e:
print(f"Error unlinking trades from plan: {e}")