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.

Where Next?

Popular in Questions Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
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
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I'm a nov...
New

Other popular topics Top

skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
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
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
nobody
Hi! In PHP: $SERVER['SERVERADDR'] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
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
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement