diff --git a/rails_5_1.gemfile b/rails_5_1.gemfile index 3a6d91f..a5e0f97 100644 --- a/rails_5_1.gemfile +++ b/rails_5_1.gemfile @@ -10,4 +10,4 @@ gem 'rails', '~> 5.1.0' # https://bitbucket.org/ged/ruby-pg/issues/270/pg-100-x64-mingw32-rails-server-not-start gem 'pg', '~> 0.21' -gem 'sqlite3', '~> 1.4.0' +gem 'sqlite3', '~> 1.3.6' diff --git a/rails_5_2.gemfile b/rails_5_2.gemfile index e6b73e3..bb43325 100644 --- a/rails_5_2.gemfile +++ b/rails_5_2.gemfile @@ -7,4 +7,4 @@ gem 'rails', '~> 5.2.0' gem 'pg' -gem 'sqlite3', '~> 1.4.0' +gem 'sqlite3', '~> 1.3.6' diff --git a/spec/union_spec.rb b/spec/union_spec.rb index 2d82cb8..f2020e1 100644 --- a/spec/union_spec.rb +++ b/spec/union_spec.rb @@ -105,9 +105,15 @@ describe ActiveRecord::Relation do context "in SQLite" do it "lets ORDER BY in query subselects throw a syntax error" do - expect(union.to_sql.squish).to eq( - "SELECT \"posts\".* FROM ( SELECT \"posts\".* FROM \"posts\" WHERE \"posts\".\"user_id\" = 1 ORDER BY \"posts\".\"created_at\" ASC UNION SELECT \"posts\".* FROM \"posts\" WHERE (created_at > '#{SQL_TIME}') ORDER BY \"posts\".\"created_at\" ASC ) \"posts\" ORDER BY \"posts\".\"created_at\" ASC" - ) + if ([ActiveRecord::VERSION::MAJOR, ActiveRecord::VERSION::MINOR] <=> [5, 2]) >= 0 + expect(union.to_sql.squish).to eq( + "SELECT \"posts\".* FROM ( SELECT \"posts\".* FROM \"posts\" WHERE \"posts\".\"user_id\" = 1 ORDER BY \"posts\".\"created_at\" ASC UNION SELECT \"posts\".* FROM \"posts\" WHERE (created_at > '2014-07-19 00:00:00') ORDER BY \"posts\".\"created_at\" ASC ) \"posts\" ORDER BY \"created_at\" ASC" + ) + else + expect(union.to_sql.squish).to eq( + "SELECT \"posts\".* FROM ( SELECT \"posts\".* FROM \"posts\" WHERE \"posts\".\"user_id\" = 1 ORDER BY \"posts\".\"created_at\" ASC UNION SELECT \"posts\".* FROM \"posts\" WHERE (created_at > '#{SQL_TIME}') ORDER BY \"posts\".\"created_at\" ASC ) \"posts\" ORDER BY \"posts\".\"created_at\" ASC" + ) + end expect{union.to_a}.to raise_error(ActiveRecord::StatementInvalid) end end @@ -115,9 +121,15 @@ describe ActiveRecord::Relation do context "in Postgres" do it "wraps query subselects in parentheses to allow ORDER BY clauses" do Databases.with_postgres do - expect(union.to_sql.squish).to eq( - "SELECT \"posts\".* FROM ( (SELECT \"posts\".* FROM \"posts\" WHERE \"posts\".\"user_id\" = 1 ORDER BY \"posts\".\"created_at\" ASC) UNION (SELECT \"posts\".* FROM \"posts\" WHERE (created_at > '#{SQL_TIME}') ORDER BY \"posts\".\"created_at\" ASC) ) \"posts\" ORDER BY \"posts\".\"created_at\" ASC" - ) + if ([ActiveRecord::VERSION::MAJOR, ActiveRecord::VERSION::MINOR] <=> [5, 2]) >= 0 + expect(union.to_sql.squish).to eq( + "SELECT \"posts\".* FROM ( (SELECT \"posts\".* FROM \"posts\" WHERE \"posts\".\"user_id\" = 1 ORDER BY \"posts\".\"created_at\" ASC) UNION (SELECT \"posts\".* FROM \"posts\" WHERE (created_at > '2014-07-19 00:00:00') ORDER BY \"posts\".\"created_at\" ASC) ) \"posts\" ORDER BY \"created_at\" ASC" + ) + else + expect(union.to_sql.squish).to eq( + "SELECT \"posts\".* FROM ( (SELECT \"posts\".* FROM \"posts\" WHERE \"posts\".\"user_id\" = 1 ORDER BY \"posts\".\"created_at\" ASC) UNION (SELECT \"posts\".* FROM \"posts\" WHERE (created_at > '#{SQL_TIME}') ORDER BY \"posts\".\"created_at\" ASC) ) \"posts\" ORDER BY \"posts\".\"created_at\" ASC" + ) + end expect{union.to_a}.to_not raise_error end end @@ -126,9 +138,15 @@ describe ActiveRecord::Relation do context "in MySQL" do it "wraps query subselects in parentheses to allow ORDER BY clauses" do Databases.with_mysql do - expect(union.to_sql.squish).to eq( - "SELECT `posts`.* FROM ( (SELECT `posts`.* FROM `posts` WHERE `posts`.`user_id` = 1 ORDER BY `posts`.`created_at` ASC) UNION (SELECT `posts`.* FROM `posts` WHERE (created_at > '#{SQL_TIME}') ORDER BY `posts`.`created_at` ASC) ) `posts` ORDER BY `posts`.`created_at` ASC" - ) + if ([ActiveRecord::VERSION::MAJOR, ActiveRecord::VERSION::MINOR] <=> [5, 2]) >= 0 + expect(union.to_sql.squish).to eq( + "SELECT `posts`.* FROM ( (SELECT `posts`.* FROM `posts` WHERE `posts`.`user_id` = 1 ORDER BY `posts`.`created_at` ASC) UNION (SELECT `posts`.* FROM `posts` WHERE (created_at > '2014-07-19 00:00:00') ORDER BY `posts`.`created_at` ASC) ) `posts` ORDER BY `created_at` ASC" + ) + else + expect(union.to_sql.squish).to eq( + "SELECT `posts`.* FROM ( (SELECT `posts`.* FROM `posts` WHERE `posts`.`user_id` = 1 ORDER BY `posts`.`created_at` ASC) UNION (SELECT `posts`.* FROM `posts` WHERE (created_at > '#{SQL_TIME}') ORDER BY `posts`.`created_at` ASC) ) `posts` ORDER BY `posts`.`created_at` ASC" + ) + end expect{union.to_a}.to_not raise_error end end