diff options
author | Rémy Coutable <remy@rymai.me> | 2017-03-27 12:42:41 +0200 |
---|---|---|
committer | Rémy Coutable <remy@rymai.me> | 2017-03-28 10:52:13 +0200 |
commit | 8e388fca678d2373be4e364a4b00f007b921ef5a (patch) | |
tree | 1940acf50e6a8d7ef9b14ebd4f717d14ecf3c5af | |
parent | 20bada3382c21e50c496bf4d6bd4be3a4621b20d (diff) | |
download | gitlab-ce-8e388fca678d2373be4e364a4b00f007b921ef5a.tar.gz |
Get rid of a first N+1 query problem
Use the same approach in Banzai::Filter::MergeRequestReferenceFilter
as we do with issues.
Signed-off-by: Rémy Coutable <remy@rymai.me>
-rw-r--r-- | lib/banzai/filter/merge_request_reference_filter.rb | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/lib/banzai/filter/merge_request_reference_filter.rb b/lib/banzai/filter/merge_request_reference_filter.rb index ac5216d9cfb..1a24525e462 100644 --- a/lib/banzai/filter/merge_request_reference_filter.rb +++ b/lib/banzai/filter/merge_request_reference_filter.rb @@ -11,8 +11,8 @@ module Banzai MergeRequest end - def find_object(project, id) - project.merge_requests.find_by(iid: id) + def find_object(project, iid) + merge_requests_per_project[project][iid] end def url_for_object(mr, project) @@ -21,6 +21,25 @@ module Banzai only_path: context[:only_path]) end + # Returns a Hash containing the merge_requests per Project instance. + def merge_requests_per_project + @merge_requests_per_project ||= begin + hash = Hash.new { |h, k| h[k] = {} } + + projects_per_reference.each do |path, project| + merge_request_ids = references_per_project[path] + + merge_requests = project.merge_requests.where(iid: merge_request_ids.to_a) + + merge_requests.each do |merge_request| + hash[project][merge_request.iid.to_i] = merge_request + end + end + + hash + end + end + def object_link_text_extras(object, matches) extras = super |