diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2016-10-21 09:08:38 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-10-21 09:08:38 -0700 |
commit | 0aed0bbb2956e3607a1a15d260a0833d4d3a7aa0 (patch) | |
tree | b397dd02111aaf30d6e042a66a20ad0d27669500 /activerecord/lib | |
parent | 4393406a8ac0d3c616210a96bca81dd6a12bf881 (diff) | |
parent | a60a20bb7faae4d0758676b865f625275c500e4e (diff) | |
download | rails-0aed0bbb2956e3607a1a15d260a0833d4d3a7aa0.tar.gz rails-0aed0bbb2956e3607a1a15d260a0833d4d3a7aa0.tar.bz2 rails-0aed0bbb2956e3607a1a15d260a0833d4d3a7aa0.zip |
Merge pull request #26050 from bogdanvlviv/optimistic_locking
Fixed: Optimistic locking does not work correctly
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/locking/optimistic.rb | 20 |
1 files changed, 12 insertions, 8 deletions
diff --git a/activerecord/lib/active_record/locking/optimistic.rb b/activerecord/lib/active_record/locking/optimistic.rb index 8e8a97990a..d39b7181f4 100644 --- a/activerecord/lib/active_record/locking/optimistic.rb +++ b/activerecord/lib/active_record/locking/optimistic.rb @@ -60,6 +60,7 @@ module ActiveRecord end private + def increment_lock lock_col = self.class.locking_column previous_lock_value = send(lock_col).to_i @@ -77,21 +78,24 @@ module ActiveRecord def _update_record(attribute_names = self.attribute_names) #:nodoc: return super unless locking_enabled? - return 0 if attribute_names.empty? lock_col = self.class.locking_column - previous_lock_value = send(lock_col).to_i - increment_lock - attribute_names += [lock_col] - attribute_names.uniq! + return super if attribute_names.include?(lock_col) + return 0 if attribute_names.empty? begin + previous_lock_value = read_attribute_before_type_cast(lock_col) + + increment_lock + + attribute_names.push(lock_col) + relation = self.class.unscoped affected_rows = relation.where( self.class.primary_key => id, - lock_col => previous_lock_value, + lock_col => previous_lock_value ).update_all( attributes_for_update(attribute_names).map do |name| [name, _read_attribute(name)] @@ -104,9 +108,9 @@ module ActiveRecord affected_rows - # If something went wrong, revert the version. + # If something went wrong, revert the locking_column value. rescue Exception - send(lock_col + "=", previous_lock_value) + send(lock_col + "=", previous_lock_value.to_i) raise end end |