diff options
author | Sean McGivern <sean@mcgivern.me.uk> | 2018-08-07 10:38:46 +0000 |
---|---|---|
committer | Sean McGivern <sean@mcgivern.me.uk> | 2018-08-07 10:38:46 +0000 |
commit | 6b2b89f3cdb949b2001218b386bbc922166e4d4e (patch) | |
tree | 9908d9b0b03e0a4d142e2f3244e4fcd768bd77ff /app/models/merge_request.rb | |
parent | c8c38f94f28779167dbff6a0ba84a38427f705d4 (diff) | |
parent | 49dc8215b407f44eb02a4adea8cb100de5c55a7b (diff) | |
download | gitlab-ce-6b2b89f3cdb949b2001218b386bbc922166e4d4e.tar.gz |
Merge branch 'osw-fix-n-plus-1-for-mrs-without-merge-info' into 'master'
Avoid N+1 on MRs page when metrics merging date cannot be found
Closes #47613
See merge request gitlab-org/gitlab-ce!21053
Diffstat (limited to 'app/models/merge_request.rb')
-rw-r--r-- | app/models/merge_request.rb | 28 |
1 files changed, 17 insertions, 11 deletions
diff --git a/app/models/merge_request.rb b/app/models/merge_request.rb index acad8b91e9f..9b3e2d4446d 100644 --- a/app/models/merge_request.rb +++ b/app/models/merge_request.rb @@ -1089,23 +1089,29 @@ class MergeRequest < ActiveRecord::Base def can_be_reverted?(current_user) return false unless merge_commit + return false unless merged_at - merged_at = metrics&.merged_at - notes_association = notes_with_associations + # It is not guaranteed that Note#created_at will be strictly later than + # MergeRequestMetric#merged_at. Nanoseconds on MySQL may break this + # comparison, as will a HA environment if clocks are not *precisely* + # synchronized. Add a minute's leeway to compensate for both possibilities + cutoff = merged_at - 1.minute - if merged_at - # It is not guaranteed that Note#created_at will be strictly later than - # MergeRequestMetric#merged_at. Nanoseconds on MySQL may break this - # comparison, as will a HA environment if clocks are not *precisely* - # synchronized. Add a minute's leeway to compensate for both possibilities - cutoff = merged_at - 1.minute - - notes_association = notes_association.where('created_at >= ?', cutoff) - end + notes_association = notes_with_associations.where('created_at >= ?', cutoff) !merge_commit.has_been_reverted?(current_user, notes_association) end + def merged_at + strong_memoize(:merged_at) do + next unless merged? + + metrics&.merged_at || + merge_event&.created_at || + notes.system.reorder(nil).find_by(note: 'merged')&.created_at + end + end + def can_be_cherry_picked? merge_commit.present? end |