A fairly simple use-case: a user has one or more email addresses, stored in the database in 2 separate tables (users, emails) such that the emails table has a foreign key for user_id. (In reality, my use case is more complex, but this illustrates the problem).
Ecto.Multi lets us create a single transaction for adding both the user record and an email record, and we can rollback if either one of those operations fail. Iām looking at the docs for the run/3 and run/5 functions https://hexdocs.pm/ecto/Ecto.Multi.html#run/3 but I canāt figure out how these are supposed to be used. From the docs, Iām not at all clear on what value the run function is supposed to return or how that gets ingested by the next operation.
I know I could (sometimes) do this with a single insert operation by leveraging the relationships defined in the schema, but Iām more interested in the principle of the thing.
When you use Multi.run, you tag the operation using the second argument. For example, the |> Ecto.Multi.run(:user ... makes the result of create_user available under the :user key in the next the function.
create_user!(user_params) and create_email!(user, email_params) would need to return {:ok, resource} or {:error, reason}, so these are unlikely to be ābangā operations.
And the last |> Repo.insert() would need to be replaced with Repo.transaction().
@fireproofsocks note that Multi.runs are not automatically rolled back on errors, Iād probably use inserts instead. Modifying @abitdodgyās example:
Are you sure? A Multi is executed in a transaction and for as long as the command in a Multi.run does only cause side effects to the db and nothing else it should just be rolled back like any other db operation, which happened in that transaction. Iāve even some places in my projects, where Multis are effectively nested, as the inner multis are executed in named functions which a outer multi composes.
Precisely, DB actions are rolled back because of a transaction, but run/3,5 is often used to perform non-db actions that need to be run only if the DB succeeds, however there is not version of run/? that allows you to pass both a ādoā and an āundoā function, which would be a huge help personally. as right now you have to check it āafterā running Repo.transaction, thus meaning the actions of the multi have to āleakā out of where they are defined. We could really use a run/4;8 as well to support both a do and undoā¦
How would you ensure that the cleanup doesnāt fail? For the database part itās a guarantee of the database, but for other side effects ecto cannot do that. Ecto.Multi.run exist because before ecto 3 you could only pass a callback to Ecto.Multi.run and itās the only way to compose function which do their own database operations.
That would be on the onus of the one writing their own cleanup function. Some things are just not undoable of course, thus keep using the currently existing functions for those.
Thatās exactly why Ecto.Multi is not the tool to use imo. Youāre no longer orchestrating database operations within a transaction, but arbitrary operations. This is a different requirement, which should be handled by a more appropriate tool like sage.
Well in my case Iām orchestrating things that need to stay in sync between 2 different databases and an LDAP system, where if any of them fail I can and do need to roll them all back. Itās still a database transaction, just spread across 3 databases (one of which is not transactional, LDAP, so have to roll it back manually)⦠^.^;
What is this sage thing and most curiously how would it handle this?
@LostKobrakai Awesome, thanks for that! It seems the readme makes clear everything Iād need to do and how it works except for one big thing, the multiple database calls across the multiple databases do they run in a singular DB transaction each across them all or are they on a call-by-call basis only (I.E. no DB transactions across the entire Sage call)? I.E. does it start up an Ecto.Repo.transaction for all calls to the given database (thus those calls need to be handled inside that transaction) on the first call of a given repoās transaction orā¦)? How does it handle the transactions failing in any order and handling the rollback then?
Iāve never used it by myself, so Iām not sure how integrated it is with ecto. But maybe the possibility to arbitrarily nest transactions in ecto can be of aid for that.
That depends on the database itself, but thatās not the issue here, the issue is keeping two transactions active and in sync across two different databases, I.E. two different Repo modules.