Commit 020701fa authored by Jan Provaznik's avatar Jan Provaznik

Use 'preload' in export to CSV

If issues are filtered by multiple labels, then GROUP BY statement
is used to filter only issues with these labels. This is problem in
combination with '.includes' which may use JOIN statement to load
associated resources in single query instead of a separate query.

Then the generated SQL query fails because associated resources
are included in 'SELECT ...' statements but not in the GROUP BY
statement.

Usage of '.preload' instead of '.includes' ensures that Active Record
uses a separate query instead of JOIN.

Closes #4376
parent 0a395a08
---
title: Fix export to CSV if a filter with multiple labels is used
merge_request:
author:
type: fixed
......@@ -21,7 +21,7 @@ module Issues
def csv_builder
@csv_builder ||=
CsvBuilder.new(@issues.includes(:author, :assignees, :timelogs), header_to_value_hash)
CsvBuilder.new(@issues.preload(:author, :assignees, :timelogs), header_to_value_hash)
end
private
......
......@@ -127,6 +127,20 @@ describe Issues::ExportCsvService do
expect(csv[0]['Time Spent']).to eq '560'
expect(csv[1]['Time Spent']).to eq '0'
end
context 'with issues filtered by labels and project' do
let(:subject) do
described_class.new(
IssuesFinder.new(user,
project_id: project.id,
label_name: %w(Idea Feature)).execute)
end
it 'returns only filtered objects' do
expect(csv.count).to eq(1)
expect(csv[0]['Issue ID']).to eq issue.iid.to_s
end
end
end
context 'with minimal details' 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