diff options
author | micael.bergeron <micael.bergeron@solutionstlm.com> | 2017-09-12 15:49:01 -0400 |
---|---|---|
committer | micael.bergeron <micael.bergeron@solutionstlm.com> | 2017-09-12 15:49:01 -0400 |
commit | b376e5c80e3dc6f30dfcc06f6a63209381414f89 (patch) | |
tree | 5c329738b841e887ff9489d9021313adf983e65e | |
parent | 53e632cbca5c9a304079721c26c140628c09e81c (diff) | |
download | gitlab-ce-b376e5c80e3dc6f30dfcc06f6a63209381414f89.tar.gz |
fix another N+1 query for label priorities34510-board-issues-sql-speedup
added a QueryRecorder for IssuesController#index.json
-rw-r--r-- | app/controllers/boards/issues_controller.rb | 4 | ||||
-rw-r--r-- | app/models/label.rb | 7 | ||||
-rw-r--r-- | spec/controllers/boards/issues_controller_spec.rb | 11 |
3 files changed, 19 insertions, 3 deletions
diff --git a/app/controllers/boards/issues_controller.rb b/app/controllers/boards/issues_controller.rb index 1c33f9a69d8..0d74078645a 100644 --- a/app/controllers/boards/issues_controller.rb +++ b/app/controllers/boards/issues_controller.rb @@ -12,10 +12,10 @@ module Boards issues = issues.page(params[:page]).per(params[:per] || 20) make_sure_position_is_set(issues) issues = issues.preload(:project, - :labels, :milestone, :assignees, - :notes => [:award_emoji, :author] + labels: [:priorities], + notes: [:award_emoji, :author] ) render json: { diff --git a/app/models/label.rb b/app/models/label.rb index 958141a7358..899028a01a0 100644 --- a/app/models/label.rb +++ b/app/models/label.rb @@ -127,7 +127,12 @@ class Label < ActiveRecord::Base end def priority(project) - priorities.find_by(project: project).try(:priority) + priority = if priorities.loaded? + priorities.first { |p| p.project == project } + else + priorities.find_by(project: project) + end + priority.try(:priority) end def template? diff --git a/spec/controllers/boards/issues_controller_spec.rb b/spec/controllers/boards/issues_controller_spec.rb index dfa06c78d46..5163099cd98 100644 --- a/spec/controllers/boards/issues_controller_spec.rb +++ b/spec/controllers/boards/issues_controller_spec.rb @@ -45,6 +45,17 @@ describe Boards::IssuesController do expect(parsed_response.length).to eq 2 expect(development.issues.map(&:relative_position)).not_to include(nil) end + + it 'avoids N+1 database queries' do + create(:labeled_issue, project: project, labels: [development]) + control_count = ActiveRecord::QueryRecorder.new { list_issues(user: user, board: board, list: list2) }.count + + # 25 issues is bigger than the page size + # the relative position will ignore the `#make_sure_position_set` queries + create_list(:labeled_issue, 25, project: project, labels: [development], assignees: [johndoe], relative_position: 1) + + expect { list_issues(user: user, board: board, list: list2) }.not_to exceed_query_limit(control_count) + end end context 'with invalid list id' do |