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

86 lines
2.3 KiB
Ruby
Raw Normal View History

2016-09-29 09:46:39 +05:30
# rubocop:disable Lint/RescueException
2018-11-08 19:23:39 +05:30
# Remove this monkey-patch when all lock_version values are converted from NULLs to zeros.
# See https://gitlab.com/gitlab-org/gitlab-ce/issues/25228
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
2016-09-29 09:46:39 +05:30
2018-11-18 11:00:15 +05:30
previous_lock_value = send(lock_col).to_i
2016-09-29 09:46:39 +05:30
2018-11-08 19:23:39 +05:30
# This line is added as a patch
previous_lock_value = nil if previous_lock_value == '0' || previous_lock_value == 0
2016-09-29 09:46:39 +05:30
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,
lock_col => previous_lock_value
).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`
if Gitlab.rails5?
class LockingType
def deserialize(value)
super
end
2017-08-17 22:00:37 +05:30
2018-11-08 19:23:39 +05:30
def serialize(value)
super
end
end
else
2018-05-09 12:01:36 +05:30
class LockingType < SimpleDelegator
def type_cast_from_database(value)
super
end
2017-08-17 22:00:37 +05:30
end
2016-09-29 09:46:39 +05:30
end
end
end