I don't know why Ash.bulk_destroy!/2 undefined bulk_destroy

Hi I would like to do a bulk_description as shown in the example bulk_destory as shown in the example.

However, I ran into the error below.

[error] 6efcbea5-5a0e-403c-9731-b17a2dccfbcc: Exception raised while resolving query.

** (UndefinedFunctionError) function Ash.bulk_destroy!/2 is undefined or private

Can you tell me what the problem is?

actions do

defaults [:read, :destroy]
...
action :bulk_destroy_user_noti, :boolean do
      argument :ids, {:array, :uuid}, allow_nil?: false

      run fn input, context ->
        IO.inspect(input.arguments.ids)

        __MODULE__
        |> Ash.Query.filter(id in ^input.arguments.ids)
        |> Ash.bulk_destroy!(:destroy)
        |> dbg

        {:ok, false}
      end
    end
end

The function in the documentation is bulk_destroy!/4 so it’s expecting up to 4 arguments and the last one is optional:

bulk_destroy!(stream_or_query, action, input, opts \\ [])

I think the documentation link in your post has a bug and it should be passing a 3rd parameter

Ticket
|> Ash.Query.filter(status == :open)
|> Ash.bulk_destroy!(:close, %{}) 

because the next example is

Ash.bulk_destroy!(one_hundred_tickets, :close, %{}, batch_size: 10)

Please try to change your code to

__MODULE__
        |> Ash.Query.filter(id in ^input.arguments.ids)
        |> Ash.bulk_destroy!(:destroy, %{})
1 Like

Yes, this is correct. An empty input is required even if the action takes no inputs.

action :bulk_destroy_user_notification, :boolean do

      argument :ids, {:array, :uuid}, allow_nil?: false

      run fn input, context ->
        ids = input.arguments.ids

        __MODULE__
        |> Ash.Query.filter(id in ^ids)
        |> Ash.bulk_destroy(:destroy, %{}, return_errors?: true)
        |> dbg

        {:ok, true}
      end
    end

Sorry for the late reply

Ash.bulk_destroy(:destroy, %{}, return_errors?: true, strategy: :atomic) #=> %Ash.BulkResult{
  status: :error,
  errors: [
    %Ash.Error.Invalid{
      errors: [
        %Ash.Error.Invalid.NoMatchingBulkStrategy{
          resource: Dentallog.Accounts.UserNotification,
          action: :destroy,
          requested_strategies: [:atomic],
          not_stream_reason: nil,
          not_atomic_batches_reason: "Not in requested strategies",
          not_atomic_reason: "AshArchival.Resource.Changes.ArchiveRelated does not implement `atomic/3`",
          footer: nil,
          splode: Ash.Error,
          bread_crumbs: [],
          vars: [],
          path: [],
          stacktrace: #Splode.Stacktrace<>,
          class: :invalid
        }
      ]
    }
  ],
  records: nil,
  notifications: nil,
  error_count: 1
}

The bulk_destory itself succeeded, but the

not_atomic_batches_reason: "Not in requested strategies",
          not_atomic_reason: "AshArchival.Resource.Changes.ArchiveRelated does not implement `atomic/3`",

I am getting the above error. It seems to be related to AshArchival.

I believe we updated AshArchival to support bulk atomic updates, but there were also some unreleased changes. Please upgrade ash_archival and give it a try.

{:ash, "~> 3.0.16"},
{:ash_postgres, "~> 2.0.12"},
{:ash_phoenix, "~> 2.0.4"},
{:ash_archival, git: "https://github.com/ash-project/ash_archival.git"},
{:ash_paper_trail, git: "https://github.com/ash-project/ash_paper_trail.git"},
{:ash_graphql, "~> 1.2.0"},
{:ash_authentication, "~> 4.0.1"},
{:ash_authentication_phoenix, "~> 2.0.0"},

I’m using that version, but is there a more recent version than this?

That doesn’t show me what version of ash_archival you are using. You’re pointing at “some commit”, but which commit is not specified. Do mix deps.update ash_archival (also, I’d suggest not using GitHub dependencies unless absolutely necessary, or for testing things)

I thought it was automatically reading the latest source, sorry.

defp deps do
    [
      {:ash, "~> 3.0.16"},
      {:ash_postgres, "~> 2.0.12"},
      {:ash_phoenix, "~> 2.0.4"},
      {:ash_archival, "~> 1.0.1"},
      {:ash_paper_trail, "~> 0.1.2"},
      {:ash_graphql, "~> 1.2.0"},
      {:ash_authentication, "~> 4.0.1"},
      {:ash_authentication_phoenix, "~> 2.0.0"},
      {:phoenix, "~> 1.7.12"},
      {:phoenix_ecto, "~> 4.5.1"},
      {:ecto_sql, "~> 3.11.1"},
      {:postgrex, ">= 0.17.5"},
      {:phoenix_html, "~> 4.0"},
      {:phoenix_live_reload, "~> 1.2", only: :dev},
      {:phoenix_live_view, "~> 0.20.1"},
      {:floki, ">= 0.30.0", only: :test},
      {:phoenix_live_dashboard, "~> 0.8.2"},
      {:esbuild, "~> 0.8", runtime: Mix.env() == :dev},
      {:tailwind, "~> 0.2.0", runtime: Mix.env() == :dev},
      {:swoosh, "~> 1.15.3"},
      {:finch, "~> 0.13"},
      {:telemetry_metrics, "~> 0.6"},
      {:telemetry_poller, "~> 1.0"},
      {:gettext, "~> 0.20"},
      {:jason, "~> 1.2"},
      {:dns_cluster, "~> 0.1.1"},
      {:plug_cowboy, "~> 2.5"},
      {:guardian, "~> 2.3"},
      {:guardian_db, "~> 3.0"},
      {:absinthe, "~> 1.7"},
      {:picosat_elixir, "~> 0.2"},
      {:appsignal, "~> 2.0"}
    ]
  end

I used that dependency.

|> Ash.bulk_destroy(:destroy, %{}, return_errors?: true) #=> %Ash.BulkResult{
  status: :error,
  errors: [
    %Ash.Error.Invalid{
      errors: [
        %Ash.Error.Invalid.NoMatchingBulkStrategy{
          resource: Dentallog.Accounts.UserNotification,
          action: :destroy,
          requested_strategies: [:atomic_batches, :atomic],
          not_stream_reason: nil,
          not_atomic_batches_reason: "AshPaperTrail.Resource.Changes.CreateNewVersion does not implement `atomic/3`",
          not_atomic_reason: nil,
          footer: nil,
          splode: Ash.Error,
          bread_crumbs: [],
          vars: [],
          path: [],
          stacktrace: #Splode.Stacktrace<>,
          class: :invalid
        }
      ]
    }
  ],
  records: nil,
  notifications: nil,
  error_count: 1
}

But I still facing that error.

Okay, you will need to be on main of ash and ash_paper_trail for this to work, but I’ve pushed an atomic/3 callback there. I was wrong, we hadn’t added it before.

2 Likes

It isn’t optimized for batches though so would you mind opening an issue on ash_paper_trail requesting to optimize the atomic/3 behaviour for batches using after_batch/3?

넵 알겠습니다. 이슈로 등록하였습니다.

항상 그렇지만 많은 도움을 주셔서 감사합니다.