diff options
author | bogdanvlviv <bogdanvlviv@gmail.com> | 2016-08-04 03:36:30 +0300 |
---|---|---|
committer | bogdanvlviv <bogdanvlviv@gmail.com> | 2016-10-21 19:06:02 +0300 |
commit | 22a822e5813ef7ea9ab6dbbb670a363899a083af (patch) | |
tree | 9678d652295f9f1afac03f733e96221572b21288 /activerecord/lib/active_record/locking | |
parent | 4393406a8ac0d3c616210a96bca81dd6a12bf881 (diff) | |
download | rails-22a822e5813ef7ea9ab6dbbb670a363899a083af.tar.gz rails-22a822e5813ef7ea9ab6dbbb670a363899a083af.tar.bz2 rails-22a822e5813ef7ea9ab6dbbb670a363899a083af.zip |
Fixed: Optimistic locking does not work well with null in the database
Diffstat (limited to 'activerecord/lib/active_record/locking')
-rw-r--r-- | activerecord/lib/active_record/locking/optimistic.rb | 10 |
1 files changed, 6 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/locking/optimistic.rb b/activerecord/lib/active_record/locking/optimistic.rb index 8e8a97990a..fa9e789b24 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 @@ -80,7 +81,8 @@ module ActiveRecord return 0 if attribute_names.empty? lock_col = self.class.locking_column - previous_lock_value = send(lock_col).to_i + previous_lock_value = read_attribute_before_type_cast(lock_col) + increment_lock attribute_names += [lock_col] @@ -91,7 +93,7 @@ module ActiveRecord 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 +106,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 |