From ae42e4c728e00638cf1c118f0c73abad30753435 Mon Sep 17 00:00:00 2001 From: Cho Young-Hwi Date: Tue, 17 Mar 2026 15:12:55 +0000 Subject: [PATCH 1/2] [#259] Add migration to reconcile stale plot_count data Idempotent UPDATE that sets storylines.plot_count and last_plot_time from COUNT(*) and MAX(block_timestamp) on the plots table. Only updates rows where values differ from computed aggregates. Co-Authored-By: Claude Opus 4.6 (1M context) --- .../migrations/00013_reconcile_plot_counts.sql | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 supabase/migrations/00013_reconcile_plot_counts.sql diff --git a/supabase/migrations/00013_reconcile_plot_counts.sql b/supabase/migrations/00013_reconcile_plot_counts.sql new file mode 100644 index 00000000..a37eeac4 --- /dev/null +++ b/supabase/migrations/00013_reconcile_plot_counts.sql @@ -0,0 +1,16 @@ +-- Reconcile stale storylines.plot_count and last_plot_time from plots table. +-- Idempotent: only updates rows where values differ from computed aggregates. +-- Context: PR #258 fixed the code path; this migration fixes existing stale data. + +UPDATE storylines s +SET plot_count = sub.cnt, + last_plot_time = sub.latest +FROM ( + SELECT storyline_id, + COUNT(*) AS cnt, + MAX(block_timestamp) AS latest + FROM plots + GROUP BY storyline_id +) sub +WHERE s.storyline_id = sub.storyline_id + AND (s.plot_count != sub.cnt OR s.last_plot_time != sub.latest); From 3f8c01b3108f64ab5b5e66a60a8559c33cb25072 Mon Sep 17 00:00:00 2001 From: Cho Young-Hwi Date: Tue, 17 Mar 2026 15:14:09 +0000 Subject: [PATCH 2/2] [#259] Fix NULL-safety: use IS DISTINCT FROM in reconciliation NULL != value evaluates to NULL in Postgres, silently skipping stale rows with NULL last_plot_time. IS DISTINCT FROM handles NULLs correctly. Co-Authored-By: Claude Opus 4.6 (1M context) --- supabase/migrations/00013_reconcile_plot_counts.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/supabase/migrations/00013_reconcile_plot_counts.sql b/supabase/migrations/00013_reconcile_plot_counts.sql index a37eeac4..b55c22a0 100644 --- a/supabase/migrations/00013_reconcile_plot_counts.sql +++ b/supabase/migrations/00013_reconcile_plot_counts.sql @@ -13,4 +13,4 @@ FROM ( GROUP BY storyline_id ) sub WHERE s.storyline_id = sub.storyline_id - AND (s.plot_count != sub.cnt OR s.last_plot_time != sub.latest); + AND (s.plot_count IS DISTINCT FROM sub.cnt OR s.last_plot_time IS DISTINCT FROM sub.latest);