Module: Sequel::Plugins::PgXminOptimisticLocking

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

Overview

This plugin implements optimistic locking mechanism on PostgreSQL based on the xmin of the row. The xmin system column is automatically set to the current transaction id whenever the row is inserted or updated:

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

The advantage of pg_xmin_optimistic_locking plugin compared to the regular optimistic_locking plugin as that it does not require any additional columns setup on the model. This allows it to be loaded in the base model and have all subclasses automatically use optimistic locking. The disadvantage is that testing can be more difficult if you are modifying the underlying row between when a model is retrieved and when it is saved.

This plugin may not work with the class_table_inheritance plugin.

This plugin relies on the instance_filters plugin.

Defined Under Namespace

Modules: ClassMethods, InstanceMethods

Constant Summary collapse

WILDCARD =
LiteralString.new('*').freeze

Class Method Summary collapse

Class Method Details

.apply(model) ⇒ Object

Define the xmin column accessor



32
33
34
35
36
37
38
# File 'lib/sequel/plugins/pg_xmin_optimistic_locking.rb', line 32

def self.apply(model)
  model.instance_exec do
    plugin(:optimistic_locking_base)
    @lock_column = :xmin
    def_column_accessor(:xmin)
  end
end

.configure(model) ⇒ Object

Update the dataset to append the xmin column if it is usable and there is a dataset for the model.



42
43
44
45
46
# File 'lib/sequel/plugins/pg_xmin_optimistic_locking.rb', line 42

def self.configure(model)
  model.instance_exec do
    set_dataset(@dataset) if @dataset
  end
end