debian-mirror-gitlab/config/initializers/active_record_locking.rb

77 lines
2.3 KiB
Ruby
Raw Normal View History

2016-09-29 09:46:39 +05:30
# rubocop:disable Lint/RescueException
2019-02-15 15:39:39 +05:30
# 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-11-08 19:23:39 +05:30
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-09-29 09:46:39 +05:30
2018-11-08 19:23:39 +05:30
def _update_record(attribute_names = self.attribute_names) #:nodoc:
return super unless locking_enabled?
return 0 if attribute_names.empty?
2016-09-29 09:46:39 +05:30
2018-11-08 19:23:39 +05:30
lock_col = self.class.locking_column
2018-11-18 11:00:15 +05:30
previous_lock_value = send(lock_col).to_i
2018-11-08 19:23:39 +05:30
increment_lock
2016-09-29 09:46:39 +05:30
2018-11-08 19:23:39 +05:30
attribute_names += [lock_col]
attribute_names.uniq!
2016-09-29 09:46:39 +05:30
2018-11-08 19:23:39 +05:30
begin
relation = self.class.unscoped
2016-09-29 09:46:39 +05:30
2018-11-08 19:23:39 +05:30
affected_rows = relation.where(
self.class.primary_key => id,
2019-02-15 15:39:39 +05:30
# 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-11-08 19:23:39 +05:30
).update_all(
attributes_for_update(attribute_names).map do |name|
[name, _read_attribute(name)]
end.to_h
)
2016-09-29 09:46:39 +05:30
2018-11-08 19:23:39 +05:30
unless affected_rows == 1
raise ActiveRecord::StaleObjectError.new(self, "update")
end
2016-09-29 09:46:39 +05:30
2018-11-08 19:23:39 +05:30
affected_rows
2016-09-29 09:46:39 +05:30
2018-11-08 19:23:39 +05:30
# If something went wrong, revert the version.
rescue Exception
2018-11-18 11:00:15 +05:30
send(lock_col + '=', previous_lock_value)
2018-11-08 19:23:39 +05:30
raise
2016-09-29 09:46:39 +05:30
end
2018-11-08 19:23:39 +05:30
end
2017-08-17 22:00:37 +05:30
2018-11-08 19:23:39 +05:30
# 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?
2017-08-17 22:00:37 +05:30
2018-11-08 19:23:39 +05:30
column_name = self.class.locking_column
super.where(self.class.arel_table[column_name].eq(self[column_name]))
2017-08-17 22:00:37 +05:30
end
2018-11-08 19:23:39 +05:30
end
# This is patched because we want `lock_version` default to `NULL`
# rather than `0`
2019-02-15 15:39:39 +05:30
class LockingType
def deserialize(value)
super
2018-11-08 19:23:39 +05:30
end
2019-02-15 15:39:39 +05:30
def serialize(value)
super
2017-08-17 22:00:37 +05:30
end
2016-09-29 09:46:39 +05:30
end
end
end