From d769596aec7daa2ff43f86c8fad4211fbc4f607d Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Thu, 12 Nov 2015 16:16:49 +0100 Subject: Added Gitlab::SQL::Union class This class can be used to join multiple AcitveRecord::Relation objects together using a SQL UNION statement. ActiveRecord < 5.0 sadly doesn't support UNION and existing Gems out there don't handle prepared statements (e.g. they never incremented the variable bindings). --- spec/lib/gitlab/sql/union_spec.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 spec/lib/gitlab/sql/union_spec.rb (limited to 'spec/lib') diff --git a/spec/lib/gitlab/sql/union_spec.rb b/spec/lib/gitlab/sql/union_spec.rb new file mode 100644 index 00000000000..976360af9b5 --- /dev/null +++ b/spec/lib/gitlab/sql/union_spec.rb @@ -0,0 +1,16 @@ +require 'spec_helper' + +describe Gitlab::SQL::Union do + describe '#to_sql' do + it 'returns a String joining relations together using a UNION' do + rel1 = User.where(email: 'alice@example.com') + rel2 = User.where(email: 'bob@example.com') + union = described_class.new([rel1, rel2]) + + sql1 = rel1.reorder(nil).to_sql + sql2 = rel2.reorder(nil).to_sql + + expect(union.to_sql).to eq("(#{sql1}) UNION (#{sql2})") + end + end +end -- cgit v1.2.1