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

  def up do
    # Aggressive autovacuum for propagation_scores — high churn table with
    # ~14M upserts per hourly run. Default settings can't keep up, causing
    # 100M+ dead tuples and 70GB+ bloat.
    execute """
    ALTER TABLE propagation_scores SET (
      autovacuum_vacuum_scale_factor = 0.01,
      autovacuum_vacuum_threshold = 10000,
      autovacuum_vacuum_cost_delay = 0,
      autovacuum_vacuum_cost_limit = 2000,
      autovacuum_analyze_scale_factor = 0.02
    )
    """

    # Also run ANALYZE to refresh stats after this change
    execute "ANALYZE propagation_scores"
  end

  def down do
    execute """
    ALTER TABLE propagation_scores RESET (
      autovacuum_vacuum_scale_factor,
      autovacuum_vacuum_threshold,
      autovacuum_vacuum_cost_delay,
      autovacuum_vacuum_cost_limit,
      autovacuum_analyze_scale_factor
    )
    """
  end
end
