Skip to content

Commit

Permalink
Allow double parens in SQL spec
Browse files Browse the repository at this point in the history
On Rails post-7.1 edge @ c402ec78724a, SQL generated for certain 'NOT' queries has a double set of parens nested.

Our spec testing for SQL then fails.  We modify it to accept double set of parens.  This makes no semantic difference, our code was working on this version of Rails regardless; the spec just has to accep the variant SQL that means the same thing.

(No idea why Rails changed to generate a double set of parens here, or if it's a bug somewhere; but the SQL still works fine, an unnecessary double set of parens doesn't hurt anything I'm aware of, so I'm not going to worry about it).
  • Loading branch information
jrochkind committed Feb 26, 2024
1 parent 244e4ed commit 7d78003
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
4 changes: 2 additions & 2 deletions spec/query_scopes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@
describe "#not_jsonb_contains" do
it 'generates a negated query' do
query = klass.not_jsonb_contains(str: 'foo')

expect(query.to_sql).to match(/WHERE \(?NOT \(products.json_attributes @> \('{"str":"foo"}'\)::jsonb\)/)
# Rails 8 has double parens for some reason, previous does not
expect(query.to_sql).to match(/WHERE \(?NOT \(?\(products.json_attributes @> \('{"str":"foo"}'\)::jsonb\)\)?/)
end
end

Expand Down
9 changes: 6 additions & 3 deletions spec/type/polymorphic_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -262,15 +262,18 @@
describe "not_jsonb_contains" do
it "can create keypath query" do
sql = klass.not_jsonb_contains("one_poly.bool" => true).to_sql
expect(sql).to match(/WHERE \(?NOT \(products.json_attributes @> \('{\"one_poly\":{\"bool\":true}}'\)/)
# Rails 8 has double parens for some reason, previous does not
expect(sql).to match(/WHERE \(?NOT \(?\(products.json_attributes @> \('{\"one_poly\":{\"bool\":true}}'\)\)?/)
end
it "can create keypath query with type" do
sql = klass.not_jsonb_contains("one_poly" => {"bool" => true, "type" => "Model2"}).to_sql
expect(sql).to match(/WHERE \(?NOT \(products.json_attributes @> \('{\"one_poly\":{\"bool\":true,\"type\":\"Model2\"}}'\)/)
# Rails 8 has double parens for some reason, previous does not
expect(sql).to match(/WHERE \(?NOT \(?\(products.json_attributes @> \('{\"one_poly\":{\"bool\":true,\"type\":\"Model2\"}}'\)\)?/)
end
it "can create keypath query with model arg" do
sql = klass.not_jsonb_contains("one_poly" => model2.new(bool: true)).to_sql
expect(sql).to match(/WHERE \(?NOT \(products.json_attributes @> \('{\"one_poly\":{\"bool\":true,\"type\":\"Model2\"}}'\)/)
# Rails 8 has double parens for some reason, previous does not
expect(sql).to match(/WHERE \(?NOT \(?\(products.json_attributes @> \('{\"one_poly\":{\"bool\":true,\"type\":\"Model2\"}}'\)\)?/)
end
end
end

0 comments on commit 7d78003

Please sign in to comment.