nyakio
How to update inserted_at timestamps?
I have a list of records that are to be sorted by the inserted_at column, and I would like to test the sorting functionality. In Ecto, I would create a list of the records and use Repo.update to update the inserted_at column with a calculated value so that the records have different timestamps.
I was able to work this out when I made the inserted_at attributed writable and passed it as an attribute to the update action
I would like to have the inserted_at field as a private attribute so that users cannot update it via the form, but this approach does not return updated values.
In my resource I have:
attribute :inserted_at, :utc_datetime_usec do
writable? true
default &DateTime.utc_now/0
match_other_defaults? true
allow_nil? false
end
...
update :update do
argument :inserted_at, :utc_datetime_usec
accept [:email, :language, :creator_id, :tenant_id, :inserted_at]
primary? true
end
And In the test I have:
for invitation <- invitations, i <- 1..length(invitations) do
invitation
|> Ash.Changeset.for_update(
:update,
%{},
private_arguments: %{inserted_at: time_before_or_after(-i * 12000)},
authorize?: false
)
|> Ash.update!()
end
Most Liked
barnabasJ
Hi
You still need to do something with the argument. That’s probably not precisely clear just by reading the Privat Inputs section of the docs. But an argument by itself is not doing anything.
update :update do
argument :inserted_at, :utc_datetime_usec
accept [:email, :language, :creator_id, :tenant_id, :inserted_at]
primary? true
change fn changeset, _context ->
case Ash.Changeset.get_argument(changeset, :inserted_at) do
nil -> changeset
datetime -> Ash.Changeset.change_atttrribute(changeset, :inserted_at, datetime)
end
end
end
barnabasJ
I would probably go with the context route described in the next section of the Docs.
For example, if you use AshGraphql, the argument would appear in the API. You could set it to public?: false to hide it from that layer though. But then you don’t have a good way to supply it yourself if needed.
But if you go the context route, you could supply values to AshGraphl in a plug or middleware that would be passed on to the actions.
Popular in Discussions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








