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

  def up do
    execute("""
    UPDATE contacts SET pos1 = jsonb_set(pos1 - 'lng', '{lon}', pos1->'lng')
     WHERE pos1 ? 'lng'
    """)

    execute("""
    UPDATE contacts SET pos2 = jsonb_set(pos2 - 'lng', '{lon}', pos2->'lng')
     WHERE pos2 ? 'lng'
    """)
  end

  def down do
    execute("""
    UPDATE contacts SET pos1 = jsonb_set(pos1 - 'lon', '{lng}', pos1->'lon')
     WHERE pos1 ? 'lon' AND NOT (pos1 ? 'lng')
    """)

    execute("""
    UPDATE contacts SET pos2 = jsonb_set(pos2 - 'lon', '{lng}', pos2->'lon')
     WHERE pos2 ? 'lon' AND NOT (pos2 ? 'lng')
    """)
  end
end
