princemaple

princemaple

Tricks and regrets re:update_change

Just want to share the bug I caused and spent too long debugging.

I got this order item that has amount (required amount) and amount_shipped on it. You know, sometimes shipping isn’t done in a single batch.

Instead of writing a plain and simple context function, I thought it was a good idea to use a changeset with an update_change to streamline the process and the client only has to supply a new value to amount_shipped, thus avoiding a controller action, a context function and potentially a virtual field.

So I ended up with something like this:

  def changeset(order_item, %{amount_shipped: _} = attrs) do
    order_item
    |> cast(attrs, [:amount_shipped])
    |> update_change(:amount_shipped, &(&1 + order_item.amount_shipped))
  end

The atom key amount_shipped in pattern matching is just because this particular changeset clause is only used with processed data. Irrelevant here.

This approach worked relatively well, until some point where a weird bug was reported. Sometimes, the amount_shipped is not updated.

This took me way too long than it should have taken to figure out… The bug turned out to be simple, if 500 was already shipped and you ship another 500, this is not a “change” and does not trigger update_change.

I could have taken the chance to rewrite it back to plain and simple albeit more verbose solutions… :wink: but I ended up with this fix:

  def changeset(order_item, %{amount_shipped: amount_shipped} = attrs) do
    order_item
    |> cast(attrs, [:amount_shipped])
    # in case same amount
    |> force_change(:amount_shipped, amount_shipped)
    |> update_change(:amount_shipped, &(&1 + order_item.amount_shipped))
  end

Not promoting the trick or anything. Just sharing some head-scratching fun I had.

Most Liked

LostKobrakai

LostKobrakai

Tbh that sounds like the more appropriate solution to me. The customer is no longer submitting the schemas fields. They’re submitting deltas. The way to handle that would imo be validating their input based on a schema for the input. Then do the business logic of applying the deltas and use the result as the changes for the persisted data.

E.g. validating :amount_shipped to be >= 0 has completely different meaning applied to the delta send by the customer than it has to the resulting total on the schema. Trying to conflate the two will imo only be a source of confusion in the future.

A more viable shortcut could imo be an virtual fields for :amount_shipped_delta vs the :amount_shipped total.

princemaple

princemaple

Hi, glad you found it interesting.

To answer this, I have to come back to quote my own text in the post first:

  1. I have multi clause changeset functions, this clause is only dealing with :amount_shipped and has no chance confusing with other fields
  2. This clause has atom key pattern matching, which means it is only explicitly used in the program, and it cannot be called with user input directly

This is a very good point. I didn’t show everything here. I only showed order and order-items here, there are actually associated shipping and shipping-items. When a shipping is approved, it updates the items in its parent order with its shipping-items. History can be constructed by querying all the shippings relevant to an order, and details are in their items :slight_smile:

And you probably guessed by now, each order-item is updated with amount_shipped from the according shipping-item’s amount.

Where Next?

Popular in Discussions Top

PragTob
Hey everyone, this has been on my mind for some time and I’d love your input on it! TLDR: I feel like maps are superioer for storing and...
New
New
jesse
Hi everyone, I hesitated to post this here because I don’t want you to think I’m spamming, but I’ve been working on a Platform-as-a-Serv...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
Fl4m3Ph03n1x
Background A few days ago I was listening to The future of Elixir from Elixir Talks, with Dave Thomas (@pragdave ) and Brian Mitchell. I...
New
eteeselink
Hi all, In the last days, two things happened: A blog post titled “They might never tell you it’s broken” made the rounds. It’s about ...
New
RudManusachi
What configs will make sense to put to runtime.exs? – A bit of how I configure apps: I have generic configs in config/config.exs, dev...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36820 110
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44778 311
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
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42716 114
New

We're in Beta

About us Mission Statement