Module: Sequel::Plugins::MssqlOptimisticLocking
- Defined in:
- lib/sequel/plugins/mssql_optimistic_locking.rb
Overview
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
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 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
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 :mssql_optimistic_locking, prevent_increase: true
This plugin relies on the instance_filters plugin.
Defined Under Namespace
Modules: InstanceMethods
Class Method Summary collapse
-
.apply(model, opts = OPTS) ⇒ Object
Load the instance_filters plugin into the model.
-
.configure(model, opts = OPTS) ⇒ Object
Set the lock column.
Class Method Details
.apply(model, opts = OPTS) ⇒ Object
Load the instance_filters plugin into the model.
35 36 37 |
# File 'lib/sequel/plugins/mssql_optimistic_locking.rb', line 35 def self.apply(model, opts=OPTS) model.plugin(:optimistic_locking_base) end |
.configure(model, opts = OPTS) ⇒ Object
Set the lock column
40 41 42 43 44 |
# File 'lib/sequel/plugins/mssql_optimistic_locking.rb', line 40 def self.configure(model, opts=OPTS) model.lock_column = opts[:lock_column] || model.lock_column || :timestamp # SEQUEL6: Make prevent_increase true by default model.send(:prevent_lock_column_increase!) if opts[:prevent_increase] end |