From c923fecd8a0e0c278d31ad2f4939597e9fa30784 Mon Sep 17 00:00:00 2001 From: "user.email" Date: Tue, 10 Nov 2026 17:20:27 -0500 Subject: [PATCH 1/2] Fix ML pass failing to write Historic-Picks table --- smartscore/scripts/backtrack/run_daily_picks.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/smartscore/scripts/backtrack/run_daily_picks.py b/smartscore/scripts/backtrack/run_daily_picks.py index 453975a..3efdd08 100644 --- a/smartscore/scripts/backtrack/run_daily_picks.py +++ b/smartscore/scripts/backtrack/run_daily_picks.py @@ -1254,6 +1254,20 @@ def write_historic_picks_if_missing(picks, on_date, env): return columns = _historic_pick_columns(env) + # `id` is the table's (non-null, no-default) primary key. The site's own + # publish renumbers every row sequentially on each write, but this ML pass + # INSERTs brand-new rows, so a fixed 1..N would collide with the rows + # already stored from earlier dates. Derive the next free id from the + # current max instead (and fall back to 1..N only on an empty/odd table). + next_id = 1 + if "id" in columns: + try: + existing_ids = SUPABASE_ADMIN_CLIENT.table(table).select("id").execute().data or [] + parsed = [int(r.get("id")) for r in existing_ids if r.get("id") is not None] + if parsed: + next_id = max(parsed) + 1 + except (TypeError, ValueError): + next_id = 1 records = [] for i, (_, row) in enumerate(picks.iterrows()): record = {"date": on_date} @@ -1262,7 +1276,7 @@ def write_historic_picks_if_missing(picks, on_date, env): pid = pid.item() record["player_id"] = int(pid) if pid is not None and not (isinstance(pid, float) and np.isnan(pid)) else None if "id" in columns: - record["id"] = i + 1 + record["id"] = next_id + i if "Scored" in columns: record["Scored"] = None for col in columns: @@ -1270,6 +1284,7 @@ def write_historic_picks_if_missing(picks, on_date, env): continue if col == "stat": value = stat_value(row.get("ensemble", np.nan)) + record[col] = value elif col in getattr(row, "index", []): value = row.get(col) if isinstance(value, (np.generic,)): From 4298e5392de346e2219b2ea62cc924071eb87848 Mon Sep 17 00:00:00 2001 From: "user.email" Date: Tue, 10 Nov 2026 17:22:22 -0500 Subject: [PATCH 2/2] Prune Historic-Picks to keep ~8 days (ML pass owns the write) --- smartscore/scripts/backtrack/run_daily_picks.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/smartscore/scripts/backtrack/run_daily_picks.py b/smartscore/scripts/backtrack/run_daily_picks.py index 3efdd08..6d5c864 100644 --- a/smartscore/scripts/backtrack/run_daily_picks.py +++ b/smartscore/scripts/backtrack/run_daily_picks.py @@ -1254,6 +1254,21 @@ def write_historic_picks_if_missing(picks, on_date, env): return columns = _historic_pick_columns(env) + + # Keep the table to the most recent DAYS_TO_KEEP_HISTORIC_DATA days (mirrors + # the site's own publish pruning) so it stays a ~week visual instead of + # growing forever. Prune before inserting today's rows. + DAYS_TO_KEEP_HISTORIC_DATA = 8 + try: + all_rows = SUPABASE_ADMIN_CLIENT.table(table).select("date").execute().data or [] + distinct_dates = sorted({r.get("date") for r in all_rows if r.get("date")}) + if len(distinct_dates) >= DAYS_TO_KEEP_HISTORIC_DATA: + to_remove = distinct_dates[: len(distinct_dates) - (DAYS_TO_KEEP_HISTORIC_DATA - 1)] + for d in to_remove: + SUPABASE_ADMIN_CLIENT.table(table).delete().eq("date", d).execute() + except Exception as exc: # noqa: BLE001 + print(f"WARNING: Historic-Picks pruning failed: {exc}", flush=True) + # `id` is the table's (non-null, no-default) primary key. The site's own # publish renumbers every row sequentially on each write, but this ML pass # INSERTs brand-new rows, so a fixed 1..N would collide with the rows