Forgotten `change` in update action leads to hours of troubleshooting and sadness

I forgot a change in front of a set_attribute in an update action and nothing really reacted to that except that the attribute wasn't set. Is there a flag I can set somewhere to have Spark or Ash or something crash loudly at me when I use keywords in wrong places? Sad code:
update :set_family do
argument :family_id, :uuid, allow_nil?: true, public?: true
set_attribute(:family_id, arg(:family_id))
end
update :set_family do
argument :family_id, :uuid, allow_nil?: true, public?: true
set_attribute(:family_id, arg(:family_id))
end
Happy code:
update :set_family do
argument :family_id, :uuid, allow_nil?: true, public?: true
change set_attribute(:family_id, arg(:family_id))
end
update :set_family do
argument :family_id, :uuid, allow_nil?: true, public?: true
change set_attribute(:family_id, arg(:family_id))
end
10 Replies
ZachDaniel
ZachDaniel2mo ago
We've had this issue multiple times, and honestly its just a weirdly hard problem to solve 😢
Andreas Ekeroot @ work
Oh no. 🙁 Well at least I know I'm not alone in this.
ZachDaniel
ZachDaniel2mo ago
it is silly that it can happen, I just haven't found a good way to prevent it 😢
Meeq
Meeq2mo ago
Seems like something that a Credo check could spot and warn about
ZachDaniel
ZachDaniel2mo ago
Its possible, but not super easy arbitrary code can go in actions i.e
create :create do
your_own_macro()
end
create :create do
your_own_macro()
end
We could try and spot builtin changes without change
Andreas Ekeroot @ work
What if that macro is evaluated/normalized/demacrofied and then the check is run? Or is it still possible to have any Elixir you want in the action?
ZachDaniel
ZachDaniel2mo ago
I don't think it really works that way I don't know of like an ability to do a post-expension pass like that
Gonzalo Muñoz
Gonzalo Muñoz2mo ago
There could be an 80/20 solution maybe? Like setting up a whitelist of "must be there"s. For example, I don't think we can have a resources do block without a resource macro inside, right?
ZachDaniel
ZachDaniel2mo ago
Potentially yes I see no reason for folks not to build credo checks like that or to have an ash_credo package for example
Andreas Ekeroot @ work
I was hoping it was possible. Oh well.

Did you find this page helpful?