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

  # ALTER TABLE ... RENAME is atomic and instant (no row rewrite). User-created
  # indexes keep their old names after the table rename, so we rename them too
  # for consistency. Implicit PK index / sequence are renamed automatically by
  # PostgreSQL.
  def up do
    rename table(:era5_profiles), to: table(:narr_profiles)

    execute "ALTER INDEX era5_profiles_lat_lon_valid_time_index RENAME TO narr_profiles_lat_lon_valid_time_index"

    execute "ALTER INDEX era5_profiles_valid_time_index RENAME TO narr_profiles_valid_time_index"

    execute "ALTER INDEX era5_profiles_valid_time_lat_lon_index RENAME TO narr_profiles_valid_time_lat_lon_index"
  end

  def down do
    execute "ALTER INDEX narr_profiles_valid_time_lat_lon_index RENAME TO era5_profiles_valid_time_lat_lon_index"

    execute "ALTER INDEX narr_profiles_valid_time_index RENAME TO era5_profiles_valid_time_index"

    execute "ALTER INDEX narr_profiles_lat_lon_valid_time_index RENAME TO era5_profiles_lat_lon_valid_time_index"

    rename table(:narr_profiles), to: table(:era5_profiles)
  end
end
