apr

apr

Ecto Multi.run

Hello!

I had a question regarding the usage of Multi.run/3. I am using pg as my database. Let’s say I have a Multi with a couple different updates to my db. At the very end, I want to run a custom function that does not update the db (so no Repo.insert etc. It updates an ets table) using Multi.run/3.

My question is, does it make sense to use Multi.run/3 to run functions that do not update the db? I would like to do this because my custom function can return {:error, value} and all the preceding updates can be rolled back.

Thanks!

Marked As Solved

OvermindDL1

OvermindDL1

You use Multi.run for something not DB oriented but where you still want the DB to rollback if it fails, so yes, this is the use-case it is for. :slight_smile:

Just remember that an entire Multi pipeline is in a transaction, so the rows it accessed in the database are locked until it’s complete, so make sure it doesn’t take too long to run.

Also Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

@apr Ecto itself doesn’t “undo” the operations, rather it opens a transaction and then all operations within that transaction will be rolled back by the database unless the transaction is committed.

apr

apr

Gotcha! Thanks a lot @OvermindDL1!

Edit: I was also wondering, if you use a Multi.run to actually update the DB, does the update get rolled back when some other operation down the line fails? If so, how does Ecto figure out which Multi runs update the DB and which ones don’t (In order to rollback any updates on failure)?

medikent

medikent

In order for an operation in a Multi.run block to be included in the transaction must the run block use the Repo argument passed from the earlier Multi step or can it call a function that makes arbitrary Repo calls while not using the repo passed in from the Multi? This could be such as calling Repo.one, Repo.insert, or something like that

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New

Other popular topics Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement