Laetitia

Laetitia

Hi everybody,
I have 2 entities: parent and child.
The parent entity have a life cycle so it transits from one status to another a.k.a ‘open’ or ‘discard’.
When it transits (it is a transactionnal action with multi) any child can’t be created.
I check before create child that the parent is open but i need that check to be on the commit of the creation transaction because the parent can change while the child is beeing created. I don’t want the parent to be blocked because of children. The parent blocks the childs but the children don’t block the parent.
I thought on locking but it is not suited to my need because it solves concurrency and here the case is more a blocking in only one direction.
How can i solve this problem?
Do you think using prepare_change is suffisant and safe?
I hope i expressed clearly.
Thank you all

.

Showing Posts 1 to 5

dimitarvp

dimitarvp

If your parent record can change at any time then there really is not much you can do.

Not sure I understood you well but you can use Ecto.Multi and pair that with DB triggers that disallow insertion of children if the parent is “locked”. I had cases in my career when we had such requirements and usually limiting things in the DB itself is the way to go.

Laetitia

Laetitia OP

Hello,
thank for your response.
I already use Multi.

pair that with DB triggers that disallow insertion of children if the parent is “locked”

What do you mean?

Here is a snippet:

  def abandon_parent(%Parent{} = parent) do
    utc_now = DateTime.utc_now()

    Multi.new()
    |> Multi.run(:abandon_parent, fn _, _ ->
     abandon_parent()
    end)
    |> Multi.run(:other_actions, fn _, _ ->
      other_actions()
    end)
    |> Repo.transaction()
  end

  def create_child(%{} = attrs) do
    Multi.new()
    |> Multi.insert(:child, Child.create_changeset(%Child{}, attrs))
    |> Multi.run(:other_actions, fn _repo, %{child: child} ->
      other_actions()
    end)
    |> Repo.transaction()
  end

and the changeset of child has a validation that the parent is open, in a prepare_change functions.
I am just afraid the abandon_parent occurs while the create_child, just after the Multi.insert so the validation will not help.
I don’t want to block the abandon action of the parent.
The parent has one owner. But child may be created by anyone.

evadne

evadne

If you are really serious about this, then you can implement some database level change to forbid changes to the child while the parent is locked (maybe an exclusive lock on the parent, advisory locks would work well too) but that can seriously gimp your performance

Another way to do it would be to funnel all mutations to the child through the same thing that mutates the parent (persistent process, works well with one host and less well as you add more) - but you can handle all this in the app

Triggers might or might not work as you can have the transaction that changes the parent and the transaction that changes the child start concurrently. Assuming that this is Postgres, the result might be surprising

sasajuric

sasajuric

Author of Elixir In Action

This was my first thought. Specifying a for share lock while checking that the parent is open should guarantee consistent behaviour while allowing some concurrency (depending on whether parent is otherwise updated in the transaction). Explicit locking can be done in Ecto with lock.

Laetitia

Laetitia OP

Thanks a lot!
Locking while checking the parent is open seems to be the solution.
I think i will go for optimistic_lock that will ensure the parent didn’t change its status while the create_child transaction occurs.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews