From c6df39f755e028fb3637e534c732ddec36514f83 Mon Sep 17 00:00:00 2001 From: Ray Zane Date: Fri, 9 Mar 2018 11:03:35 -0500 Subject: [PATCH 1/8] ActiveRecord::QueryMethods#build_joins now takes 3 arguments --- lib/baby_squeel/active_record/query_methods.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/baby_squeel/active_record/query_methods.rb b/lib/baby_squeel/active_record/query_methods.rb index ad0c30c..5124df1 100644 --- a/lib/baby_squeel/active_record/query_methods.rb +++ b/lib/baby_squeel/active_record/query_methods.rb @@ -40,8 +40,14 @@ def when_having(&block) # Active Record will call `group_by` on the `joins`. The # Injector has a custom `group_by` method that handles # BabySqueel::JoinExpression nodes. - def build_joins(manager, joins) - super manager, BabySqueel::JoinDependency::Injector.new(joins) + if ::ActiveRecord::VERSION::MAJOR >= 5 && ::ActiveRecord::VERSION::MINOR >= 2 + def build_joins(manager, joins, aliases) + super manager, BabySqueel::JoinDependency::Injector.new(joins), aliases + end + else + def build_joins(manager, joins) + super manager, BabySqueel::JoinDependency::Injector.new(joins) + end end end end From b9508a5ea9a01546b54da8d752c7b99b54bfbda9 Mon Sep 17 00:00:00 2001 From: Ray Zane Date: Fri, 9 Mar 2018 17:00:57 -0500 Subject: [PATCH 2/8] Mark specs that failed due to extra parents as variants --- .../__snapshots__/sifting_spec.yaml | 8 +++++ .../__snapshots__/when_having_spec.yaml | 13 +++++++++ .../__snapshots__/where_chain_spec.yaml | 29 +++++++++++++++++++ spec/integration/sifting_spec.rb | 4 +-- spec/integration/when_having_spec.rb | 8 ++--- spec/integration/where_chain_spec.rb | 18 ++++++------ 6 files changed, 65 insertions(+), 15 deletions(-) diff --git a/spec/integration/__snapshots__/sifting_spec.yaml b/spec/integration/__snapshots__/sifting_spec.yaml index efcd948..82274c7 100644 --- a/spec/integration/__snapshots__/sifting_spec.yaml +++ b/spec/integration/__snapshots__/sifting_spec.yaml @@ -9,3 +9,11 @@ BabySqueel::ActiveRecord::Base#sifter yield the root table to the block when ari INNER JOIN "authors" ON "authors"."id" = "posts"."author_id" INNER JOIN "comments" "author_comments_posts" ON "author_comments_posts"."author_id" = "authors"."id" WHERE ("author_comments_posts"."id" > 1) +'BabySqueel::ActiveRecord::Base#sifter allows the use of a sifter on an association 1 (Active Record: v5.2)': SELECT + "posts".* FROM "posts" INNER JOIN "authors" ON "authors"."id" = "posts"."author_id" + WHERE "authors"."name" LIKE 'boogies%' +'BabySqueel::ActiveRecord::Base#sifter yield the root table to the block when arity is given 1 (Active Record: v5.2)': SELECT + "posts".* FROM "posts" INNER JOIN "comments" ON "comments"."post_id" = "posts"."id" + INNER JOIN "authors" ON "authors"."id" = "posts"."author_id" INNER JOIN "comments" + "author_comments_posts" ON "author_comments_posts"."author_id" = "authors"."id" + WHERE "author_comments_posts"."id" > 1 diff --git a/spec/integration/__snapshots__/when_having_spec.yaml b/spec/integration/__snapshots__/when_having_spec.yaml index 83b1c00..ede5e55 100644 --- a/spec/integration/__snapshots__/when_having_spec.yaml +++ b/spec/integration/__snapshots__/when_having_spec.yaml @@ -24,3 +24,16 @@ FROM "posts" INNER JOIN "authors" ON "authors"."id" = "posts"."author_id" INNER JOIN "posts" "posts_authors" ON "posts_authors"."author_id" = "authors"."id" GROUP BY "posts_authors"."id" HAVING (COUNT("posts_authors"."id") > 5) +"#when_having adds a having clause 1 (Active Record: v5.2)": SELECT COUNT("posts"."id") + FROM "posts" GROUP BY "posts"."author_id" HAVING COUNT("posts"."id") > 5 +"#when_having adds a having clause with a calculation 1 (Active Record: v5.2)": SELECT + COUNT("posts"."id") FROM "posts" GROUP BY ("posts"."author_id" + 5) * 3 HAVING COUNT("posts"."id") + > 5 +"#when_having adds a having clause with an association 1 (Active Record: v5.2)": SELECT + COUNT("posts"."id") FROM "posts" INNER JOIN "authors" ON "authors"."id" = "posts"."author_id" + GROUP BY "authors"."id" HAVING COUNT("authors"."id") > 5 +"#when_having adds a having clause with an aliased table 1 (Active Record: v5.2)": SELECT + COUNT("posts_authors"."id") FROM "posts" INNER JOIN "authors" ON "authors"."id" + = "posts"."author_id" INNER JOIN "posts" "posts_authors" ON "posts_authors"."author_id" + = "authors"."id" GROUP BY "posts_authors"."id" HAVING COUNT("posts_authors"."id") + > 5 diff --git a/spec/integration/__snapshots__/where_chain_spec.yaml b/spec/integration/__snapshots__/where_chain_spec.yaml index 45f77e4..fe6ff39 100644 --- a/spec/integration/__snapshots__/where_chain_spec.yaml +++ b/spec/integration/__snapshots__/where_chain_spec.yaml @@ -65,3 +65,32 @@ ("posts"."author_id" = 42) "#where.has wheres an association using #!= 1": SELECT "posts".* FROM "posts" WHERE (("posts"."author_id" != 42)) +"#where.has wheres on deep associations 1 (Active Record: v5.2)": SELECT "posts".* + FROM "posts" INNER JOIN "authors" ON "authors"."id" = "posts"."author_id" INNER + JOIN "comments" ON "comments"."author_id" = "authors"."id" WHERE "comments"."id" + > 0 +"#where.has wheres on an aliased association 1 (Active Record: v5.2)": SELECT "posts".* + FROM "posts" INNER JOIN "authors" ON "authors"."id" = "posts"."author_id" INNER + JOIN "posts" "posts_authors" ON "posts_authors"."author_id" = "authors"."id" WHERE + "posts_authors"."id" > 0 +"#where.has wheres on an aliased association with through 1 (Active Record: v5.2)": SELECT + "posts".* FROM "posts" INNER JOIN "comments" ON "comments"."post_id" = "posts"."id" + INNER JOIN "authors" ON "authors"."id" = "posts"."author_id" INNER JOIN "comments" + "author_comments_posts" ON "author_comments_posts"."author_id" = "authors"."id" + WHERE "author_comments_posts"."id" > 0 +"#where.has wheres on an alias with a function 1 (Active Record: v5.2)": SELECT "posts".* + FROM "posts" INNER JOIN "authors" ON "authors"."id" = "posts"."author_id" INNER + JOIN "posts" "posts_authors" ON "posts_authors"."author_id" = "authors"."id" WHERE + coalesce("posts_authors"."id", 1) > 0 +"#where.has wheres with a not in subquery 1 (Active Record: v5.2)": SELECT "posts".* + FROM "posts" WHERE "posts"."author_id" NOT IN (SELECT "authors"."id" FROM "authors" + WHERE (1=0)) +"#where.has builds an exists query 1 (Active Record: v5.2)": SELECT "posts".* FROM + "posts" WHERE EXISTS(SELECT "posts".* FROM "posts" WHERE "posts"."author_id" = 1) +"#where.has builds a not exists query 1 (Active Record: v5.2)": SELECT "posts".* FROM + "posts" WHERE NOT EXISTS(SELECT "posts".* FROM "posts" WHERE "posts"."author_id" + = 1) +"#where.has wheres an association using #== 1 (Active Record: v5.2)": SELECT "posts".* + FROM "posts" WHERE "posts"."author_id" = 42 +"#where.has wheres an association using #!= 1 (Active Record: v5.2)": SELECT "posts".* + FROM "posts" WHERE "posts"."author_id" != 42 diff --git a/spec/integration/sifting_spec.rb b/spec/integration/sifting_spec.rb index f3c6ac0..8148133 100644 --- a/spec/integration/sifting_spec.rb +++ b/spec/integration/sifting_spec.rb @@ -34,7 +34,7 @@ author.sift :boogers } - expect(relation).to match_sql_snapshot + expect(relation).to match_sql_snapshot(variants: ['5.2']) end it 'yield the root table to the block when arity is given' do @@ -42,6 +42,6 @@ sift :author_comments_id, 1 } - expect(relation).to match_sql_snapshot + expect(relation).to match_sql_snapshot(variants: ['5.2']) end end diff --git a/spec/integration/when_having_spec.rb b/spec/integration/when_having_spec.rb index e44f189..eb21959 100644 --- a/spec/integration/when_having_spec.rb +++ b/spec/integration/when_having_spec.rb @@ -6,7 +6,7 @@ .grouping { author_id } .when_having { id.count > 5 } - expect(relation).to match_sql_snapshot(variants: ['4.2']) + expect(relation).to match_sql_snapshot(variants: ['4.2', '5.2']) end it 'adds a having clause with a calculation' do @@ -14,7 +14,7 @@ .grouping { (author_id + 5 ) * 3 } .when_having { id.count > 5 } - expect(relation).to match_sql_snapshot(variants: ['4.2']) + expect(relation).to match_sql_snapshot(variants: ['4.2', '5.2']) end it 'adds a having clause with an association' do @@ -23,7 +23,7 @@ .grouping { author.id } .when_having { author.id.count > 5 } - expect(relation).to match_sql_snapshot(variants: ['4.2']) + expect(relation).to match_sql_snapshot(variants: ['4.2', '5.2']) end it 'adds a having clause with an aliased table' do @@ -32,6 +32,6 @@ .grouping { author.posts.id } .when_having { author.posts.id.count > 5 } - expect(relation).to match_sql_snapshot(variants: ['4.2']) + expect(relation).to match_sql_snapshot(variants: ['4.2', '5.2']) end end diff --git a/spec/integration/where_chain_spec.rb b/spec/integration/where_chain_spec.rb index cabf5b5..dee31a4 100644 --- a/spec/integration/where_chain_spec.rb +++ b/spec/integration/where_chain_spec.rb @@ -50,7 +50,7 @@ author.comments.id > 0 } - expect(relation).to match_sql_snapshot + expect(relation).to match_sql_snapshot(variants: ['5.2']) end it 'wheres on an aliased association' do @@ -58,7 +58,7 @@ author.posts.id > 0 } - expect(relation).to match_sql_snapshot + expect(relation).to match_sql_snapshot(variants: ['5.2']) end it 'wheres on an aliased association with through' do @@ -66,7 +66,7 @@ author_comments.id > 0 } - expect(relation).to match_sql_snapshot + expect(relation).to match_sql_snapshot(variants: ['5.2']) end it 'wheres on polymorphic associations' do @@ -106,7 +106,7 @@ coalesce(author.posts.id, 1) > 0 } - expect(relation).to match_sql_snapshot + expect(relation).to match_sql_snapshot(variants: ['5.2']) end it 'wheres with a subquery' do @@ -141,7 +141,7 @@ author_id.not_in Author.none.select(:id) } - expect(relation).to match_sql_snapshot + expect(relation).to match_sql_snapshot(variants: ['5.2']) end it 'wheres using a simple table' do @@ -163,7 +163,7 @@ exists Post.where.has { author_id == 1 } } - expect(relation).to match_sql_snapshot + expect(relation).to match_sql_snapshot(variants: ['5.2']) end it 'builds a not exists query' do @@ -171,7 +171,7 @@ not_exists Post.where.has { author_id == 1 } } - expect(relation).to match_sql_snapshot + expect(relation).to match_sql_snapshot(variants: ['5.2']) end it 'wheres an association using #==' do @@ -184,7 +184,7 @@ post.author == author end - expect(relation).to match_sql_snapshot + expect(relation).to match_sql_snapshot(variants: ['5.2']) end it 'wheres an association using #!=' do @@ -197,7 +197,7 @@ post.author != author end - expect(relation).to match_sql_snapshot + expect(relation).to match_sql_snapshot(variants: ['5.2']) end it 'handles a hash' do From 94f4458683dbbe77104707cd3f9abacb8f763f56 Mon Sep 17 00:00:00 2001 From: Ray Zane Date: Fri, 9 Mar 2018 17:04:48 -0500 Subject: [PATCH 3/8] Add 5.2 to travis --- .travis.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index da915d1..9c6a24a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,8 +13,10 @@ env: - AR=4.2.10 - AR=5.0.6 - AR=5.1.5 + - AR=5.2.0.rc1 - AR=latest - AR=4.2.10 COMPAT=1 - AR=5.0.6 COMPAT=1 - AR=5.1.5 COMPAT=1 + - AR=5.2.0.rc1 COMPAT=1 - AR=latest COMPAT=1 From 6d86116ffe23a185a29524a212e097919a803b22 Mon Sep 17 00:00:00 2001 From: Ray Zane Date: Mon, 12 Mar 2018 11:23:13 -0400 Subject: [PATCH 4/8] Fix association spec --- spec/baby_squeel/association_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/baby_squeel/association_spec.rb b/spec/baby_squeel/association_spec.rb index 5e0894e..5068f2e 100644 --- a/spec/baby_squeel/association_spec.rb +++ b/spec/baby_squeel/association_spec.rb @@ -36,7 +36,7 @@ subject(:association) { create_association Post, :author } if ActiveRecord::VERSION::MAJOR >= 5 - it 'returns an wrapped Arel::Nodes::And' do + it 'generates SQL' do node = association == Author.new(id: 42) expect(node._arel.to_sql).to eq('"posts"."author_id" = 42') end @@ -59,7 +59,7 @@ subject(:association) { create_association Post, :author } if ActiveRecord::VERSION::MAJOR >= 5 - it 'returns some wrapped arel' do + it 'generates SQL' do node = association != Author.new(id: 42) expect(node._arel.to_sql).to eq('("posts"."author_id" != 42)') end From 605d3e37dbd3116c754b5189dc60fe5870814b6a Mon Sep 17 00:00:00 2001 From: Ray Zane Date: Fri, 30 Mar 2018 14:23:47 -0400 Subject: [PATCH 5/8] Rename JoinExpression to Join --- lib/baby_squeel/active_record/query_methods.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/baby_squeel/active_record/query_methods.rb b/lib/baby_squeel/active_record/query_methods.rb index 5124df1..a9c585a 100644 --- a/lib/baby_squeel/active_record/query_methods.rb +++ b/lib/baby_squeel/active_record/query_methods.rb @@ -39,7 +39,7 @@ def when_having(&block) # This is a monkey patch, and I'm not happy about it. # Active Record will call `group_by` on the `joins`. The # Injector has a custom `group_by` method that handles - # BabySqueel::JoinExpression nodes. + # BabySqueel::Join nodes. if ::ActiveRecord::VERSION::MAJOR >= 5 && ::ActiveRecord::VERSION::MINOR >= 2 def build_joins(manager, joins, aliases) super manager, BabySqueel::JoinDependency::Injector.new(joins), aliases From e3daec954f6d6443fafd4f8d5c10520ab0881c62 Mon Sep 17 00:00:00 2001 From: Ray Zane Date: Fri, 30 Mar 2018 15:33:28 -0400 Subject: [PATCH 6/8] Fix association spec --- spec/baby_squeel/__snapshots__/association_spec.yaml | 7 +++++++ spec/baby_squeel/association_spec.rb | 4 ++-- 2 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 spec/baby_squeel/__snapshots__/association_spec.yaml diff --git a/spec/baby_squeel/__snapshots__/association_spec.yaml b/spec/baby_squeel/__snapshots__/association_spec.yaml new file mode 100644 index 0000000..1be18ae --- /dev/null +++ b/spec/baby_squeel/__snapshots__/association_spec.yaml @@ -0,0 +1,7 @@ +--- +BabySqueel::Association#== generates SQL 1: '"posts"."author_id" = 42' +BabySqueel::Association#!= generates SQL 1: ("posts"."author_id" != 42) +'BabySqueel::Association#== generates SQL 1 (Active Record: v5.2)': '"posts"."author_id" + = ?' +'BabySqueel::Association#!= generates SQL 1 (Active Record: v5.2)': '"posts"."author_id" + != ?' diff --git a/spec/baby_squeel/association_spec.rb b/spec/baby_squeel/association_spec.rb index 5068f2e..1e0bc24 100644 --- a/spec/baby_squeel/association_spec.rb +++ b/spec/baby_squeel/association_spec.rb @@ -38,7 +38,7 @@ if ActiveRecord::VERSION::MAJOR >= 5 it 'generates SQL' do node = association == Author.new(id: 42) - expect(node._arel.to_sql).to eq('"posts"."author_id" = 42') + expect(node._arel.to_sql).to match_sql_snapshot(variants: ['5.2']) end it 'throws for an invalid comparison' do @@ -61,7 +61,7 @@ if ActiveRecord::VERSION::MAJOR >= 5 it 'generates SQL' do node = association != Author.new(id: 42) - expect(node._arel.to_sql).to eq('("posts"."author_id" != 42)') + expect(node._arel.to_sql).to match_sql_snapshot(variants: ['5.2']) end it 'throws for an invalid comparison' do From d5129646b98b66bf1674801bf9a7cc3a04396672 Mon Sep 17 00:00:00 2001 From: Ray Zane Date: Wed, 25 Apr 2018 14:11:35 -0400 Subject: [PATCH 7/8] 5.2 is out in the wild --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 9c6a24a..191dbff 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,10 +13,10 @@ env: - AR=4.2.10 - AR=5.0.6 - AR=5.1.5 - - AR=5.2.0.rc1 + - AR=5.2.0 - AR=latest - AR=4.2.10 COMPAT=1 - AR=5.0.6 COMPAT=1 - AR=5.1.5 COMPAT=1 - - AR=5.2.0.rc1 COMPAT=1 + - AR=5.2.0 COMPAT=1 - AR=latest COMPAT=1 From 34e07ba73ceb0ef9b8c1d4d39f67fb4f9b7e5b67 Mon Sep 17 00:00:00 2001 From: Ray Zane Date: Wed, 25 Apr 2018 14:19:08 -0400 Subject: [PATCH 8/8] Mark specs that are broken due to polyamorous as pending --- spec/integration/joining_spec.rb | 12 ++++++++++++ spec/integration/where_chain_spec.rb | 8 ++++++++ 2 files changed, 20 insertions(+) diff --git a/spec/integration/joining_spec.rb b/spec/integration/joining_spec.rb index a53c2e4..3a2fd24 100644 --- a/spec/integration/joining_spec.rb +++ b/spec/integration/joining_spec.rb @@ -122,12 +122,20 @@ describe 'polymorphism' do it 'inner joins' do + if ActiveRecord::VERSION::STRING >= '5.2.0' + pending "polyamorous's support for polymorphism is broken" + end + relation = Picture.joining { imageable.of(Post) } expect(relation).to match_sql_snapshot end it 'outer joins' do + if ActiveRecord::VERSION::STRING >= '5.2.0' + pending "polyamorous's support for polymorphism is broken" + end + relation = Picture.joining { imageable.of(Post).outer } expect(relation).to match_sql_snapshot @@ -156,6 +164,10 @@ end it 'handles polymorphism' do + if ActiveRecord::VERSION::STRING >= '5.2.0' + pending "polyamorous's support for polymorphism is broken" + end + relation = Picture.joining { imageable.of(Post).comments } expect(relation).to match_sql_snapshot diff --git a/spec/integration/where_chain_spec.rb b/spec/integration/where_chain_spec.rb index dee31a4..3ba8344 100644 --- a/spec/integration/where_chain_spec.rb +++ b/spec/integration/where_chain_spec.rb @@ -70,6 +70,10 @@ end it 'wheres on polymorphic associations' do + if ActiveRecord::VERSION::STRING >= '5.2.0' + pending "polyamorous's support for polymorphism is broken" + end + relation = Picture.joining { imageable.of(Post) }.where.has { imageable.of(Post).title =~ 'meatloaf' } @@ -78,6 +82,10 @@ end it 'wheres on polymorphic associations outer join' do + if ActiveRecord::VERSION::STRING >= '5.2.0' + pending "polyamorous's support for polymorphism is broken" + end + relation = Picture.joining { imageable.of(Post).outer }.where.has { imageable.of(Post).title =~ 'meatloaf' }