Writing a Change with DB lock
I have a
change
that do some db queries in order to verify that the value being inserted in a foreign key is valid. Since I'm being based on other records to validate this field I need to lock those to prevent them to be modified in the middle of the change
.
Before adding any locking to my change
I decided to test this concurrent scenario so first I make it fail so then I could be certain the problem is solved once I lock the records.
My current issue is that the tests mean to run concurrently are not failing. No mater how many times I run the tests, the records are being updated in order as if a locking mechanism were in place
Here is the change I'm talking about: https://github.com/Euen/ash_sample/blob/main/lib/helpdesk/changes/enforce_single_level_parenting.ex
This is the test that I understand should fail but not: https://github.com/Euen/ash_sample/blob/main/test/helpdesk/changes/enforce_single_level_parenting_test.exs
And here is the issue I have created in Github to follow up this: https://github.com/Euen/ash_sample/issues/1
Thanks in advance ๐GitHub
Creating a Ash change with locking ยท Issue #1 ยท Euen/ash_sample
Final Objective Create an Ash change that allows running the EnforceSingleLevelParenting validation inside a database transaction to support concurrent updates. Current State I've created a ser...
4 Replies
๐ค Its hard to say. We definitely aren't doing locking on your behalf magically. What might be happening though is something in your tests serializing the queries, like when using the sandbox and having one connection or something along those lines?
I might try not doing it in a test, but in a
.exs
file
and then mix run your_test.exs
, so its running w/ your standard dev setup
and yes locking should be a viable strategy. You can also use transaction advisory locks to represent the whole operation.Great, thanks! I'll try that ๐