{@thread.name}

zachdaniel
2023-05-08

zachdaniel:

Optimistic locking is a pattern that exists in ecto that allows for simple concurrency safety without the complexity of locks/transactions, at the cost of some actions simply returning a StaleRecord error. It was pointed out by a user that we didn’t really have a good way of implementing that same pattern, so I’ve added it as a builtin change plus a new field on changeset (that likely won’t need to be used for any other reason) changeset.filters . Check out the docs for Ash.Resource.Change.Builtins.optimistic_lock/1 for more information. The basic usage to ensure that a given resource used optimistic locking for all update/destroy actions:

attributes do
 attribute :version, :integer do
   allow_nil? false
   default 1
 end
end

# in the global changes block
changes do
  # ensure that we check that the version matches before we update or destroy
  change optimistic_lock(:version)
  # increment the version on every change
  change increment(:version)
end