# SPDX-FileCopyrightText: 2020 Zach Daniel
#
# SPDX-License-Identifier: MIT

defmodule AshPostgres.TestRepo.TenantMigrations.MigrateResources7 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(:non_multitenant_post_multitenant_links, primary_key: false, prefix: prefix()) do
      add(:id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true)
      add(:state, :text, default: "active")

      add(
        :source_id,
        references(:multitenant_posts,
          column: :id,
          name: "non_multitenant_post_multitenant_links_source_id_fkey",
          type: :uuid,
          prefix: prefix(),
          on_delete: :delete_all
        ),
        null: false
      )

      add(
        :dest_id,
        references(:posts,
          column: :id,
          name: "non_multitenant_post_multitenant_links_dest_id_fkey",
          type: :uuid,
          prefix: "public",
          on_delete: :delete_all
        ),
        null: false
      )
    end

    create(index(:non_multitenant_post_multitenant_links, [:source_id]))

    create(index(:non_multitenant_post_multitenant_links, [:dest_id]))

    create(
      unique_index(:non_multitenant_post_multitenant_links, [:source_id, :dest_id],
        name: "non_multitenant_post_multitenant_links_unique_link_index"
      )
    )
  end

  def down do
    drop_if_exists(
      unique_index(:non_multitenant_post_multitenant_links, [:source_id, :dest_id],
        name: "non_multitenant_post_multitenant_links_unique_link_index"
      )
    )

    drop_if_exists(index(:non_multitenant_post_multitenant_links, [:dest_id]))

    drop_if_exists(index(:non_multitenant_post_multitenant_links, [:source_id]))

    drop(
      constraint(
        :non_multitenant_post_multitenant_links,
        "non_multitenant_post_multitenant_links_source_id_fkey"
      )
    )

    drop(
      constraint(
        :non_multitenant_post_multitenant_links,
        "non_multitenant_post_multitenant_links_dest_id_fkey"
      )
    )

    drop(table(:non_multitenant_post_multitenant_links, prefix: prefix()))
  end
end
