Module: Sequel::Plugins::OptimisticLocking

Defined in:
lib/sequel/plugins/optimistic_locking.rb

Overview

This plugin implements a simple database-independent locking mechanism to ensure that concurrent updates do not override changes. This is best implemented by a code example:

class Person < Sequel::Model
  plugin :optimistic_locking
end
p1 = Person[1]
p2 = Person[1]
p1.update(name: 'Jim') # works
p2.update(name: 'Bob') # raises Sequel::NoExistingObject

In order for this plugin to work, you need to make sure that the database table has a lock_version column that defaults to 0. To change the column used, provide a :lock_column option when loading the plugin:

plugin :optimistic_locking, lock_column: :version

This plugin relies on the instance_filters plugin.

Defined Under Namespace

Modules: InstanceMethods

Constant Summary collapse

Error =

Exception class raised when trying to update or destroy a stale object.

Sequel::NoExistingObject

Class Method Summary collapse

Class Method Details

.apply(model, opts = OPTS) ⇒ Object



28
29
30
# File 'lib/sequel/plugins/optimistic_locking.rb', line 28

def self.apply(model, opts=OPTS)
  model.plugin(:optimistic_locking_base)
end

.configure(model, opts = OPTS) ⇒ Object

Set the lock column



33
34
35
# File 'lib/sequel/plugins/optimistic_locking.rb', line 33

def self.configure(model, opts=OPTS)
  model.lock_column = opts[:lock_column] || model.lock_column || :lock_version
end