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

  # `kind` splits the queue between forecast-hour work (the 18 f01..f18 rows
  # Rust has owned since Phase 2) and analysis-hour work (f00, which Elixir
  # still runs because it carries the native-level duct merge, NEXRAD, and
  # commercial-link enrichment). Stream A of the Phase 3 migration plan
  # ports those enrichments to Rust and flips f00 onto this queue too.
  # Forecast rows default to 'forecast' so existing producers keep working
  # during the transition.
  def change do
    alter table(:grid_tasks) do
      add :kind, :string, null: false, default: "forecast"
    end

    create index(:grid_tasks, [:status, :kind])

    # Replace (run_time, forecast_hour) uniqueness with a three-column key so
    # an analysis row and a forecast row for f00 can coexist during the
    # cutover. Post-cutover the analysis row replaces f00 outright.
    drop_if_exists unique_index(:grid_tasks, [:run_time, :forecast_hour])
    create unique_index(:grid_tasks, [:run_time, :forecast_hour, :kind])
  end
end
