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

Print the proper ::Float::INFINITY value when used as a default value #24571

Merged
merged 1 commit into from
Oct 4, 2016

Conversation

raimo
Copy link
Contributor

@raimo raimo commented Apr 15, 2016

Summary

This will properly print ::Float::INFINITYor -::Float::INFINITY in schema.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 potential Infinity 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

@rails-bot
Copy link

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.

@maclover7
Copy link
Contributor

Can you add a regression test to confirm this fixes the problem? Thanks!!

@raimo
Copy link
Contributor Author

raimo commented Apr 18, 2016

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)
Copy link
Contributor

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.

@raimo raimo force-pushed the patch-1 branch 2 times, most recently from f5bff9d to f14b952 Compare April 19, 2016 23:48
@raimo
Copy link
Contributor Author

raimo commented Apr 19, 2016

Changed the test to use schema_dumper.

@sgrif
Copy link
Contributor

sgrif commented Apr 19, 2016

Can you move it to be with the rest of the tests for this behavior?

@raimo
Copy link
Contributor Author

raimo commented Apr 20, 2016

Added to the schema_dumper_test.rb along with other default value tests.

@kaspth
Copy link
Contributor

kaspth commented Apr 20, 2016

r? @sgrif

@rails-bot rails-bot assigned sgrif and unassigned kaspth Apr 20, 2016
@raimo raimo force-pushed the patch-1 branch 2 times, most recently from c956f6b to 8ef18a6 Compare April 20, 2016 14:33
@raimo
Copy link
Contributor Author

raimo commented Apr 20, 2016

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 infinity_test.rb.

@raimo raimo force-pushed the patch-1 branch 2 times, most recently from 65605f7 to ad8f45e Compare April 20, 2016 14:40
@sgrif
Copy link
Contributor

sgrif commented Apr 20, 2016

It should remain in the schema dumper test, with skip unless current_adapter?(:PostgreSQL)

@raimo
Copy link
Contributor Author

raimo commented Apr 21, 2016

Ok. It would have been helpful, smart and saved both of our times if instead of "this" behavior you would have referred to something more specific before. Since obviously there's a specific way you want to go with, could you help a bit first to get the process forward:

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 before block (and skip both in the before block and in the actual test from others than PostgreSQL using skip unless current_adapter?(:PostgreSQL))?

@raimo
Copy link
Contributor Author

raimo commented Apr 21, 2016

Ok, assuming we want to go with the latter option. :)

@sgrif
Copy link
Contributor

sgrif commented Apr 21, 2016

We get hundreds of these pings a day, and have limited time to deal with
them. As such we can't always reply immediately. Thank you for your patience

On Thu, Apr 21, 2016, 11:24 AM Raimo Tuisku notifications@github.com
wrote:

Ok, assuming we want to go with the latter option. :)


You are receiving this because you were assigned.
Reply to this email directly or view it on GitHub
#24571 (comment)

@@ -7,6 +7,10 @@ def type
:float
end

def type_cast_for_schema(value)
value.inspect.gsub('Infinity', '::Float::INFINITY')
Copy link
Contributor

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

Copy link
Contributor

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. Done.

@raimo
Copy link
Contributor Author

raimo commented Oct 4, 2016

The pull request now addresses Float::NAN as well.

@@ -7,6 +7,15 @@ def type
:float
end

def type_cast_for_schema(value)
return "::Float::NAN" if value.try(:nan?)
Copy link
Contributor

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

@sgrif sgrif merged commit de9a56b into rails:master Oct 4, 2016
shadowghostgg pushed a commit to shadowghostgg/rails that referenced this pull request Oct 5, 2016
This reverts commit de9a56b, reversing
changes made to 4d6feef.
@raimo raimo deleted the patch-1 branch March 30, 2017 03:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants