defmodule Helpcenter.Repo.TenantMigrations.RefactoredGroupPermissionsRelationshp 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
    alter table(:group_permissions, prefix: prefix()) do
      remove :permission_id
      add :action, :text, null: false
      add :resource, :text, null: false
    end

    drop_if_exists unique_index(:group_permissions, [:group_id, :permission_id],
                     name: "group_permissions_unique_name_index"
                   )

    create unique_index(:group_permissions, [:group_id, :resource, :action],
             name: "group_permissions_unique_name_index"
           )
  end

  def down do
    drop_if_exists unique_index(:group_permissions, [:group_id, :resource, :action],
                     name: "group_permissions_unique_name_index"
                   )

    create unique_index(:group_permissions, [:group_id, :permission_id],
             name: "group_permissions_unique_name_index"
           )

    alter table(:group_permissions, prefix: prefix()) do
      remove :resource
      remove :action

      add :permission_id,
          references(:permissions,
            column: :id,
            name: "group_permissions_permission_id_fkey",
            type: :uuid,
            prefix: "public"
          ),
          null: false
    end
  end
end
