Skip to content

Commit dd93e8c

Browse files
committed
improvement: add backwards compatibility for data layers returning only index metadata
1 parent dbc9d9f commit dd93e8c

File tree

6 files changed

+202
-95
lines changed

6 files changed

+202
-95
lines changed

lib/ash/actions/create/bulk.ex

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ defmodule Ash.Actions.Create.Bulk do
669669
:bulk_create
670670
)
671671

672-
changesets_by_index = index_changesets(batch)
672+
{changesets_by_ref, changesets_by_index} = index_changesets(batch)
673673

674674
run_batch(
675675
resource,
@@ -683,6 +683,7 @@ defmodule Ash.Actions.Create.Bulk do
683683
ref,
684684
attrs_to_require,
685685
action_select,
686+
changesets_by_ref,
686687
changesets_by_index
687688
)
688689
|> run_after_action_hooks(opts, domain, ref)
@@ -691,6 +692,7 @@ defmodule Ash.Actions.Create.Bulk do
691692
all_changes,
692693
opts,
693694
ref,
695+
changesets_by_ref,
694696
changesets_by_index,
695697
batch,
696698
domain,
@@ -720,7 +722,7 @@ defmodule Ash.Actions.Create.Bulk do
720722
argument_names
721723
) do
722724
base
723-
|> Ash.Changeset.put_context(:bulk_create, %{index: index})
725+
|> Ash.Changeset.put_context(:bulk_create, %{index: index, ref: make_ref()})
724726
|> Ash.Changeset.set_private_arguments_for_action(opts[:private_arguments] || %{})
725727
|> handle_params(
726728
Keyword.get(opts, :assume_casted?, false),
@@ -868,12 +870,14 @@ defmodule Ash.Actions.Create.Bulk do
868870
end
869871

870872
defp index_changesets(batch) do
871-
Enum.reduce(batch, %{}, fn changeset, changesets_by_index ->
872-
Map.put(
873-
changesets_by_index,
874-
changeset.context.bulk_create.index,
875-
changeset
876-
)
873+
Enum.reduce(batch, {%{}, %{}}, fn changeset, {by_ref, by_index} ->
874+
ref = changeset.context.bulk_create.ref
875+
index = changeset.context.bulk_create.index
876+
877+
{
878+
Map.put(by_ref, ref, changeset),
879+
Map.put(by_index, index, ref)
880+
}
877881
end)
878882
end
879883

@@ -1031,6 +1035,7 @@ defmodule Ash.Actions.Create.Bulk do
10311035
ref,
10321036
attrs_to_require,
10331037
action_select,
1038+
changesets_by_ref,
10341039
changesets_by_index
10351040
) do
10361041
batch
@@ -1092,7 +1097,7 @@ defmodule Ash.Actions.Create.Bulk do
10921097
end)
10931098
|> case do
10941099
[] ->
1095-
{[], changesets_by_index}
1100+
{[], changesets_by_ref, changesets_by_index}
10961101

10971102
batch ->
10981103
upsert_keys =
@@ -1122,7 +1127,7 @@ defmodule Ash.Actions.Create.Bulk do
11221127
end
11231128
end
11241129

1125-
changesets_by_index = index_changesets(batch)
1130+
{changesets_by_ref, changesets_by_index} = index_changesets(batch)
11261131

11271132
batch
11281133
|> Enum.group_by(&{&1.atomics, &1.filter})
@@ -1346,7 +1351,7 @@ defmodule Ash.Actions.Create.Bulk do
13461351
[]
13471352
end
13481353
end)
1349-
|> then(&{&1, changesets_by_index})
1354+
|> then(&{&1, changesets_by_ref, changesets_by_index})
13501355
end
13511356
end
13521357

@@ -1365,13 +1370,13 @@ defmodule Ash.Actions.Create.Bulk do
13651370
end
13661371

