修复复盘列表与统计并行同步撞锁导致模拟盘加载失败。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-10-07 13:46:38 +08:00
parent cfcc0596ff
commit 2158a30179
5 changed files with 83 additions and 27 deletions
+14 -5
View File
@@ -112,11 +112,16 @@ def migrate_options_utc_wall_clock_to_app(conn: sqlite3.Connection) -> dict[str,
init_options_tables(conn)
trades = _shift_table("options_trades", ("created_at", "closed_at"))
except sqlite3.OperationalError:
return {"skipped": 0, "trades": 0, "review": 0, "hidden": 0, "locked": 1}
except Exception:
trades = 0
review = _shift_table("options_review_trades", ("opened_at", "closed_at"))
hidden = _shift_table("options_review_hidden", ("closed_at",))
try:
review = _shift_table("options_review_trades", ("opened_at", "closed_at"))
hidden = _shift_table("options_review_hidden", ("closed_at",))
except sqlite3.OperationalError:
return {"skipped": 0, "trades": trades, "review": 0, "hidden": 0, "locked": 1}
set_sync_state(conn, _MIGRATE_FLAG, "1")
return {"skipped": 0, "trades": trades, "review": review, "hidden": hidden}
@@ -129,7 +134,7 @@ def repair_swapped_closed_at_signal_note(conn: sqlite3.Connection) -> dict[str,
幂等:同步状态 options_closed_at_note_swap_done=1 后仍扫描可修行(新坏行可再修).
"""
from lib.options.options_review_db import init_options_review_tables
from lib.options.options_review_lib import set_sync_state
from lib.options.options_review_lib import get_sync_state, set_sync_state
init_options_review_tables(conn)
trades_fixed = 0
@@ -174,9 +179,13 @@ def repair_swapped_closed_at_signal_note(conn: sqlite3.Connection) -> dict[str,
(note, hk, closed),
)
review_fixed += int(cur.rowcount or 0)
except sqlite3.OperationalError:
# 并发刷新撞锁时跳过本轮修复,不影响列表读取
return {"trades": 0, "review": 0, "locked": 1}
except Exception:
pass
# 复盘表里仍残留非时间戳 closed_at 的,若能从 local_opt 源修好则已处理;否则仅标记完成
set_sync_state(conn, _SWAP_REPAIR_FLAG, "1")
# 已标记过则不再写同步状态,减少并发写冲突
if get_sync_state(conn, _SWAP_REPAIR_FLAG) != "1":
set_sync_state(conn, _SWAP_REPAIR_FLAG, "1")
return {"trades": trades_fixed, "review": review_fixed}