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

  def up do
    execute """
    DO $$
    BEGIN
      IF NOT EXISTS (
        SELECT 1 FROM information_schema.columns
        WHERE table_name = 'surface_observations' AND column_name = 'precip_1h_in'
      ) THEN
        ALTER TABLE surface_observations ADD COLUMN precip_1h_in double precision;
      END IF;

      IF NOT EXISTS (
        SELECT 1 FROM information_schema.columns
        WHERE table_name = 'surface_observations' AND column_name = 'wx_codes'
      ) THEN
        ALTER TABLE surface_observations ADD COLUMN wx_codes character varying(255);
      END IF;
    END
    $$;
    """
  end

  def down do
    alter table(:surface_observations) do
      remove :precip_1h_in
      remove :wx_codes
    end
  end
end
