diff --git a/Gemfile b/Gemfile index 22d0dbb..ecc2060 100644 --- a/Gemfile +++ b/Gemfile @@ -5,4 +5,4 @@ gemspec gem 'rails', ENV.fetch('RAILS_VERSION') gem 'pg' -gem 'sqlite3' +gem 'sqlite3', '~> 1.3' diff --git a/active_record_union.gemspec b/active_record_union.gemspec index a945f75..dc11cd4 100644 --- a/active_record_union.gemspec +++ b/active_record_union.gemspec @@ -18,7 +18,7 @@ Gem::Specification.new do |spec| spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) + spec.files.grep(%r{^bin/}) spec.require_paths = ["lib"] - spec.add_dependency "activerecord", ">= 4.0" + spec.add_dependency "activerecord", ">= 7.0" spec.add_development_dependency "bundler" spec.add_development_dependency "rake" diff --git a/spec/union_spec.rb b/spec/union_spec.rb index f2020e1..431b02f 100644 --- a/spec/union_spec.rb +++ b/spec/union_spec.rb @@ -41,15 +41,9 @@ end def bind_values_from_relation(relation) - if ActiveRecord.gem_version >= Gem::Version.new('5.2.0.beta2') - relation.arel_table.class.engine.connection.visitor.accept( - relation.arel.ast, Arel::Collectors::Bind.new - ).value.map(&:value) - elsif ActiveRecord::VERSION::MAJOR >= 5 - relation.bound_attributes.map { |a| a.value_for_database } - else - (relation.arel.bind_values + relation.bind_values).map { |_column, value| value } - end + relation.arel_table.class.engine.connection.visitor.accept( + relation.arel.ast, Arel::Collectors::Bind.new + ).value.map(&:value) end it "binds values properly" do @@ -72,16 +66,16 @@ def bind_values_from_relation(relation) bind_values = bind_values_from_relation union expect(bind_values).to eq([true, 11]) - expect(union.to_sql.squish).to eq( - "SELECT \"users\".* FROM ( SELECT \"users\".* FROM \"users\" INNER JOIN \"posts\" ON \"posts\".\"user_id\" = \"users\".\"id\" AND \"posts\".\"draft\" = 't' UNION SELECT \"users\".* FROM \"users\" WHERE \"users\".\"id\" = 11 ) \"users\"" + "SELECT \"users\".* FROM ( SELECT \"users\".* FROM \"users\" INNER JOIN \"posts\" ON \"posts\".\"draft\" = 1 AND \"posts\".\"user_id\" = \"users\".\"id\" UNION SELECT \"users\".* FROM \"users\" WHERE \"users\".\"id\" = 11 ) \"users\"" ) expect{union.to_a}.to_not raise_error end it "doesn't repeat default scopes" do expect(Time).to receive(:now) { Time.utc(2014, 7, 24, 0, 0, 0) } - sql_now = "2014-07-24 00:00:00#{".000000" if ActiveRecord::VERSION::MAJOR < 5}" + + sql_now = "2014-07-24 00:00:00" class PublishedPost < ActiveRecord::Base self.table_name = "posts" @@ -105,15 +99,10 @@ class PublishedPost < ActiveRecord::Base context "in SQLite" do it "lets ORDER BY in query subselects throw a syntax error" do - 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_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" + ) + expect{union.to_a}.to raise_error(ActiveRecord::StatementInvalid) end end @@ -121,15 +110,10 @@ class PublishedPost < ActiveRecord::Base context "in Postgres" do it "wraps query subselects in parentheses to allow ORDER BY clauses" do Databases.with_postgres do - 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_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" + ) + expect{union.to_a}.to_not raise_error end end @@ -138,15 +122,10 @@ class PublishedPost < ActiveRecord::Base context "in MySQL" do it "wraps query subselects in parentheses to allow ORDER BY clauses" do Databases.with_mysql do - 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_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" + ) + expect{union.to_a}.to_not raise_error end end