Skip to content

Commit 6ed97f5

Browse files
committed
bugfix: Re-add bulk_create_index metadata when running bulk creates
This is necessary when running bulk create actions with the `sorted?: true` option, and also retains consistency with other built-in data layers Related to ash-project/ash#2452
1 parent d3c8ca2 commit 6ed97f5

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

lib/data_layer.ex

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2142,8 +2142,12 @@ defmodule AshPostgres.DataLayer do
21422142
end
21432143

21442144
# Compatibility fallback
2145-
Ash.Resource.put_metadata(
2146-
result_for_changeset,
2145+
result_for_changeset
2146+
|> Ash.Resource.put_metadata(
2147+
:bulk_create_index,
2148+
changeset.context[:bulk_create][:index]
2149+
)
2150+
|> Ash.Resource.put_metadata(
21472151
:bulk_action_ref,
21482152
changeset.context[:bulk_create][:ref]
21492153
)
@@ -2159,8 +2163,12 @@ defmodule AshPostgres.DataLayer do
21592163
end
21602164

21612165
# Compatibility fallback
2162-
Ash.Resource.put_metadata(
2163-
result,
2166+
result
2167+
|> Ash.Resource.put_metadata(
2168+
:bulk_create_index,
2169+
changeset.context[:bulk_create][:index]
2170+
)
2171+
|> Ash.Resource.put_metadata(
21642172
:bulk_action_ref,
21652173
changeset.context[:bulk_create][:ref]
21662174
)

test/bulk_create_test.exs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,4 +579,21 @@ defmodule AshPostgres.BulkCreateTest do
579579
end)
580580
end
581581
end
582+
583+
describe "bulk_create with sorted?: true" do
584+
test "works without error" do
585+
# This test reproduces https://github.com/ash-project/ash/issues/2452
586+
# generate_many uses sorted?: true which requires bulk_create_index metadata
587+
results =
588+
Ash.bulk_create!(
589+
[%{title: "post_1"}, %{title: "post_2"}, %{title: "post_3"}],
590+
Post,
591+
:create,
592+
return_records?: true,
593+
sorted?: true
594+
)
595+
596+
assert length(results.records) == 3
597+
end
598+
end
582599
end

0 commit comments

Comments
 (0)