1
votes

For a given Ecto transaction, how to improve it to automatically add SQL inserts to log all changed values from executed changesets ? Here are unsuccessful tries:

Using Ecto.Multi:

  • Ecto.Multi.to_list is not a solution since the multi can contain Ecto.Multi.run operations (these operations are needed in case of dependencies between operations: for example when creating an account with its first user, we need to inject the account id in the user changeset).

  • Adding a final Ecto.Multi.run that is responsible to automatically inject audit events is not possible too since the changes_so_far parameter are the results from the previous operations and not the changesets.

Using Repo.transaction() with a anonymous function does not return any changeset.

1

1 Answers

0
votes

From https://groups.google.com/forum/#!topic/elixir-ecto/5M6VA8fg364:

There's no such feature currently, and I'm not in favour of adding it - this can be already achieved today without any changes to the API. If I were to create a system doing this I would most probably wrap the Ecto.Multi functions in my own that would add the additional tracking - for example:

defmodule MyApp.Multi do 

  def insert(multi, name, changeset) do 
    multi 
    |> Ecto.Multi.insert(name, changeset) 
    |> Ecto.Multi.insert({:log, name}, build_log(changeset)) 
  end 
end 

This will allow you to keep the nice API of Ecto.Multi, while allowing you to do additional operations easily.