Skip to content

Commit e0018e2

Browse files
committed
improvement: use bulk_metadata ref added to changesets
1 parent ce6da74 commit e0018e2

File tree

4 files changed

+85
-46
lines changed

4 files changed

+85
-46
lines changed

lib/ash/actions/bulk_manual_action_helpers.ex

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,21 +87,29 @@ defmodule Ash.Actions.BulkManualActionHelpers do
8787
case result do
8888
{:ok, record} ->
8989
record =
90-
Ash.Resource.put_metadata(
91-
record,
90+
record
91+
|> Ash.Resource.put_metadata(
9292
metadata_index_name,
9393
changeset.context[bulk_action_type].index
9494
)
95+
|> Ash.Resource.put_metadata(
96+
:bulk_action_ref,
97+
changeset.context[bulk_action_type].ref
98+
)
9599

96100
{:ok, record}
97101

98102
{:ok, record, notifications} ->
99103
record =
100-
Ash.Resource.put_metadata(
101-
record,
104+
record
105+
|> Ash.Resource.put_metadata(
102106
metadata_index_name,
103107
changeset.context[bulk_action_type].index
104108
)
109+
|> Ash.Resource.put_metadata(
110+
:bulk_action_ref,
111+
changeset.context[bulk_action_type].ref
112+
)
105113

106114
{:ok, record, notifications}
107115

lib/ash/actions/update/bulk.ex

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,11 @@ defmodule Ash.Actions.Update.Bulk do
660660
case results do
661661
[result] ->
662662
if atomic_changeset.context[:data_layer][:use_atomic_update_data?] do
663-
Map.put(result, :__metadata__, atomic_changeset.data.__metadata__)
663+
Map.put(
664+
result,
665+
:__metadata__,
666+
Map.merge(atomic_changeset.data.__metadata__, result.__metadata__)
667+
)
664668
else
665669
[result]
666670
end
@@ -2594,7 +2598,6 @@ defmodule Ash.Actions.Update.Bulk do
25942598
ref,
25952599
opts
25962600
)
2597-
|> add_metadata_to_results(batch, metadata_key, ref_metadata_key, context_key)
25982601

25992602
_ ->
26002603
Enum.reduce_while(
@@ -2730,8 +2733,16 @@ defmodule Ash.Actions.Update.Bulk do
27302733
store_error(ref, error, opts)
27312734
[]
27322735

2733-
{:ok, result, _changeset, %{notifications: more_new_notifications}} ->
2736+
{:ok, result_after_action, _changeset, %{notifications: more_new_notifications}} ->
27342737
store_notification(ref, more_new_notifications, opts)
2738+
2739+
result =
2740+
Map.put(
2741+
result_after_action,
2742+
:__metadata__,
2743+
Map.merge(result_after_action.__metadata__, result.__metadata__)
2744+
)
2745+
27352746
[result]
27362747
end
27372748

@@ -3547,23 +3558,6 @@ defmodule Ash.Actions.Update.Bulk do
35473558
find_changeset_by_ref(result, changesets_by_ref, metadata_key)
35483559
end
35493560

3550-
defp add_metadata_to_results({:ok, results}, batch, metadata_key, ref_metadata_key, context_key) do
3551-
results_with_metadata =
3552-
results
3553-
|> Enum.zip(batch)
3554-
|> Enum.map(fn {result, changeset} ->
3555-
result
3556-
|> Ash.Resource.put_metadata(metadata_key, changeset.context[context_key].index)
3557-
|> Ash.Resource.put_metadata(ref_metadata_key, changeset.context[context_key].ref)
3558-
end)
3559-
3560-
{:ok, results_with_metadata}
3561-
end
3562-
3563-
defp add_metadata_to_results(other, _batch, _metadata_key, _ref_metadata_key, _context_key) do
3564-
other
3565-
end
3566-
35673561
defp ensure_changeset!(nil, _result, _metadata_key, ref_metadata_key) do
35683562
raise "Missing ref metadata for record. The record should have had metadata key #{inspect(ref_metadata_key)} set during bulk update."
35693563
end
@@ -3576,15 +3570,11 @@ defmodule Ash.Actions.Update.Bulk do
35763570
if ref_metadata_key do
35773571
ref_key = result.__metadata__[ref_metadata_key]
35783572

3579-
if ref_key do
3580-
ref_key in List.wrap(changes)
3581-
else
3582-
# Fallback: find if this record ID has a changeset with ref in changes
3583-
Enum.any?(changesets_by_ref, fn {changeset_ref, changeset} ->
3584-
Map.get(changeset.data, :id) == result.id and
3585-
changeset_ref in List.wrap(changes)
3586-
end)
3587-
end
3573+
changesets_by_ref
3574+
|> Map.get(ref_key)
3575+
|> ensure_changeset!(result, metadata_key, ref_metadata_key)
3576+
3577+
ref_key in List.wrap(changes)
35883578
else
35893579
result.__metadata__[metadata_key] in List.wrap(changes)
35903580
end

lib/ash/data_layer/ets/ets.ex

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1874,8 +1874,25 @@ defmodule Ash.DataLayer.Ets do
18741874
|> case do
18751875
{:ok, results} ->
18761876
Enum.reduce_while(results, acc, fn result, acc ->
1877-
case update(query.resource, %{changeset | data: result}, nil, true) do
1877+
result_changeset = %{changeset | data: result}
1878+
1879+
case update(query.resource, result_changeset, nil, true) do
18781880
{:ok, result} ->
1881+
result =
1882+
if result_changeset.context[:bulk_update] do
1883+
result
1884+
|> Ash.Resource.put_metadata(
1885+
:bulk_update_index,
1886+
result_changeset.context.bulk_update.index
1887+
)
1888+
|> Ash.Resource.put_metadata(
1889+
:bulk_action_ref,
1890+
result_changeset.context.bulk_update.ref
1891+
)
1892+
else
1893+
result
1894+
end
1895+
18791896
case acc do
18801897
:ok ->
18811898
{:cont, :ok}

test/actions/bulk/bulk_update_manual_test.exs

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,29 @@ defmodule Ash.Test.Actions.BulkUpdateManualTest do
3636
|> case do
3737
{:ok, record} ->
3838
record =
39-
Ash.Resource.put_metadata(
40-
record,
39+
record
40+
|> Ash.Resource.put_metadata(
4141
:bulk_update_index,
4242
changeset.context.bulk_update.index
4343
)
44+
|> Ash.Resource.put_metadata(
45+
:bulk_action_ref,
46+
changeset.context.bulk_update.ref
47+
)
4448

4549
[{:ok, record} | results]
4650

4751
{:ok, record, notifications} ->
4852
record =
49-
Ash.Resource.put_metadata(
50-
record,
53+
record
54+
|> Ash.Resource.put_metadata(
5155
:bulk_update_index,
5256
changeset.context.bulk_update.index
5357
)
58+
|> Ash.Resource.put_metadata(
59+
:bulk_action_ref,
60+
changeset.context.bulk_update.ref
61+
)
5462

5563
[{:ok, record, notifications} | results]
5664

@@ -80,21 +88,29 @@ defmodule Ash.Test.Actions.BulkUpdateManualTest do
8088
|> case do
8189
{:ok, record} ->
8290
record =
83-
Ash.Resource.put_metadata(
84-
record,
91+
record
92+
|> Ash.Resource.put_metadata(
8593
:bulk_update_index,
8694
changeset.context.bulk_update.index
8795
)
96+
|> Ash.Resource.put_metadata(
97+
:bulk_action_ref,
98+
changeset.context.bulk_update.ref
99+
)
88100

89101
[{:ok, record} | results]
90102

91103
{:ok, record, notifications} ->
92104
record =
93-
Ash.Resource.put_metadata(
94-
record,
105+
record
106+
|> Ash.Resource.put_metadata(
95107
:bulk_update_index,
96108
changeset.context.bulk_update.index
97109
)
110+
|> Ash.Resource.put_metadata(
111+
:bulk_action_ref,
112+
changeset.context.bulk_update.ref
113+
)
98114

99115
[{:ok, record, %{notifications: notifications}} | results]
100116

@@ -124,21 +140,29 @@ defmodule Ash.Test.Actions.BulkUpdateManualTest do
124140
|> case do
125141
{:ok, record} ->
126142
record =
127-
Ash.Resource.put_metadata(
128-
record,
143+
record
144+
|> Ash.Resource.put_metadata(
129145
:bulk_update_index,
130146
changeset.context.bulk_update.index
131147
)
148+
|> Ash.Resource.put_metadata(
149+
:bulk_action_ref,
150+
changeset.context.bulk_update.ref
151+
)
132152

133153
[{:ok, record} | results]
134154

135155
{:ok, record, _notifications} ->
136156
record =
137-
Ash.Resource.put_metadata(
138-
record,
157+
record
158+
|> Ash.Resource.put_metadata(
139159
:bulk_update_index,
140160
changeset.context.bulk_update.index
141161
)
162+
|> Ash.Resource.put_metadata(
163+
:bulk_action_ref,
164+
changeset.context.bulk_update.ref
165+
)
142166

143167
[{:ok, record} | results]
144168

0 commit comments

Comments
 (0)