module Sequel::Plugins::OptimisticLocking

  1. lib/sequel/plugins/optimistic_locking.rb

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

By default, the plugin allows using the lock column setter method to either increase or decrease the lock column value. To prevent increases, which could potentially be used to skip an update when it update should be made, you can use the :prevent_increase plugin option:

plugin :optimistic_locking, prevent_increase: true

This plugin relies on the instance_filters plugin.

Methods

Public Class

  1. apply
  2. configure

Constants

Error = Sequel::NoExistingObject  

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

Public Class methods

apply(model, opts=OPTS)
[show source]
   # File lib/sequel/plugins/optimistic_locking.rb
35 def self.apply(model, opts=OPTS)
36   model.plugin(:optimistic_locking_base)
37 end
configure(model, opts=OPTS)

Set the lock column

[show source]
   # File lib/sequel/plugins/optimistic_locking.rb
40 def self.configure(model, opts=OPTS)
41   model.lock_column = opts[:lock_column] || model.lock_column || :lock_version
42   # SEQUEL6: Make prevent_increase true by default
43   model.send(:prevent_lock_column_increase!) if opts[:prevent_increase]
44 end