-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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
Print the proper ::Float::INFINITY value when used as a default value #24571
Conversation
Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @kaspth (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
Can you add a regression test to confirm this fixes the problem? Thanks!! |
I added a confirmation test for the changed parts of code. Do you also need specific regression tests? |
class FloatTest < ActiveRecord::TestCase | ||
def test_float_cast_for_schema_with_infinity_value | ||
type = Float.new | ||
assert_equal '::Float::INFINITY', type.type_cast_for_schema(::Float::INFINITY) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not the proper way to test this, please test that the schema dumper generates proper values.
f5bff9d
to
f14b952
Compare
Changed the test to use schema_dumper. |
Can you move it to be with the rest of the tests for this behavior? |
Added to the |
r? @sgrif |
c956f6b
to
8ef18a6
Compare
So the mysql2 adapter tests failed with the syntax of `float_col` float DEFAULT Infinity) ENGINE=InnoDB which means infinity default value does not work at all in the mysql adapter, whereas in sqlite and postgres the whole chain of SQL column type printing and schema dump works. Since I don't see any tests for sqlite infinity values I'm assuming it's not inherently supported, so I'm moving the test under postgresql |
65605f7
to
ad8f45e
Compare
It should remain in the schema dumper test, with |
Ok. It would have been helpful, smart and saved both of our times if instead of Should I have a separate class from SchemaDumperDefaultsTest for the test or should it be a separate table that I'll just create in its |
Ok, assuming we want to go with the latter option. :) |
We get hundreds of these pings a day, and have limited time to deal with On Thu, Apr 21, 2016, 11:24 AM Raimo Tuisku notifications@github.com
|
@@ -7,6 +7,10 @@ def type | |||
:float | |||
end | |||
|
|||
def type_cast_for_schema(value) | |||
value.inspect.gsub('Infinity', '::Float::INFINITY') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any case here other than infinity and negative infinity? Could this be more explicit and be
case value
when ::Float::INFINITY then "::Float::INFINITY"
when -::Float::INFINITY then "-::Float::INFINITY"
else super
end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this handle Float::NAN
as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea. Done.
The pull request now addresses |
@@ -7,6 +7,15 @@ def type | |||
:float | |||
end | |||
|
|||
def type_cast_for_schema(value) | |||
return "::Float::NAN" if value.try(:nan?) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line makes me angry that the decision on how to interpret "NaN does not equal to NaN" should also apply to case equality
Summary
This will properly print
::Float::INFINITY
or-::Float::INFINITY
inschema.rb
as the default value of a float / double precision column with value 'Infinity' or '-Infinity' (in the database adapters that support that value).Other Information
Performance-wise, since the
cast_value
in the ActiveModel::Type::Float already refers to potentialInfinity
values, I conclude it's also appropriate to implement the corresponding::Float::INFINITY
type name print at this abstraction level, since the the schema is not printed very often (and not having this causes problems in all database adapters that support Infinity values).Addresses #22396