active_record_lock_issue_3664.rb 1.3 KB

12345678910111213141516171819202122232425262728293031323334
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module ActiveRecord::Locking::Pessimistic
  3. # https://github.com/zammad/zammad/issues/3664
  4. #
  5. # With Zammad 5.2 and Rails update from 6.1.6.2 to 6.1.7, internal database storage format
  6. # of preferences columns changed from serialized `ActiveSupport::HashWithIndifferentAccess` to just serialized `Hash``.
  7. # The downside is that 'old' values still have the old format, and show up as changed, which prevents
  8. # `with_lock` from working correctly - it would throw errors on previously modified records,
  9. # making tickets/users non-updateable.
  10. # We work around this by suppressing the exception in just this case.
  11. if !method_defined?(:orig_lock!)
  12. alias orig_lock! lock!
  13. def lock!(lock = true) # rubocop:disable Style/OptionalBooleanParameter
  14. if persisted? && has_changes_to_save?
  15. # We will skip the exception in case if the changes only contain columns which are store-type and have idential value.
  16. skip_exception = changes.all? do |key, value|
  17. send(key.to_sym).instance_of?(ActiveSupport::HashWithIndifferentAccess) && Marshal.dump(value[0]) == Marshal.dump(value[1])
  18. end
  19. if skip_exception
  20. reload(lock: lock)
  21. return self
  22. end
  23. end
  24. orig_lock!(lock)
  25. end
  26. end
  27. end