1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Correctly handle infinity value in PostgreSQL range type

An empty string is an invalid value in Ruby's range class.
So need to handle `Float::INFINITY` as it is and cast it in
`encode_range`.

Fixes #31612
This commit is contained in:
yuuji.yaginuma 2018-01-02 11:24:53 +09:00 committed by Yuji Yaginuma
parent ff4f69ff2d
commit bce675eac4
3 changed files with 22 additions and 2 deletions

View file

@ -60,7 +60,7 @@ module ActiveRecord
end
def type_cast_single_for_database(value)
infinity?(value) ? "" : @subtype.serialize(value)
infinity?(value) ? value : @subtype.serialize(value)
end
def extract_bounds(value)

View file

@ -138,7 +138,7 @@ module ActiveRecord
end
def encode_range(range)
"[#{type_cast(range.first)},#{type_cast(range.last)}#{range.exclude_end? ? ')' : ']'}"
"[#{type_cast_range_value(range.first)},#{type_cast_range_value(range.last)}#{range.exclude_end? ? ')' : ']'}"
end
def determine_encoding_of_strings_in_array(value)
@ -154,6 +154,14 @@ module ActiveRecord
else _type_cast(values)
end
end
def type_cast_range_value(value)
infinity?(value) ? "" : type_cast(value)
end
def infinity?(value)
value.respond_to?(:infinite?) && value.infinite?
end
end
end
end

View file

@ -358,6 +358,18 @@ _SQL
end
end
def test_infinity_values
PostgresqlRange.create!(int4_range: 1..Float::INFINITY,
int8_range: -Float::INFINITY..0,
float_range: -Float::INFINITY..Float::INFINITY)
record = PostgresqlRange.first
assert_equal(1...Float::INFINITY, record.int4_range)
assert_equal(-Float::INFINITY...1, record.int8_range)
assert_equal(-Float::INFINITY...Float::INFINITY, record.float_range)
end
private
def assert_equal_round_trip(range, attribute, value)
round_trip(range, attribute, value)