defmodule Microwaveprop.Repo.Migrations.MakePropagationScoresUnlogged do
  use Ecto.Migration

  @moduledoc """
  propagation_scores holds ~475k rows per forecast hour × 19 hours of
  retention ≈ 9M row churns per 3-hour cron. Writing every row to WAL
  is a significant share of the scoring+upsert wall time. Set the
  table UNLOGGED so inserts bypass WAL entirely.

  Durability tradeoff: on an unclean Postgres shutdown the table is
  truncated on startup. That's acceptable here — PropagationGridWorker
  rebuilds the full CONUS grid from HRRR every 3 hours, so a lost
  table is re-populated within one cron cycle. The map page already
  handles an empty score table gracefully (shows "no data").

  Lock note: SET UNLOGGED rewrites the table, which takes an
  AccessExclusiveLock for the duration. In prod this momentarily
  blocks other writers (AsosAdjustmentWorker, PropagationGridWorker,
  PropagationPruneWorker all share the :propagation queue). Run
  during a lull. With a 1.4M-row table the rewrite is fast (~seconds).
  """

  def up do
    execute "ALTER TABLE propagation_scores SET UNLOGGED"
  end

  def down do
    execute "ALTER TABLE propagation_scores SET LOGGED"
  end
end
