defmodule AshHq.Repo.Migrations.MigrateResources45 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
    execute("""
    DROP INDEX discord_messages_search_index;
    """)

    execute("""
    DROP INDEX dsls_name_lower_index;
    """)

    execute("""
    DROP INDEX dsls_name_trigram_index;
    """)

    execute("""
    DROP INDEX dsls_search_index;
    """)

    execute("""
    DROP INDEX extensions_name_lower_index;
    """)

    execute("""
    DROP INDEX extensions_name_trigram_index;
    """)

    execute("""
    DROP INDEX extensions_search_index;
    """)

    execute("""
    DROP INDEX functions_name_lower_index;
    """)

    execute("""
    DROP INDEX functions_name_trigram_index;
    """)

    execute("""
    DROP INDEX functions_search_index;
    """)

    execute("""
    DROP INDEX guides_name_lower_index;
    """)

    execute("""
    DROP INDEX guides_name_trigram_index;
    """)

    execute("""
    DROP INDEX guides_search_index;
    """)

    execute("""
    DROP INDEX library_versions_name_lower_index;
    """)

    execute("""
    DROP INDEX library_versions_name_trigram_index;
    """)

    execute("""
    DROP INDEX library_versions_search_index;
    """)

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

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

    execute("""
    DROP INDEX mix_tasks_search_index;
    """)

    execute("""
    DROP INDEX modules_name_lower_index;
    """)

    execute("""
    DROP INDEX modules_name_trigram_index;
    """)

    execute("""
    DROP INDEX modules_search_index;
    """)

    execute("""
    DROP INDEX options_name_lower_index;
    """)

    execute("""
    DROP INDEX options_name_trigram_index;
    """)

    execute("""
    DROP INDEX options_search_index;
    """)

    execute("""
    ALTER TABLE discord_messages
      ADD COLUMN searchable tsvector
      GENERATED ALWAYS AS (
        setweight(to_tsvector('english', content), 'A')
      ) STORED;
    """)

    execute("""
    ALTER TABLE dsls
      ADD COLUMN searchable tsvector
      GENERATED ALWAYS AS (
        setweight(to_tsvector('english', name), 'A') ||
        setweight(to_tsvector('english', doc), 'B')
      ) STORED;
    """)

    execute("""
    ALTER TABLE extensions
      ADD COLUMN searchable tsvector
      GENERATED ALWAYS AS (
        setweight(to_tsvector('english', name), 'A') ||
        setweight(to_tsvector('english', doc), 'B')
      ) STORED;
    """)

    execute("""
    ALTER TABLE functions
      ADD COLUMN searchable tsvector
      GENERATED ALWAYS AS (
        setweight(to_tsvector('english', name), 'A') ||
        setweight(to_tsvector('english', doc), 'B')
      ) STORED;
    """)

    execute("""
    ALTER TABLE guides
      ADD COLUMN searchable tsvector
      GENERATED ALWAYS AS (
        setweight(to_tsvector('english', name), 'A') ||
        setweight(to_tsvector('english', text), 'B')
      ) STORED;
    """)

    execute("""
    ALTER TABLE library_versions
      ADD COLUMN searchable tsvector
      GENERATED ALWAYS AS (
        setweight(to_tsvector('english', version), 'A')
      ) STORED;
    """)

    execute("""
    ALTER TABLE mix_tasks
      ADD COLUMN searchable tsvector
      GENERATED ALWAYS AS (
        setweight(to_tsvector('english', name), 'A') ||
        setweight(to_tsvector('english', doc), 'B')
      ) STORED;
    """)

    execute("""
    ALTER TABLE modules
      ADD COLUMN searchable tsvector
      GENERATED ALWAYS AS (
        setweight(to_tsvector('english', name), 'A') ||
        setweight(to_tsvector('english', doc), 'B')
      ) STORED;
    """)

    execute("""
    ALTER TABLE options
      ADD COLUMN searchable tsvector
      GENERATED ALWAYS AS (
        setweight(to_tsvector('english', name), 'A') ||
        setweight(to_tsvector('english', doc), 'B')
      ) STORED;
    """)

    create index(:options, ["searchable"], using: "GIN")

    create index(:modules, ["searchable"], using: "GIN")

    create index(:mix_tasks, ["searchable"], using: "GIN")

    create index(:library_versions, ["searchable"], using: "GIN")

    create index(:guides, ["searchable"], using: "GIN")

    create index(:functions, ["searchable"], using: "GIN")

    create index(:extensions, ["searchable"], using: "GIN")

    create index(:dsls, ["searchable"], using: "GIN")

    create index(:discord_messages, ["searchable"], using: "GIN")
  end

  def down do
    drop_if_exists(
      index(:discord_messages, ["searchable"], name: "discord_messages_searchable_index")
    )

    drop_if_exists(index(:dsls, ["searchable"], name: "dsls_searchable_index"))

    drop_if_exists(index(:extensions, ["searchable"], name: "extensions_searchable_index"))

    drop_if_exists(index(:functions, ["searchable"], name: "functions_searchable_index"))

    drop_if_exists(index(:guides, ["searchable"], name: "guides_searchable_index"))

    drop_if_exists(
      index(:library_versions, ["searchable"], name: "library_versions_searchable_index")
    )

    drop_if_exists(index(:mix_tasks, ["searchable"], name: "mix_tasks_searchable_index"))

    drop_if_exists(index(:modules, ["searchable"], name: "modules_searchable_index"))

    drop_if_exists(index(:options, ["searchable"], name: "options_searchable_index"))

    execute("""
    ALTER TABLE options
      DROP COLUMN searchable
    """)

    execute("""
    ALTER TABLE modules
      DROP COLUMN searchable
    """)

    execute("""
    ALTER TABLE mix_tasks
      DROP COLUMN searchable
    """)

    execute("""
    ALTER TABLE library_versions
      DROP COLUMN searchable
    """)

    execute("""
    ALTER TABLE guides
      DROP COLUMN searchable
    """)

    execute("""
    ALTER TABLE functions
      DROP COLUMN searchable
    """)

    execute("""
    ALTER TABLE extensions
      DROP COLUMN searchable
    """)

    execute("""
    ALTER TABLE dsls
      DROP COLUMN searchable
    """)

    execute("""
    ALTER TABLE discord_messages
      DROP COLUMN searchable
    """)

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

    execute("""
    CREATE INDEX options_name_trigram_index ON options USING GIST (name gist_trgm_ops);
    """)

    execute("""
    CREATE INDEX options_name_lower_index ON options(lower(name));
    """)

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

    execute("""
    CREATE INDEX modules_name_trigram_index ON modules USING GIST (name gist_trgm_ops);
    """)

    execute("""
    CREATE INDEX modules_name_lower_index ON modules(lower(name));
    """)

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

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

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

    execute("""
    CREATE INDEX library_versions_search_index ON library_versions USING GIN((
      to_tsvector('english', version)
    ));
    """)

    execute("""
    CREATE INDEX library_versions_name_trigram_index ON library_versions USING GIST (version gist_trgm_ops);
    """)

    execute("""
    CREATE INDEX library_versions_name_lower_index ON library_versions(lower(version));
    """)

    execute("""
    CREATE INDEX guides_search_index ON guides USING GIN((
      setweight(to_tsvector('english', name), 'A') ||
      setweight(to_tsvector('english', text), 'D')
    ));
    """)

    execute("""
    CREATE INDEX guides_name_trigram_index ON guides USING GIST (name gist_trgm_ops);
    """)

    execute("""
    CREATE INDEX guides_name_lower_index ON guides(lower(name));
    """)

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

    execute("""
    CREATE INDEX functions_name_trigram_index ON functions USING GIST (name gist_trgm_ops);
    """)

    execute("""
    CREATE INDEX functions_name_lower_index ON functions(lower(name));
    """)

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

    execute("""
    CREATE INDEX extensions_name_trigram_index ON extensions USING GIST (name gist_trgm_ops);
    """)

    execute("""
    CREATE INDEX extensions_name_lower_index ON extensions(lower(name));
    """)

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

    execute("""
    CREATE INDEX dsls_name_trigram_index ON dsls USING GIST (name gist_trgm_ops);
    """)

    execute("""
    CREATE INDEX dsls_name_lower_index ON dsls(lower(name));
    """)

    execute("""
    CREATE INDEX discord_messages_search_index ON discord_messages USING GIN((
      setweight(to_tsvector('english', content), 'D')
    ));
    """)
  end
end
