Making DB calls with Ecto Multi `run` sections

Hi

With Ecto Multi, when making DB calls within a run, do you need to use the repo function argument or is that provided purely as a convience?

eg.

Multi.new()
|> Multi.run(:customer, fn _repo, %{connection: connection} ->
  FooBars.create_or_update_foobar(foobar_attrs)
end)
|> Repo.transaction()

Thanks

Josh

It’s a convenience for allowing those callbacks to be better abstractable in case you have many repos.

There are projects that have more than one repo. And in rare cases you might even use Ecto.Multi with the multiple repos in a single “transaction” (though that’s not trivial to pull off well because it’s not technically a single DB transaction as it can span multiple DBs).

1 Like

Thanks for the quick replies @LostKobrakai and @dimitarvp, and the explanations.