Don't break when building unions on empty collections

This commit is contained in:
Bob Van Landuyt 2017-09-07 11:46:58 +02:00
parent 438a0773dc
commit 530cf2a266
2 changed files with 12 additions and 1 deletions

View File

@ -26,7 +26,11 @@ module Gitlab
@relations.map { |rel| rel.reorder(nil).to_sql }.reject(&:blank?)
end
fragments.join("\n#{union_keyword}\n")
if fragments.any?
fragments.join("\n#{union_keyword}\n")
else
'NULL'
end
end
def union_keyword

View File

@ -29,5 +29,12 @@ describe Gitlab::SQL::Union do
expect(union.to_sql).to include('UNION ALL')
end
it 'returns `NULL` if all relations are empty' do
empty_relation = User.none
union = described_class.new([empty_relation, empty_relation])
expect(union.to_sql).to eq('NULL')
end
end
end