Skip to content

Keep lock_version when updating partitioned records

Marius Bobin requested to merge 423054-fix-locking-queries into master

What does this MR do and why?

Optimistic locking is overriding the query_constraints method and our patch was overriding it instead of overriding the original method and the locking_version attribute was lost during the update:

        def _query_constraints_hash
          return super unless locking_enabled?

          locking_column = self.class.locking_column
          super.merge(locking_column => _lock_value_for_database(locking_column))
        end

Before

[6] pry(main)> Ci::Build.new.method(:_query_constraints_hash).source_location
=> ["/Users/marius/Workspace/gitlab-development-kit/gitlab/gems/activerecord-gitlab/lib/active_record/gitlab_patches/partitioning/base.rb", 13]
[7] pry(main)> Ci::Build.new.method(:_query_constraints_hash).super_method.source_location
=> ["/Users/marius/.gem/ruby/3.1.4/gems/activerecord-7.0.8/lib/active_record/locking/optimistic.rb", 147]
[8] pry(main)> Ci::Build.new.method(:_query_constraints_hash).super_method.super_method.source_location
=> ["/Users/marius/.gem/ruby/3.1.4/gems/activerecord-7.0.8/lib/active_record/persistence.rb", 1033]
[9] pry(main)> Ci::Build.new.method(:_query_constraints_hash).super_method.super_method.super_method
=> nil

After

[1] pry(main)> Ci::Build.new.method(:_query_constraints_hash).source_location
=> ["/Users/marius/.gem/ruby/3.1.4/gems/activerecord-7.0.8/lib/active_record/locking/optimistic.rb", 147]
[2] pry(main)> Ci::Build.new.method(:_query_constraints_hash).super_method.source_location
=> ["/Users/marius/Workspace/gitlab-development-kit/gitlab/gems/activerecord-gitlab/lib/active_record/gitlab_patches/partitioning/base.rb", 11]
[3] pry(main)> Ci::Build.new.method(:_query_constraints_hash).super_method.super_method.source_location
=> ["/Users/marius/.gem/ruby/3.1.4/gems/activerecord-7.0.8/lib/active_record/persistence.rb", 1033]

How to set up and validate locally

Numbered steps to set up and validate the change are strongly suggested.

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Related to #423054 (closed)

Edited by Marius Bobin

Merge request reports