defmodule AshHq.Repo.Migrations.MigrateResources28 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
    create table(:mix_tasks, primary_key: false) do
      add(:sanitized_name, :text, null: false)
      add(:id, :uuid, null: false, default: fragment("uuid_generate_v4()"), primary_key: true)
      add(:name, :text, null: false)
      add(:category, :text, null: false, default: "Misc")
      add(:file, :text)
      add(:doc, :text, null: false, default: "")
      add(:doc_html, :text)
      add(:order, :bigint, null: false)

      add(
        :library_version_id,
        references(:library_versions,
          column: :id,
          name: "mix_tasks_library_version_id_fkey",
          type: :uuid,
          prefix: "public",
          on_delete: :delete_all
        )
      )
    end

    execute("""
    CREATE INDEX mix_tasks_name_lower_index ON mix_tasks(lower(name));
    """)

    execute("""
    CREATE INDEX mix_tasks_name_trigram_index ON mix_tasks USING GIST (name gist_trgm_ops);
    """)

    execute("""
    CREATE INDEX mix_tasks_search_index ON mix_tasks USING GIN((
      setweight(to_tsvector('english', name), 'A') ||
      setweight(to_tsvector('english', doc), 'D')
    ));
    """)
  end

  def down do
    execute("""
    DROP INDEX mix_tasks_search_index;
    """)

    execute("""
    DROP INDEX mix_tasks_name_trigram_index;
    """)

    execute("""
    DROP INDEX mix_tasks_name_lower_index;
    """)

    drop(constraint(:mix_tasks, "mix_tasks_library_version_id_fkey"))

    drop(table(:mix_tasks))
  end
end
