Commit b376e5c8 authored by micael.bergeron's avatar micael.bergeron

fix another N+1 query for label priorities

added a QueryRecorder for IssuesController#index.json
parent 53e632cb
......@@ -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: {
......
......@@ -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?
......
......@@ -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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment