superfert.blogg.se

Ecto multi
Ecto multi












ecto multi
  1. #ECTO MULTI UPDATE#
  2. #ECTO MULTI CODE#

When a user signs up for our service, we need to create a new organization, create a new user, add the user to the organization, and send the user a confirmation email. In the following example, we have two entities in our service: organizations and users (each organization can have multiple users). But when there are multiple entities you need to create in your database when a user signs up, this can get complex. Most applications have some sort of signup form. This is what we will look at in our next example where we will also use Ecto.Multi. A more realistic example would be a user registration, where you need to create multiple entities and maybe even send an email. However, the famous Alice and Bob example is not something we encounter often in the real world. This means neither Bob nor Alice would send or receive money.

#ECTO MULTI UPDATE#

If any part of this transaction were to fail, we would receive an exception (because we’re using the update with !), and the transaction would roll back.

ecto multi

In our example database, we have stored how much money two of our users have.

ecto multi

To do this, we’ll continue with an example of transferring money. Let’s start with an overview of what database transactions are and why we need them. Today we will explore what database transactions are, what are they used for, and how you can use them nicely in Elixir with Ecto.Multi. Rather, it was created by an error - an error that could have been prevented with, as you might guess by the title, database transactions!

ecto multi

Wait… you don’t know about this issue? Well yes, it’s true that this doesn’t really happen in the real world, but with software, anything is possible.Īlthough it’s nice to have some extra money appear in your wallet, the truth is, it doesn’t really exist. This results in both of you having 10 dollars in your wallets. You have 10 dollars in your wallet, which you kindly give to them, but when you go to close your wallet, you notice you have 10 dollars in there, which you definitely already took out and gave to your friend. Imagine this: You’re out with a friend and they need some money because they haven’t had time to stop by the bank. Elixir is the freakin best.Let’s talk about transactions for a moment. I kept changing things around just to make sure I’m not missing anything, and nope. I changed the function around to break the tests, truly enough, the failed as intended. The tests passed from the first try… I was in disbelief.

#ECTO MULTI CODE#

The SolutionĮven being away from this code for a freakin year now, I wrote a simple solution that first enumerates the purchases, and filters out duplicates found in the DB. The rollback for Ecto.Multi happens at the end, so I couldn’t suppress this error, unfortunately. You see, if a DB error is thrown during a transaction, the whole transaction is marked as invalid, and you should rollback before you can start querying the db again. it was there, as clear as day… The function, which was part of an Ecto.Multi was handling a Repo.insert with a with statement that “suppressed” a certain DB error that detected duplicate purchase receipts… You can’t do that. After seeing the 20 line stacktrace, even that didn’t make sense, lol, but it at least confirmed my suspicion of which function the error was being thrown.Īfter looking deep and hard at the function, I was finally able to see it.














Ecto multi