defmodule WandererApp.Repo.Migrations.AddMapAclFkCheck 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
    drop constraint(:map_access_lists_v1, "map_access_lists_v1_access_list_id_fkey")

    drop constraint(:map_access_lists_v1, "map_access_lists_v1_map_id_fkey")

    alter table(:map_access_lists_v1) do
      modify :map_id,
             references(:maps_v1,
               column: :id,
               name: "map_access_lists_v1_map_id_fkey",
               type: :uuid,
               prefix: "public",
               on_delete: :delete_all
             )
    end

    drop constraint(:access_list_members_v1, "access_list_members_v1_access_list_id_fkey")

    alter table(:access_list_members_v1) do
      modify :access_list_id,
             references(:access_lists_v1,
               column: :id,
               name: "access_list_members_v1_access_list_id_fkey",
               type: :uuid,
               prefix: "public",
               on_delete: :delete_all
             )
    end

    execute(
      "ALTER TABLE access_list_members_v1 alter CONSTRAINT access_list_members_v1_access_list_id_fkey NOT DEFERRABLE"
    )

    execute(
      "ALTER TABLE map_access_lists_v1 alter CONSTRAINT map_access_lists_v1_map_id_fkey NOT DEFERRABLE"
    )

    alter table(:map_access_lists_v1) do
      modify :access_list_id,
             references(:access_lists_v1,
               column: :id,
               name: "map_access_lists_v1_access_list_id_fkey",
               type: :uuid,
               prefix: "public",
               on_delete: :delete_all
             )
    end

    execute(
      "ALTER TABLE map_access_lists_v1 alter CONSTRAINT map_access_lists_v1_access_list_id_fkey NOT DEFERRABLE"
    )
  end

  def down do
    drop constraint(:map_access_lists_v1, "map_access_lists_v1_access_list_id_fkey")

    alter table(:map_access_lists_v1) do
      modify :access_list_id,
             references(:access_lists_v1,
               column: :id,
               name: "map_access_lists_v1_access_list_id_fkey",
               type: :uuid,
               prefix: "public"
             )
    end

    drop constraint(:access_list_members_v1, "access_list_members_v1_access_list_id_fkey")

    alter table(:access_list_members_v1) do
      modify :access_list_id,
             references(:access_lists_v1,
               column: :id,
               name: "access_list_members_v1_access_list_id_fkey",
               type: :uuid,
               prefix: "public"
             )
    end

    drop constraint(:map_access_lists_v1, "map_access_lists_v1_map_id_fkey")

    alter table(:map_access_lists_v1) do
      modify :map_id,
             references(:maps_v1,
               column: :id,
               name: "map_access_lists_v1_map_id_fkey",
               type: :uuid,
               prefix: "public"
             )
    end
  end
end
