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
Hello everyone, I know we had quite some threads (read through lots of them) about background job processing but it remains a hotly deba...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
thojanssens1
It would be nice to be able to define a redirect from one route to another from the router.ex file. E.g.: redirect "/", UserController, ...
New
arcanemachine
https://nitter.net/josevalim/status/1744395345872683471 https://twitter.com/josevalim/status/1744395345872683471
New
AstonJ
Are there any Elixir or Erlang libraries that help with this? I’ve been thinking how streaming services like twitch have exploded recentl...
New
sashaafm
Piggy backing a bit on @dvcrn topic BEAM optimization for functions with static return type?, I’ve been trying to understand in a deeper ...
New
tmbb
This is a post to discuss the new Phoenix LiveView functionality. From Chris’s talk, it appears that they generate all HTML on the serve...
342 18146 126
New
praveenperera
How We Replaced React with Phoenix By: Thought Bot
New
shishini
I think this twitter post and youtube video didn’t get as much attention as I hoped I am still new to Elixir, so can’t really judge ...
New
CharlesO
Erlang :list.nth simple, but 1 - based nth(1, [H|_]) -> H; nth(N, [_|T]) when N > 1 -> nth(N - 1, T). Elixir Enum.at … coo...
New

Other popular topics Top

lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
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
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
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
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New

We're in Beta

About us Mission Statement