Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix issues in Ash.Generator.generate_many/2 #1703

Merged
merged 5 commits into from
Jan 9, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions lib/ash/generator/generator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ defmodule Ash.Generator do
use Ash.Generator

# using `seed_generator`, bypasses the action and saves directly to the data layer
def blog_post(opts \\ []) do
def blog_post(opts \\\\ []) do
seed_generator(
%MyApp.Blog.Post{
name: sequence(:title, &"My Blog Post \#{&1}")
Expand All @@ -23,7 +23,7 @@ defmodule Ash.Generator do
end

# using `changeset_generator`, calls the action when passed to `generate`
def blog_post_comment(opts \\ []) do
def blog_post_comment(opts \\\\ []) do
blog_post_id = opts[:blog_post_id] || once(:default_blog_post_id, fn -> generate(blog_post()).id end)

changeset_generator(
Expand Down Expand Up @@ -505,12 +505,27 @@ defmodule Ash.Generator do
fn _changeset, record ->
{:ok, after_action.(record)}
end
else
# Do nothing
fn _changeset, record -> {:ok, record} end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll merge but I'm going to figure out how to make this not necessary, as adding hooks that aren't needed can prevent internal optimizations etc.

end

Ash.bulk_create!(Enum.map(batch, & &1.params), first.resource, first.action,
after_action: after_action,
return_records?: true
).records || []
result =
Ash.bulk_create!(Enum.map(batch, & &1.params), first.resource, first.action.name,
after_action: after_action,
return_records?: true,
return_errors?: true,
return_notifications?: true,
actor: first.context[:private][:actor]
)

if result.status != :success do
raise Ash.Error.to_error_class(result.errors)
end

Ash.Notifier.notify(result.notifications)

result.records || []

batch ->
Enum.map(batch, fn record ->
Expand Down Expand Up @@ -641,7 +656,7 @@ defmodule Ash.Generator do
Generate input meant to be passed into `Ash.Seed.seed!/2`.

A map of custom `StreamData` generators can be provided to add to or overwrite the generated input,
for example: `Ash.Generator.for_seed(Post, %{text: StreamData.constant("Post")})`
for example: `Ash.Generator.seed_input(Post, %{text: StreamData.constant("Post")})`
"""
@spec seed_input(Ash.Resource.t(), map()) :: StreamData.t(map())
def seed_input(resource, generators \\ %{}) do
Expand Down
Loading