dewetblomerus

dewetblomerus

Do not touch `updated_at` when upsert has no effect

This is not important, I’m just curious about how it would be done.

The updated_at field on my User gets bumped on every login, even if no fields changed. Auth0 already saves all the history about successful and unsuccessful logins, so I only want updated_at to be bumped when one of the fields for the User resource is being changed to a different value.

I followed this guide and then made some changes and eded up with the following action:

    create :register_with_auth0 do
      argument :user_info, :map, allow_nil?: false
      argument :oauth_tokens, :map, allow_nil?: false
      upsert? true
      upsert_identity :unique_auth0_id

      # Required if you have token generation enabled.
      change AshAuthentication.GenerateTokenChange

      # Required if you have the `identity_resource` configuration enabled.
      change AshAuthentication.Strategy.OAuth2.IdentityChange

      change fn changeset, _ ->
        user_info = Ash.Changeset.get_argument(changeset, :user_info)

        changes = %{
          "email" => Map.get(user_info, "email"),
          "auth0_id" => Map.get(user_info, "sub")
        }

        Ash.Changeset.change_attributes(
          changeset,
          changes
        )
      end
    end

Marked As Solved

zachdaniel

zachdaniel

Creator of Ash

:thinking: this may actually be something the framework should handle for you? Like if something has an update_default, it should only be applied if something is changing. We actually could solve for this in the update, i.e fields that are being set due to update_defaults would be set only if at least one other thing is changing…The difficulty is pairing that with atomics, i.e we don’t know if nothing is changing until we actually do the upsert. There are a few things we’d need to do to make this work, incrementally, that are on the roadmap that is in my brain :laughing:.

  1. add an expression variable that lets you refer to the thing you are replacing in an upsert.
  2. add an expression variable that corresponds to wether or not you are in an upsert.
  3. add an expression variable that corresponds to wether or not something is changing if you are in an upsert. # this one may or may not be reasonable

So then you could use an atomic_update to get this behavior in the short term, and then in the long term the framework can do this automatically. In a create, set the value, in an update, only set if something is changing.

change atomic_update(:updated_at, expr(
  if ash.updating? and ash.has_changes? do
    now()
  else
    updated_at
  end
))

But yeah none of that will happen in the short term. Someday :smiley:

Also Liked

jimsynz

jimsynz

Ash Core Team

Thanks for the confirmation. I believe this could be solved by #760. Keep an eye on that issue for updates.

zachdaniel

zachdaniel

Creator of Ash

I don’t recall what I was getting at :laughing: But I think what I was implying was try to call the action in iex (with the upsert_fields: {:replace_all_except, ...} option passed that was giving you trouble) as opposed to submitting a form.

dewetblomerus

dewetblomerus

Thanks a million for all the effort you have put into something that was merely an annoyance for me. The least I can do is open an issue. I’ll also try to provide a more minimal example for reproducing it. I don’t think Ash Authentication or Phoenix would need to be involved.

Last Post!

dewetblomerus

dewetblomerus

That settles it. It makes sense that people would want a mature framework to handle it for them. And it also makes sense that Ash has other more important steps to take before adding in these kinds of nice-to-haves. Thanks for your commitment to the community @zachdaniel

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
saveman71
Hello ! We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement