diff options
author | Sean McGivern <sean@gitlab.com> | 2019-05-06 20:24:23 +0000 |
---|---|---|
committer | Mayra Cabrera <mcabrera@gitlab.com> | 2019-05-06 20:24:23 +0000 |
commit | 835c616b12a8712634f5ddbfd8a5e70673a1a663 (patch) | |
tree | a5ac03e27de2d0c92060bc7c6f40b9dae8c0802f /config | |
parent | 1309e180fad744658ed8de9d912fb4eaf9998be8 (diff) | |
download | gitlab-ce-835c616b12a8712634f5ddbfd8a5e70673a1a663.tar.gz |
Fix editing issues and MRs with NULL lock_version
Diffstat (limited to 'config')
-rw-r--r-- | config/initializers/config_initializers_active_record_locking.rb | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/config/initializers/config_initializers_active_record_locking.rb b/config/initializers/config_initializers_active_record_locking.rb new file mode 100644 index 00000000000..1c4352b135d --- /dev/null +++ b/config/initializers/config_initializers_active_record_locking.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true +# rubocop:disable Lint/RescueException +module ActiveRecord + module Locking + module Optimistic + private + + def _update_row(attribute_names, attempted_action = "update") + return super unless locking_enabled? + + begin + locking_column = self.class.locking_column + previous_lock_value = read_attribute_before_type_cast(locking_column) + attribute_names << locking_column + + self[locking_column] += 1 + + # Patched because when `lock_version` is read as `0`, it may actually be `NULL` in the DB. + possible_previous_lock_value = previous_lock_value == 0 ? [nil, 0] : previous_lock_value + + affected_rows = self.class.unscoped._update_record( + arel_attributes_with_values(attribute_names), + self.class.primary_key => id_in_database, + locking_column => possible_previous_lock_value + ) + + if affected_rows != 1 + raise ActiveRecord::StaleObjectError.new(self, attempted_action) + end + + affected_rows + + # If something went wrong, revert the locking_column value. + rescue Exception + self[locking_column] = previous_lock_value.to_i + raise + end + end + end + end +end |