module Sequel::Plugins::MssqlOptimisticLocking

  1. lib/sequel/plugins/mssql_optimistic_locking.rb

This plugin implements optimistic locking mechanism on Microsoft SQL Server using a timestamp/rowversion column to ensure that concurrent updates are detected and previous changes are not automatically overridden. This is best implemented by a code example:

class Person < Sequel::Model
  plugin :mssql_optimistic_locking
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 column of timestamp or rowversion. The plugin uses a default name of timestamp for this columns, but you can override that using the :lock_column option:

plugin :mssql_optimistic_locking, lock_column: :column_name

This plugin relies on the instance_filters plugin.


Public Class

  1. apply
  2. configure

Public Class methods

apply(model, opts=OPTS)

Load the instance_filters plugin into the model.

[show source]
   # File lib/sequel/plugins/mssql_optimistic_locking.rb
28 def self.apply(model, opts=OPTS)
29   model.plugin(:optimistic_locking_base)
30 end
configure(model, opts=OPTS)

Set the lock column

[show source]
   # File lib/sequel/plugins/mssql_optimistic_locking.rb
33 def self.configure(model, opts=OPTS)
34   model.lock_column = opts[:lock_column] || model.lock_column || :timestamp
35 end