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

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

  use Ecto.Migration

  def up do
    create table(:options, primary_key: false) do
      add :extension_id,
          references(:extensions, column: :id, name: "options_extension_id_fkey", type: :uuid)

      add :library_version_id,
          references(:library_versions,
            column: :id,
            name: "options_library_version_id_fkey",
            type: :uuid,
            on_delete: :delete_all
          )

      add :dsl_id,
          references(:dsls,
            column: :id,
            name: "options_dsl_id_fkey",
            type: :uuid,
            on_delete: :delete_all
          )

      add :updated_at, :utc_datetime_usec, null: false
      add :inserted_at, :utc_datetime_usec, null: false
      add :order, :bigint, null: false
      add :path, {:array, :text}
      add :default, :text
      add :links, :map, default: %{}
      add :argument_index, :bigint
      add :required, :boolean, null: false, default: false
      add :doc_html, :text
      add :doc, :text, null: false, default: ""
      add :type, :text, null: false
      add :name, :text, null: false
      add :id, :uuid, null: false, primary_key: true
      add :sanitized_path, :text, null: false
    end

    create table(:modules, primary_key: false) do
      add :library_version_id,
          references(:library_versions,
            column: :id,
            name: "modules_library_version_id_fkey",
            type: :uuid,
            on_delete: :delete_all
          )

      add :updated_at, :utc_datetime_usec, null: false
      add :inserted_at, :utc_datetime_usec, null: false
      add :order, :bigint, null: false
      add :doc_html, :text
      add :doc, :text, null: false, default: ""
      add :file, :text
      add :category, :text, null: false, default: "Misc"
      add :name, :text, null: false
      add :id, :uuid, null: false, primary_key: true
      add :sanitized_name, :text, null: false
    end

    create table(:mix_tasks, primary_key: false) do
      add :library_version_id,
          references(:library_versions,
            column: :id,
            name: "mix_tasks_library_version_id_fkey",
            type: :uuid,
            on_delete: :delete_all
          )

      add :updated_at, :utc_datetime_usec, null: false
      add :inserted_at, :utc_datetime_usec, null: false
      add :order, :bigint, null: false
      add :doc_html, :text
      add :doc, :text, null: false, default: ""
      add :module_name, :text
      add :file, :text
      add :category, :text, null: false, default: "Misc"
      add :name, :text, null: false
      add :id, :uuid, null: false, primary_key: true
      add :sanitized_name, :text, null: false
    end

    create table(:library_versions, primary_key: false) do
      add :library_id,
          references(:libraries,
            column: :id,
            name: "library_versions_library_id_fkey",
            type: :uuid
          )

      add :updated_at, :utc_datetime_usec, null: false
      add :inserted_at, :utc_datetime_usec, null: false
      add :hydrated, :boolean, null: false, default: false
      add :version, :text, null: false
      add :id, :uuid, null: false, primary_key: true
      add :sanitized_version, :text, null: false
    end

    create table(:libraries, primary_key: false) do
      add :updated_at, :utc_datetime_usec, null: false
      add :inserted_at, :utc_datetime_usec, null: false
      add :skip_versions, {:array, :text}, null: false, default: []
      add :mix_project, :text
      add :module_prefixes, {:array, :text}, null: false, default: []
      add :repo_org, :text, null: false, default: "ash-project"
      add :description, :text
      add :order, :bigint, null: false
      add :display_name, :text, null: false
      add :name, :text, null: false
      add :id, :uuid, null: false, primary_key: true
    end

    create unique_index(:library_versions, [:version, :library_id],
             name: "library_versions_unique_version_for_library_index"
           )

    create unique_index(:libraries, [:name], name: "libraries_unique_name_index")

    create unique_index(:libraries, [:order], name: "libraries_unique_order_index")

    create table(:guides, primary_key: false) do
      add :library_version_id,
          references(:library_versions,
            column: :id,
            name: "guides_library_version_id_fkey",
            type: :uuid,
            on_delete: :delete_all
          )

      add :updated_at, :utc_datetime_usec, null: false
      add :inserted_at, :utc_datetime_usec, null: false
      add :default, :boolean, null: false, default: false
      add :sanitized_route, :text, null: false
      add :route, :text, null: false
      add :category, :text, null: false, default: "Topics"
      add :text_html, :text
      add :text, :text, null: false, default: ""
      add :name, :text, null: false
      add :order, :bigint, null: false
      add :id, :uuid, null: false, primary_key: true
      add :sanitized_name, :text, null: false
    end

    create table(:functions, primary_key: false) do
      add :module_id,
          references(:modules, column: :id, name: "functions_module_id_fkey", type: :uuid)

      add :library_version_id,
          references(:library_versions,
            column: :id,
            name: "functions_library_version_id_fkey",
            type: :uuid,
            on_delete: :delete_all
          )

      add :updated_at, :utc_datetime_usec, null: false
      add :inserted_at, :utc_datetime_usec, null: false
      add :deprecated, :text
      add :order, :bigint, null: false
      add :doc_html, :text
      add :doc, :text, null: false, default: ""
      add :heads_html, {:array, :text}, default: []
      add :heads, {:array, :text}, default: []
      add :type, :text, null: false
      add :arity, :bigint, null: false
      add :line, :bigint
      add :file, :text
      add :name, :text, null: false
      add :id, :uuid, null: false, primary_key: true
      add :sanitized_name, :text, null: false
    end

    create table(:extensions, primary_key: false) do
      add :library_version_id,
          references(:library_versions,
            column: :id,
            name: "extensions_library_version_id_fkey",
            type: :uuid,
            on_delete: :delete_all
          )

      add :updated_at, :utc_datetime_usec, null: false
      add :inserted_at, :utc_datetime_usec, null: false
      add :module, :text
      add :order, :bigint, null: false
      add :type, :text, null: false
      add :doc_html, :text
      add :doc, :text, null: false, default: ""
      add :default_for_target, :boolean, default: false
      add :target, :text
      add :name, :text, null: false
      add :id, :uuid, null: false, primary_key: true
      add :sanitized_name, :text, null: false
    end

    create unique_index(:extensions, [:name, :library_version_id],
             name: "extensions_unique_name_by_library_version_index"
           )

    create table(:dsls, primary_key: false) do
      add :dsl_id,
          references(:dsls,
            column: :id,
            name: "dsls_dsl_id_fkey",
            type: :uuid,
            on_delete: :delete_all
          )

      add :extension_id,
          references(:extensions, column: :id, name: "dsls_extension_id_fkey", type: :uuid)

      add :library_version_id,
          references(:library_versions,
            column: :id,
            name: "dsls_library_version_id_fkey",
            type: :uuid,
            on_delete: :delete_all
          )

      add :updated_at, :utc_datetime_usec, null: false
      add :inserted_at, :utc_datetime_usec, null: false
      add :type, :text, null: false
      add :order, :bigint, null: false
      add :recursive_as, :text
      add :path, {:array, :text}
      add :arg_defaults, :map
      add :optional_args, {:array, :text}, default: []
      add :args, {:array, :text}
      add :examples, {:array, :text}
      add :imports, {:array, :text}, default: []
      add :doc_html, :text
      add :doc, :text, null: false, default: ""
      add :requires_extension, :text
      add :name, :text, null: false
      add :id, :uuid, null: false, primary_key: true
      add :sanitized_path, :text, null: false
    end
  end

  def down do
    drop constraint(:dsls, "dsls_library_version_id_fkey")

    drop constraint(:dsls, "dsls_extension_id_fkey")

    drop constraint(:dsls, "dsls_dsl_id_fkey")

    drop table(:dsls)

    drop_if_exists unique_index(:extensions, [:name, :library_version_id],
                     name: "extensions_unique_name_by_library_version_index"
                   )

    drop constraint(:extensions, "extensions_library_version_id_fkey")

    drop table(:extensions)

    drop constraint(:functions, "functions_library_version_id_fkey")

    drop constraint(:functions, "functions_module_id_fkey")

    drop table(:functions)

    drop constraint(:guides, "guides_library_version_id_fkey")

    drop table(:guides)

    drop_if_exists unique_index(:libraries, [:order], name: "libraries_unique_order_index")

    drop_if_exists unique_index(:libraries, [:name], name: "libraries_unique_name_index")

    drop_if_exists unique_index(:library_versions, [:version, :library_id],
                     name: "library_versions_unique_version_for_library_index"
                   )

    drop table(:libraries)

    drop constraint(:library_versions, "library_versions_library_id_fkey")

    drop table(:library_versions)

    drop constraint(:mix_tasks, "mix_tasks_library_version_id_fkey")

    drop table(:mix_tasks)

    drop constraint(:modules, "modules_library_version_id_fkey")

    drop table(:modules)

    drop constraint(:options, "options_dsl_id_fkey")

    drop constraint(:options, "options_library_version_id_fkey")

    drop constraint(:options, "options_extension_id_fkey")

    drop table(:options)
  end
end
