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

  # Matches the hot range-scan access pattern used by both
  # `Weather.find_nearest_era5/3` (contact detail + path profiles) and
  # `StatusLive.count_era5_done/0` (status page EXISTS correlated subquery):
  #   WHERE valid_time BETWEEN ? AND ?
  #     AND lat BETWEEN ? AND ?
  #     AND lon BETWEEN ? AND ?
  # `valid_time` leads because it collapses the search to a ~1-hour bucket
  # first; lat/lon then filter the ~9×9 tile grid within that bucket.
  # The existing unique index (lat, lon, valid_time) handles upsert dedup
  # but can't serve three-range scans efficiently.
  def change do
    create index(:era5_profiles, [:valid_time, :lat, :lon])
  end
end
