Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent making bind param if casted value is nil #29282

Conversation

@kamipo
Copy link
Member

@kamipo kamipo commented May 30, 2017

If casted value is nil, generated SQL should be IS NULL. But currently
it is generated as = NULL. To prevent this behavior, avoid making bind
param if casted value is nil.

Fixes #28945.

activerecord/test/cases/relation/where_test.rb Outdated
sql = Topic.where(last_read: "").to_sql

assert_no_match %r{["`]last_read["`] = NULL}, sql
assert_match %r{["`]last_read["`] IS NULL}, sql

This comment has been minimized.

@matthewd

matthewd May 30, 2017
Member

Let's use assert_equal 1, Task.where(starting: "").count instead. Trying to match quoted identifiers seems tricky, because we'd have to account for SQL Server, etc. (And just in general, if we can prove what we need without being specific about the SQL, so much the better.)

Copy link
Member

@maclover7 maclover7 left a comment

Should we also add a CHANGELOG.md entry?

If casted value is nil, generated SQL should be `IS NULL`. But currently
it is generated as `= NULL`. To prevent this behavior, avoid making bind
param if casted value is nil.

Fixes #28945.
@kamipo kamipo force-pushed the kamipo:prevent_making_bind_param_if_casted_value_is_nil branch to 67a4a9f May 31, 2017
@@ -48,6 +48,10 @@ def test_where_copies_arel_bind_params
assert_equal [chef], chefs.to_a
end

def test_where_with_casted_value_is_nil
assert_equal 4, Topic.where(last_read: "").count

This comment has been minimized.

@kamipo

kamipo May 31, 2017
Author Member

I changed the test without assert_match against generated SQL.

@@ -1,3 +1,7 @@
* Prevent making bind param if casted value is nil.

*Ryuta Kamizono*

This comment has been minimized.

@kamipo

kamipo May 31, 2017
Author Member

I added a CHANGELOG.md entry.

@eileencodes eileencodes merged commit 6bc1468 into rails:master Jun 2, 2017
2 checks passed
2 checks passed
codeclimate no new or fixed issues
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@kamipo kamipo deleted the kamipo:prevent_making_bind_param_if_casted_value_is_nil branch Jun 2, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

4 participants