-
Notifications
You must be signed in to change notification settings - Fork 21.6k
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
Support infinity in PostgreSQL date columns #17365
Conversation
@mrnugget looks like one of the travis builds timed out, can you push another SHA to rebuild? |
8f333d7
to
425a966
Compare
@phoet Pushed the commit again with a new SHA to my fork. I'm not sure if this trigges a new PR build though. |
|
||
def cast_value(value) | ||
if value.is_a?(::String) | ||
case value |
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.
in my understanding the case
statement should work with none-strings as well, do you really need the if statement?
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.
The check is necessary because cast_value
might receive Float::INFINITY
, in which cause the case statement will raise an exception. See this example:
def cast_value(value)
case value
when 'infinity' then Float::INFINITY
when '-infinity' then -Float::INFINITY
else
super
end
end
puts cast_value('infinity') # => Infinity
puts cast_value(Float::INFINITY) # raises FloatDomainError
Removing the check breaks the tests.
Should I rebase the branch onto current master and solve the merge conflicts or is more work needed here? |
@senny As far as I can see, both of these issues are about the column type Am I missing something? |
@mrnugget my bad. Please ignore this comment |
Is there no appropriate ruby object that we can return? I don't like the idea of having to check whether the value on a date column is a float, risking |
I'm open to suggestions! We use |
Is there anything I can do to get this merged? Is the Edit:
I just rebased onto master and fixed the issues that came with the changed OID structure in the PostgresSQL adapter. |
This adds support for the special input string values `infinity` and `-infinity` stored in PostgreSQL date columns. When reading from the database the PostgreSQL adapter now returns `Float::INFINITY` when the stored value equals `infinity` and `-Float::INFINITY` when it equals `-infinity`.
425a966
to
57ac00a
Compare
Can
It would be weird to be pulling stuff from a date column that can't be compared to other dates. |
No, it's not comparable with dates and time. We handle it as a special case in our application. In the same way we use "infinity" in datetime columns, which are already supported like this. There is irb(main):020:0> Date.today < Date::Infinity.new
=> true
irb(main):025:0> Time.now < Date::Infinity.new
=> true |
It looks like In Rails 5, we're introducing a public API which would allow you to add support for infinite dates specifically to your application if you need this behavior and want to use |
Alright, thanks a lot for the feedback! And let me just add, that I was proposing this solution, since the support for |
Yeah, we're reviewing a lot of those behaviors, and that might end up getting deprecated. That's also why I want to be conservative with solutions that don't feel quite right, as they're very hard to change later. |
@sgrif Sorry for grave digging, but I could not find such an API in Rails 5, could you point me to it, if implemented? |
@aried3r I think Sean was talking about this Attributes API https://github.com/rails/rails/blob/v4.2.1/activerecord/lib/active_record/type/value.rb For example here I've implemented an array which is always sorted and contains only uniq elements class OrderedArrayWithUniqElements < ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Array
def type_cast_from_user(value)
super(value).sort.uniq
end
end And usage: class PriceList < ActiveRecord::Base
attribute :tags, OrderedArrayWithUniqElements.new(ActiveRecord::Type::String.new)
end |
Out of the box rails still does not seem to support postgres's 'infinity' value. I can't figure out from all the different issues what the expected behavior is, but if you google, this PR is what shows up. Trying to insert 'infinity' gets treated as nil/null. Trying to retrieve 'infinity' gets treated as nil. To fix this I have created a custom type as follows:
and putting the following in my model
|
This adds support for the special input string values
infinity
and-infinity
stored in PostgreSQL date columns.When reading from the database the PostgreSQL adapter now returns
Float::INFINITY
when the stored value equalsinfinity
and-Float::INFINITY
when it equals-infinity
.Until now Rails only had support for Infinity in
float
anddatetime
columns. We need this since we have start and end dates in our project. The end date can be infinity.Please let me know if this change is okay or if I should add something. I tried to use the same code as in
OID::DateTime
.