princemaple

princemaple

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.

Showing Posts 1 to 4

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 OP

Yup, totally agree. A virtual field would be more appropriate.

It’s a low usage in-house project so I prioritized fun and experimenting with different solutions, instead of a simple and easy to understand one.

Over the years I had some good fun with update_change. Given that a field is never directly writable by a user, I have done things

  1. like this one, using it to avoid an extra delta and kinda auto sum.
  2. on password field I do update_change(:password, &Bcrypt.hash_pwd_salt/1), so I don’t have a virtual password and a materialized hashed_password

A lot of fun.

codeanpeace

codeanpeace

That’s a fun bug, thanks for sharing!

Out of curisoity, could the force_change trick open the door to the opposite problem? For example, 500 was already shipped and user updates an unrelated field or “messes” with :amount_shipped field without actually meaning to change it, then sees an unexpected doubling of :amount_shipped.

To add to the point about deltas made by @LostKobrakai, tracking each delta would also be useful for auditability down the line. Overwriting :amount_shipped means you won’t have the information necessary to retroactively support any feature requests to display amount shipped over time.

princemaple

princemaple OP

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.

— All posts loaded —

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 94592 917
New
cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
heathen
Quite interesting article Google brought me. Didn’t find any mentions about it here. What do you think in general? Would you use togethe...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
axelson
Hi there! :wave: @frigidcode and I (but mostly him) have been running an Elixir Book club, we’re almost done with Designing Elixir Syste...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
georgeguimaraes
Just published claude-code-elixir, a plugin marketplace for Claude Code with Elixir support. These are the plugins I’ve been using for my...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews