ash-project / ash_paper_trail

The extension for keeping an audit log of changes to your Ash resources.
https://hexdocs.pm/ash_paper_trail
MIT License
32 stars 15 forks source link

Error with missing `changed?` in context with `bulk_destroy` #58

Closed nallwhy closed 5 months ago

nallwhy commented 5 months ago

Describe the bug

Ash.bulk_destroy!(records, :destroy, %{},
  strategy: [:stream, :atomic, :atomic_batches],
  return_errors?: true
)

** (KeyError) key :changed? not found in: %{private: %{authorize?: true}, bulk_destroy: %{index: 0}}

crashes on https://github.com/nallwhy/ash_paper_trail/blob/cc5f2c4385f9120ef570b552da505dfd7df941fa/lib/resource/changes/create_new_version.ex#L21

ash_archive uses bulk_destroy.

https://github.com/ash-project/ash_archival/blob/d0763dc10f99d5db8567341fe3e68acdfcbf4a63/lib/ash_archival/resource/changes/archive_related.ex#L52

To Reproduce

https://github.com/nallwhy/ash_paper_trail/commit/cc5f2c4385f9120ef570b552da505dfd7df941fa

Expected behavior

no error

** Runtime

Additional context

zachdaniel commented 5 months ago

Can you try with ash main? I think I've fixed it there.

nallwhy commented 5 months ago

It still has the problem with ash main.

  1) test version resource a new version is created on bulk_destroy (AshPaperTrailTest)
     test/ash_paper_trail_test.exs:133
     ** (KeyError) key :changed? not found in: %{private: %{authorize?: true}, bulk_destroy: %{index: 0}}
     code: Ash.bulk_destroy!([post], :destroy, %{},
     stacktrace:
       (ash_paper_trail 0.1.2-rc.0) lib/resource/changes/create_new_version.ex:21: anonymous fn/2 in AshPaperTrail.Resource.Changes.CreateNewVersion.create_new_version/1
       (ash 3.0.0-rc.34) lib/ash/changeset/changeset.ex:3353: anonymous fn/2 in Ash.Changeset.run_after_actions/3
       (elixir 1.16.1) lib/enum.ex:4842: Enumerable.List.reduce/3
       (elixir 1.16.1) lib/enum.ex:2582: Enum.reduce_while/3
       (ash 3.0.0-rc.34) lib/ash/actions/destroy/bulk.ex:1686: anonymous fn/5 in Ash.Actions.Destroy.Bulk.run_after_action_hooks/5
       (elixir 1.16.1) lib/enum.ex:4329: Enum.flat_map_list/2
       (ash 3.0.0-rc.34) lib/ash/actions/destroy/bulk.ex:1154: Ash.Actions.Destroy.Bulk.do_handle_batch/8
       (ash 3.0.0-rc.34) lib/ash/actions/destroy/bulk.ex:805: anonymous fn/10 in Ash.Actions.Destroy.Bulk.do_stream_batches/5
       (elixir 1.16.1) lib/stream.ex:613: anonymous fn/4 in Stream.map/2
       (elixir 1.16.1) lib/stream.ex:1816: anonymous fn/3 in Enumerable.Stream.reduce/3
       (elixir 1.16.1) lib/stream.ex:273: anonymous fn/4 in Stream.chunk_while_fun/2
       (elixir 1.16.1) lib/stream.ex:1159: anonymous fn/3 in Stream.with_index/2
       (elixir 1.16.1) lib/enum.ex:4842: Enumerable.List.reduce/3
       (elixir 1.16.1) lib/stream.ex:1828: Enumerable.Stream.do_each/4
       (elixir 1.16.1) lib/stream.ex:690: Stream.run/1
       (ash 3.0.0-rc.34) lib/ash/actions/destroy/bulk.ex:825: Ash.Actions.Destroy.Bulk.run_batches/3
       (ash 3.0.0-rc.34) lib/ash/actions/destroy/bulk.ex:363: Ash.Actions.Destroy.Bulk.run/6
       (ash 3.0.0-rc.34) lib/ash.ex:2295: Ash.bulk_destroy!/4
       test/ash_paper_trail_test.exs:137: (test)
zachdaniel commented 5 months ago

Okay, I pushed your test, thanks! And the issue is fixed in ash main now.

nallwhy commented 5 months ago

Thank you!