feat: add read_action option to bulk actions (#1088)

This commit is contained in:
Frank Dugan III 2024-05-01 15:26:33 -05:00 committed by GitHub
parent 65ad0c6039
commit 718adb4891
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 19 additions and 4 deletions

View file

@ -324,6 +324,10 @@ defmodule Ash do
def create_opts, do: @create_opts_schema def create_opts, do: @create_opts_schema
@shared_bulk_opts_schema [ @shared_bulk_opts_schema [
read_action: [
type: :atom,
doc: "The action to use when building the read query."
],
assume_casted?: [ assume_casted?: [
type: :boolean, type: :boolean,
default: false, default: false,

View file

@ -22,7 +22,7 @@ defmodule Ash.Actions.Update.Bulk do
query = query =
Ash.Query.for_read( Ash.Query.for_read(
query, query,
Ash.Resource.Info.primary_action!(query.resource, :read).name, get_read_action(query.resource, opts).name,
%{}, %{},
actor: opts[:actor], actor: opts[:actor],
tenant: opts[:tenant] tenant: opts[:tenant]
@ -785,6 +785,7 @@ defmodule Ash.Actions.Update.Bulk do
defp do_run(domain, stream, action, input, opts, metadata_key, context_key, not_atomic_reason) do defp do_run(domain, stream, action, input, opts, metadata_key, context_key, not_atomic_reason) do
resource = opts[:resource] resource = opts[:resource]
opts = Ash.Actions.Helpers.set_opts(opts, domain) opts = Ash.Actions.Helpers.set_opts(opts, domain)
read_action = get_read_action(resource, opts)
{_, opts} = {_, opts} =
Ash.Actions.Helpers.set_context_and_get_opts(domain, Ash.Changeset.new(resource), opts) Ash.Actions.Helpers.set_context_and_get_opts(domain, Ash.Changeset.new(resource), opts)
@ -797,7 +798,7 @@ defmodule Ash.Actions.Update.Bulk do
Enum.empty?(Ash.Resource.Info.primary_key(resource)) -> Enum.empty?(Ash.Resource.Info.primary_key(resource)) ->
{:not_atomic, "cannot atomically update a stream without a primary key"} {:not_atomic, "cannot atomically update a stream without a primary key"}
!Ash.Resource.Info.primary_action(resource, :read) -> !read_action ->
{:not_atomic, "cannot atomically update a stream without a primary read action"} {:not_atomic, "cannot atomically update a stream without a primary read action"}
Ash.DataLayer.data_layer_can?(resource, :update_query) -> Ash.DataLayer.data_layer_can?(resource, :update_query) ->
@ -812,7 +813,7 @@ defmodule Ash.Actions.Update.Bulk do
query = query =
resource resource
|> Ash.Query.new() |> Ash.Query.new()
|> Map.put(:action, Ash.Resource.Info.primary_action!(resource, :read)) |> Map.put(:action, read_action)
case Ash.Actions.Read.Stream.stream_strategy( case Ash.Actions.Read.Stream.stream_strategy(
query, query,
@ -899,7 +900,7 @@ defmodule Ash.Actions.Update.Bulk do
pkeys = [or: Enum.map(batch, &Map.take(&1, pkey))] pkeys = [or: Enum.map(batch, &Map.take(&1, pkey))]
resource resource
|> Ash.Query.for_read(Ash.Resource.Info.primary_action!(resource, :read).name, %{}, |> Ash.Query.for_read(get_read_action(resource, opts).name, %{},
actor: opts[:actor], actor: opts[:actor],
authorize?: false, authorize?: false,
context: atomic_changeset.context, context: atomic_changeset.context,
@ -2454,4 +2455,14 @@ defmodule Ash.Actions.Update.Bulk do
context context
) )
end end
defp get_read_action(resource, opts) do
case opts[:read_action] do
nil ->
Ash.Resource.Info.primary_action!(resource, :read)
action ->
Ash.Resource.Info.action(resource, action)
end
end
end end