13671372
defp run_after_action_hooks(
1368-
{batch_results, changesets_by_index},
1373+
{batch_results, changesets_by_ref, changesets_by_index},
13691374
opts,
13701375
domain,
13711376
ref
13721377
) do
13731378
Enum.flat_map(batch_results, fn result ->
1374-
changeset = changesets_by_index[result.__metadata__.bulk_create_index]
1379+
changeset = changesets_by_ref[result.__metadata__[:bulk_action_ref]] || changesets_by_ref[changesets_by_index[result.__metadata__[:bulk_create_index]]]
13751380

13761381
case manage_relationships(result, domain, changeset,
13771382
upsert?: opts[:upsert?],
@@ -1413,6 +1418,7 @@ defmodule Ash.Actions.Create.Bulk do
14131418
all_changes,
14141419
opts,
14151420
ref,
1421+
changesets_by_ref,
14161422
changesets_by_index,
14171423
changesets,
14181424
domain,
@@ -1422,7 +1428,7 @@ defmodule Ash.Actions.Create.Bulk do
14221428
) do
14231429
results =
14241430
Enum.flat_map(batch, fn result ->
1425-
changeset = changesets_by_index[result.__metadata__.bulk_create_index]
1431+
changeset = changesets_by_ref[result.__metadata__[:bulk_action_ref]] || changesets_by_ref[changesets_by_index[result.__metadata__[:bulk_create_index]]]
14261432

14271433
if opts[:notify?] || opts[:return_notifications?] do
14281434
store_notification(ref, notification(changeset, result, opts), opts)
@@ -1455,12 +1461,14 @@ defmodule Ash.Actions.Create.Bulk do
14551461
|> Ash.Actions.Update.Bulk.run_bulk_after_changes(
14561462
all_changes,
14571463
results,
1464+
changesets_by_ref,
14581465
changesets_by_index,
14591466
changesets,
14601467
opts,
14611468
ref,
14621469
resource,
1463-
:bulk_create_index
1470+
:bulk_create_index,
1471+
:bulk_action_ref
14641472
)
14651473
|> then(fn records ->
14661474
if opts[:return_records?] do

lib/ash/actions/destroy/bulk.ex

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1472,7 +1472,8 @@ defmodule Ash.Actions.Destroy.Bulk do
14721472

14731473
[
14741474
Ash.Resource.set_metadata(result, %{
1475-
bulk_destroy_index: changeset.context.bulk_destroy.index
1475+
bulk_destroy_index: changeset.context.bulk_destroy.index,
1476+
bulk_action_ref: changeset.context.bulk_destroy.ref
14761477
})
14771478
]
14781479

@@ -1490,7 +1491,8 @@ defmodule Ash.Actions.Destroy.Bulk do
14901491

14911492
[
14921493
Ash.Resource.set_metadata(result, %{
1493-
bulk_destroy_index: changeset.context.bulk_destroy.index
1494+
bulk_destroy_index: changeset.context.bulk_destroy.index,
1495+
bulk_action_ref: changeset.context.bulk_destroy.ref
14941496
})
14951497
]
14961498

@@ -1628,7 +1630,7 @@ defmodule Ash.Actions.Destroy.Bulk do
16281630
:bulk_destroy
16291631
)
16301632

1631-
changesets_by_index = index_changesets(batch)
1633+
{changesets_by_ref, changesets_by_index} = index_changesets(batch)
16321634

16331635
run_batch(
16341636
resource,
@@ -1640,12 +1642,13 @@ defmodule Ash.Actions.Destroy.Bulk do
16401642
domain,
16411643
ref
16421644
)
1643-
|> run_after_action_hooks(opts, domain, ref, changesets_by_index)
1645+
|> run_after_action_hooks(opts, domain, ref, changesets_by_ref, changesets_by_index)
16441646
|> process_results(
16451647
changes,
16461648
all_changes,
16471649
opts,
16481650
ref,
1651+
changesets_by_ref,
16491652
changesets_by_index,
16501653
batch,
16511654
domain,
@@ -1678,7 +1681,7 @@ defmodule Ash.Actions.Destroy.Bulk do
16781681
|> Map.put(:domain, domain)
16791682
|> Ash.Changeset.prepare_changeset_for_action(action, opts)
16801683
|> Ash.Changeset.set_private_arguments_for_action(opts[:private_arguments] || %{})
1681-
|> Ash.Changeset.put_context(:bulk_destroy, %{index: index})
1684+
|> Ash.Changeset.put_context(:bulk_destroy, %{index: index, ref: make_ref()})
16821685
|> Ash.Changeset.set_context(opts[:context] || %{})
16831686
|> handle_params(
16841687
Keyword.get(opts, :assume_casted?, false),
@@ -1769,12 +1772,14 @@ defmodule Ash.Actions.Destroy.Bulk do
17691772
end
17701773

17711774
defp index_changesets(batch) do
1772-
Enum.reduce(batch, %{}, fn changeset, changesets_by_index ->
1773-
Map.put(
1774-
changesets_by_index,
1775-
changeset.context.bulk_destroy.index,
1776-
changeset
1777-
)
1775+
Enum.reduce(batch, {%{}, %{}}, fn changeset, {by_ref, by_index} ->
1776+
ref = changeset.context.bulk_destroy.ref
1777+
index = changeset.context.bulk_destroy.index
1778+
1779+
{
1780+
Map.put(by_ref, ref, changeset),
1781+
Map.put(by_index, index, ref)
1782+
}
17781783
end)
17791784
end
17801785

@@ -2072,6 +2077,10 @@ defmodule Ash.Actions.Destroy.Bulk do
20722077
:bulk_destroy_index,
20732078
changeset.context.bulk_destroy.index
20742079
)
2080+
|> Ash.Resource.put_metadata(
2081+
:bulk_action_ref,
2082+
changeset.context.bulk_destroy.ref
2083+
)
20752084
]}
20762085

20772086
{:error, error} ->
@@ -2116,10 +2125,11 @@ defmodule Ash.Actions.Destroy.Bulk do
21162125
opts,
21172126
domain,
21182127
ref,
2128+
changesets_by_ref,
21192129
changesets_by_index
21202130
) do
21212131
Enum.flat_map(batch_results, fn result ->
2122-
changeset = changesets_by_index[result.__metadata__.bulk_destroy_index]
2132+
changeset = changesets_by_ref[result.__metadata__[:bulk_action_ref]] || changesets_by_ref[changesets_by_index[result.__metadata__[:bulk_destroy_index]]]
21232133

21242134
case manage_relationships(result, domain, changeset,
21252135
actor: opts[:actor],
@@ -2160,6 +2170,7 @@ defmodule Ash.Actions.Destroy.Bulk do
21602170
all_changes,
21612171
opts,
21622172
ref,
2173+
changesets_by_ref,
21632174
changesets_by_index,
21642175
changesets,
21652176
domain,
@@ -2170,15 +2181,17 @@ defmodule Ash.Actions.Destroy.Bulk do
21702181
|> Ash.Actions.Update.Bulk.run_bulk_after_changes(
21712182
all_changes,
21722183
batch,
2184+
changesets_by_ref,
21732185
changesets_by_index,
21742186
changesets,
21752187
opts,
21762188
ref,
21772189
resource,
2178-
:bulk_destroy_index
2190+
:bulk_destroy_index,
2191+
:bulk_action_ref
21792192
)
21802193
|> Enum.flat_map(fn result ->
2181-
changeset = changesets_by_index[result.__metadata__[:bulk_destroy_index]]
2194+
changeset = changesets_by_ref[result.__metadata__[:bulk_action_ref]] || changesets_by_ref[changesets_by_index[result.__metadata__[:bulk_destroy_index]]]
21822195

21832196
if opts[:notify?] || opts[:return_notifications?] do
21842197
store_notification(ref, notification(changeset, result, opts), opts)

0 commit comments

Comments
 (0)