Don't break when building unions on empty collections
This commit is contained in:
parent
438a0773dc
commit
530cf2a266
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue