diff --git a/supabase/migrations/00013_reconcile_plot_counts.sql b/supabase/migrations/00013_reconcile_plot_counts.sql new file mode 100644 index 00000000..b55c22a0 --- /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 IS DISTINCT FROM sub.cnt OR s.last_plot_time IS DISTINCT FROM sub.latest);