Low commit activity in last 3 years
A long-lived project that still receives updates
gem that implement optimistic lock using unicity restriction from database
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 2.7
~> 1.10
~> 1.7
~> 10.0
~> 3.0
~> 0.47.1
~> 0.13.0
~> 1.0

Runtime

~> 4
 Project Readme

TP optimistic lock¶ ↑

This gem extends ActiveRecord to handle ActiveRecord::RecordNotUnique with an optimistic behavior. If a uniqueness violation is triggered by the database, it will be returned through the ‘errors` method.

The purpose is to be more performative and assertive than ‘validates_uniqueness_of`, a Rails pessimistic instruction for handling uniqueness.

‘validates_uniqueness_of` performs a select query for each new insert to check unicity. `validates_uniqueness_of` doesn’t work very well in an environment with high concurrency. The select and insert are atomic operations, so some attempts of insert the same record twice happens witha high frequency.

example: “‘

class Foo < ActiveRecord::Base
  acts_as_unique #
end

def sample
  Bar.create(uuid: '5ea880de-e4ce-4770-8d10-c89bac181e40', other: 'bla bla')
  bar = Bar.create(uuid: '5ea880de-e4ce-4770-8d10-c89bac181e40', other: 'bla bla')

  #error returned: UNIQUE constraint failed
  bar.errors
end

def other_sample
  Bar.create!(uuid: '5ea880de-e4ce-4770-8d10-c89bac181e40', other: 'bla bla')

  #error raised: ActiveRecord::RecordInvalid UNIQUE constraint failed
  Bar.create!(uuid: '5ea880de-e4ce-4770-8d10-c89bac181e40', other: 'bla bla')
end