# SPDX-FileCopyrightText: 2022 Alembic Pty Ltd
#
# SPDX-License-Identifier: MIT

defmodule Example.Repo.Migrations.FixMultitenancy 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(:mt_user_identities, "mt_user_identities_user_id_fkey"))

    alter table(:mt_user_identities) do
      modify(
        :user_id,
        references(:mt_user,
          column: :id,
          name: "mt_user_identities_user_id_fkey",
          type: :uuid,
          prefix: "public"
        )
      )
    end

    drop_if_exists(unique_index(:mt_user, [:username], name: "mt_user_username_index"))

    create unique_index(:mt_user, [:organisation_id, :username], name: "mt_user_username_index")
  end

  def down do
    drop_if_exists(
      unique_index(:mt_user, [:organisation_id, :username], name: "mt_user_username_index")
    )

    create unique_index(:mt_user, [:organisation_id, :username], name: "mt_user_username_index")

    drop(constraint(:mt_user_identities, "mt_user_identities_user_id_fkey"))

    alter table(:mt_user_identities) do
      modify(
        :user_id,
        references(:mt_user,
          column: :id,
          name: "mt_user_identities_user_id_fkey",
          type: :uuid,
          prefix: "public"
        )
      )
    end
  end
end
