defmodule Mv.Repo.Migrations.AddTrigramToMembers do
  @moduledoc """
  Updates resources based on their most recent snapshots.

  This file was autogenerated with `mix ash_postgres.generate_migrations`
  """

  use Ecto.Migration

  def up do
    # activate trigram-extension
    execute("CREATE EXTENSION IF NOT EXISTS pg_trgm;")

    # -------------------------------------------------
    # Trigram‑Indizes (GIN) for fields we want to search in
    # -------------------------------------------------
    #
    #   `gin_trgm_ops` ist the operator-class-name
    #

    execute("""
      CREATE INDEX members_first_name_trgm_idx
      ON members
      USING GIN (first_name gin_trgm_ops);
    """)

    execute("""
      CREATE INDEX members_last_name_trgm_idx
      ON members
      USING GIN (last_name gin_trgm_ops);
    """)

    execute("""
      CREATE INDEX members_email_trgm_idx
      ON members
      USING GIN (email gin_trgm_ops);
    """)

    execute("""
      CREATE INDEX members_city_trgm_idx
      ON members
      USING GIN (city gin_trgm_ops);
    """)

    execute("""
      CREATE INDEX members_street_trgm_idx
      ON members
      USING GIN (street gin_trgm_ops);
    """)

    execute("""
      CREATE INDEX members_notes_trgm_idx
      ON members
      USING GIN (notes gin_trgm_ops);
    """)
  end

  def down do
    execute("DROP INDEX IF EXISTS members_first_name_trgm_idx;")
    execute("DROP INDEX IF EXISTS members_last_name_trgm_idx;")
    execute("DROP INDEX IF EXISTS members_email_trgm_idx;")
    execute("DROP INDEX IF EXISTS members_city_trgm_idx;")
    execute("DROP INDEX IF EXISTS members_street_trgm_idx;")
    execute("DROP INDEX IF EXISTS members_notes_trgm_idx;")
  end
end
