| 1 | module ActiveRecord |
|---|
| 2 | module Locking |
|---|
| 3 | # Active Records support optimistic locking if the field <tt>lock_version</tt> is present. Each update to the |
|---|
| 4 | # record increments the lock_version column and the locking facilities ensure that records instantiated twice |
|---|
| 5 | # will let the last one saved raise a StaleObjectError if the first was also updated. Example: |
|---|
| 6 | # |
|---|
| 7 | # p1 = Person.find(1) |
|---|
| 8 | # p2 = Person.find(1) |
|---|
| 9 | # |
|---|
| 10 | # p1.first_name = "Michael" |
|---|
| 11 | # p1.save |
|---|
| 12 | # |
|---|
| 13 | # p2.first_name = "should fail" |
|---|
| 14 | # p2.save # Raises a ActiveRecord::StaleObjectError |
|---|
| 15 | # |
|---|
| 16 | # You're then responsible for dealing with the conflict by rescuing the exception and either rolling back, merging, |
|---|
| 17 | # or otherwise apply the business logic needed to resolve the conflict. |
|---|
| 18 | # |
|---|
| 19 | # You must ensure that your database schema defaults the lock_version column to 0. |
|---|
| 20 | # |
|---|
| 21 | # This behavior can be turned off by setting <tt>ActiveRecord::Base.lock_optimistically = false</tt>. |
|---|
| 22 | # To override the name of the lock_version column, invoke the <tt>set_locking_column</tt> method. |
|---|
| 23 | # This method uses the same syntax as <tt>set_table_name</tt> |
|---|
| 24 | module Optimistic |
|---|
| 25 | def self.included(base) #:nodoc: |
|---|
| 26 | super |
|---|
| 27 | base.extend ClassMethods |
|---|
| 28 | |
|---|
| 29 | base.cattr_accessor :lock_optimistically, :instance_writer => false |
|---|
| 30 | base.lock_optimistically = true |
|---|
| 31 | |
|---|
| 32 | base.alias_method_chain :update, :lock |
|---|
| 33 | base.alias_method_chain :attributes_from_column_definition, :lock |
|---|
| 34 | |
|---|
| 35 | class << base |
|---|
| 36 | alias_method :locking_column=, :set_locking_column |
|---|
| 37 | end |
|---|
| 38 | end |
|---|
| 39 | |
|---|
| 40 | def locking_enabled? #:nodoc: |
|---|
| 41 | lock_optimistically && respond_to?(self.class.locking_column) |
|---|
| 42 | end |
|---|
| 43 | |
|---|
| 44 | def attributes_from_column_definition_with_lock |
|---|
| 45 | result = attributes_from_column_definition_without_lock |
|---|
| 46 | |
|---|
| 47 | # If the locking column has no default value set, |
|---|
| 48 | # start the lock version at zero. Note we can't use |
|---|
| 49 | # locking_enabled? at this point as @attributes may |
|---|
| 50 | # not have been initialized yet |
|---|
| 51 | |
|---|
| 52 | if lock_optimistically && result.include?(self.class.locking_column) |
|---|
| 53 | result[self.class.locking_column] ||= 0 |
|---|
| 54 | end |
|---|
| 55 | |
|---|
| 56 | return result |
|---|
| 57 | end |
|---|
| 58 | |
|---|
| 59 | def update_with_lock #:nodoc: |
|---|
| 60 | return update_without_lock unless locking_enabled? |
|---|
| 61 | |
|---|
| 62 | lock_col = self.class.locking_column |
|---|
| 63 | previous_value = send(lock_col) |
|---|
| 64 | send(lock_col + '=', previous_value + 1) |
|---|
| 65 | |
|---|
| 66 | affected_rows = connection.update(<<-end_sql, "#{self.class.name} Update with optimistic locking") |
|---|
| 67 | UPDATE #{self.class.table_name} |
|---|
| 68 | SET #{quoted_comma_pair_list(connection, attributes_with_quotes(false))} |
|---|
| 69 | WHERE #{self.class.primary_key} = #{quote_value(id)} |
|---|
| 70 | AND #{self.class.quoted_locking_column} = #{quote_value(previous_value)} |
|---|
| 71 | end_sql |
|---|
| 72 | |
|---|
| 73 | unless affected_rows == 1 |
|---|
| 74 | raise ActiveRecord::StaleObjectError, "Attempted to update a stale object" |
|---|
| 75 | end |
|---|
| 76 | |
|---|
| 77 | return true |
|---|
| 78 | end |
|---|
| 79 | |
|---|
| 80 | module ClassMethods |
|---|
| 81 | DEFAULT_LOCKING_COLUMN = 'lock_version' |
|---|
| 82 | |
|---|
| 83 | # Set the column to use for optimistic locking. Defaults to lock_version. |
|---|
| 84 | def set_locking_column(value = nil, &block) |
|---|
| 85 | define_attr_method :locking_column, value, &block |
|---|
| 86 | value |
|---|
| 87 | end |
|---|
| 88 | |
|---|
| 89 | # The version column used for optimistic locking. Defaults to lock_version. |
|---|
| 90 | def locking_column |
|---|
| 91 | reset_locking_column |
|---|
| 92 | end |
|---|
| 93 | |
|---|
| 94 | # Quote the column name used for optimistic locking. |
|---|
| 95 | def quoted_locking_column |
|---|
| 96 | connection.quote_column_name(locking_column) |
|---|
| 97 | end |
|---|
| 98 | |
|---|
| 99 | # Reset the column used for optimistic locking back to the lock_version default. |
|---|
| 100 | def reset_locking_column |
|---|
| 101 | set_locking_column DEFAULT_LOCKING_COLUMN |
|---|
| 102 | end |
|---|
| 103 | end |
|---|
| 104 | end |
|---|
| 105 | end |
|---|
| 106 | end |
|---|