AE
Ash Elixirโ€ข2w ago
Euen

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
ZachDaniel
ZachDanielโ€ข2w ago
๐Ÿค” 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?
Euen
EuenOPโ€ข2w ago
hmm... it might be.. I tryied also spawning a functions and allowing it to the same connection but the same result. Do you think the locking will be effective adding lock: :for_update in both Ash.get calls? (line 8 and line 52)
ZachDaniel
ZachDanielโ€ข2w ago
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.
Euen
EuenOPโ€ข2w ago
Great, thanks! I'll try that ๐Ÿ‘

Did you find this page helpful?