2016-08-01 15:34:17 +00:00
|
|
|
# rubocop:disable Lint/RescueException
|
|
|
|
|
2018-12-27 14:46:32 +00:00
|
|
|
# Remove this monkey patch when we move to Rails 5.1, because the bug has been fixed in https://github.com/rails/rails/pull/26050.
|
|
|
|
if Rails.gem_version >= Gem::Version.new("5.1")
|
|
|
|
raise "Remove this monkey patch: #{__FILE__}"
|
|
|
|
end
|
|
|
|
|
2018-06-15 11:53:39 +00:00
|
|
|
module ActiveRecord
|
|
|
|
module Locking
|
|
|
|
module Optimistic
|
|
|
|
# We overwrite this method because we don't want to have default value
|
|
|
|
# for newly created records
|
|
|
|
def _create_record(attribute_names = self.attribute_names, *) # :nodoc:
|
|
|
|
super
|
|
|
|
end
|
2016-08-01 15:34:17 +00:00
|
|
|
|
2018-06-15 11:53:39 +00:00
|
|
|
def _update_record(attribute_names = self.attribute_names) #:nodoc:
|
|
|
|
return super unless locking_enabled?
|
|
|
|
return 0 if attribute_names.empty?
|
2016-08-01 15:34:17 +00:00
|
|
|
|
2018-06-15 11:53:39 +00:00
|
|
|
lock_col = self.class.locking_column
|
2018-07-02 10:43:06 +00:00
|
|
|
previous_lock_value = send(lock_col).to_i
|
2018-06-15 11:53:39 +00:00
|
|
|
increment_lock
|
2016-08-01 15:34:17 +00:00
|
|
|
|
2018-06-15 11:53:39 +00:00
|
|
|
attribute_names += [lock_col]
|
|
|
|
attribute_names.uniq!
|
2016-08-01 15:34:17 +00:00
|
|
|
|
2018-06-15 11:53:39 +00:00
|
|
|
begin
|
|
|
|
relation = self.class.unscoped
|
2016-08-01 15:34:17 +00:00
|
|
|
|
2018-06-15 11:53:39 +00:00
|
|
|
affected_rows = relation.where(
|
|
|
|
self.class.primary_key => id,
|
2018-12-27 14:46:57 +00:00
|
|
|
# Patched because when `lock_version` is read as `0`, it may actually be `NULL` in the DB.
|
|
|
|
lock_col => previous_lock_value == 0 ? [nil, 0] : previous_lock_value
|
2018-06-15 11:53:39 +00:00
|
|
|
).update_all(
|
|
|
|
attributes_for_update(attribute_names).map do |name|
|
|
|
|
[name, _read_attribute(name)]
|
|
|
|
end.to_h
|
|
|
|
)
|
2016-08-01 15:34:17 +00:00
|
|
|
|
2018-06-15 11:53:39 +00:00
|
|
|
unless affected_rows == 1
|
|
|
|
raise ActiveRecord::StaleObjectError.new(self, "update")
|
|
|
|
end
|
2016-08-01 15:34:17 +00:00
|
|
|
|
2018-06-15 11:53:39 +00:00
|
|
|
affected_rows
|
2016-08-01 15:34:17 +00:00
|
|
|
|
2018-06-15 11:53:39 +00:00
|
|
|
# If something went wrong, revert the version.
|
|
|
|
rescue Exception
|
2018-07-02 10:43:06 +00:00
|
|
|
send(lock_col + '=', previous_lock_value)
|
2018-06-15 11:53:39 +00:00
|
|
|
raise
|
2016-08-01 15:34:17 +00:00
|
|
|
end
|
2018-06-15 11:53:39 +00:00
|
|
|
end
|
2016-12-01 08:17:20 +00:00
|
|
|
|
2018-06-15 11:53:39 +00:00
|
|
|
# This is patched because we need it to query `lock_version IS NULL`
|
|
|
|
# rather than `lock_version = 0` whenever lock_version is NULL.
|
|
|
|
def relation_for_destroy
|
|
|
|
return super unless locking_enabled?
|
2016-12-01 08:17:20 +00:00
|
|
|
|
2018-06-15 11:53:39 +00:00
|
|
|
column_name = self.class.locking_column
|
|
|
|
super.where(self.class.arel_table[column_name].eq(self[column_name]))
|
2016-12-01 08:17:20 +00:00
|
|
|
end
|
2018-06-15 11:53:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# This is patched because we want `lock_version` default to `NULL`
|
|
|
|
# rather than `0`
|
2018-12-07 18:15:06 +00:00
|
|
|
class LockingType
|
|
|
|
def deserialize(value)
|
|
|
|
super
|
2018-06-15 11:53:39 +00:00
|
|
|
end
|
2018-12-07 18:15:06 +00:00
|
|
|
|
|
|
|
def serialize(value)
|
|
|
|
super
|
2016-12-01 08:17:20 +00:00
|
|
|
end
|
2016-08-01 15:34:17 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|