From ce1a1b47be2abc42062c24954108550d8a431e1b Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 13 May 2011 11:49:39 -0700 Subject: supporting infinity and -infinity for timestamps in PG. fixes #544 --- .../connection_adapters/postgresql_adapter.rb | 13 +++++++++++ activerecord/test/cases/timestamp_test.rb | 26 ++++++++++++++++++++++ 2 files changed, 39 insertions(+) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 37db2be7a9..f4beeceb61 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -39,6 +39,16 @@ module ActiveRecord # :stopdoc: class << self attr_accessor :money_precision + def string_to_time(string) + return string unless String === string + + case string + when 'infinity' then 1.0 / 0.0 + when '-infinity' then -1.0 / 0.0 + else + super + end + end end # :startdoc: @@ -349,6 +359,9 @@ module ActiveRecord return super unless column case value + when Float + return super unless value.infinite? && column.type == :datetime + "'#{value.to_s.downcase}'" when Numeric return super unless column.sql_type == 'money' # Not truly string input, so doesn't require (or allow) escape string syntax. diff --git a/activerecord/test/cases/timestamp_test.rb b/activerecord/test/cases/timestamp_test.rb index ceb1452afd..22d4cac422 100644 --- a/activerecord/test/cases/timestamp_test.rb +++ b/activerecord/test/cases/timestamp_test.rb @@ -14,6 +14,32 @@ class TimestampTest < ActiveRecord::TestCase @previously_updated_at = @developer.updated_at end + def test_load_infinity_and_beyond + unless current_adapter?(:PostgreSQLAdapter) + return skip("only tested on postgresql") + end + + d = Developer.find_by_sql("select 'infinity'::timestamp as updated_at") + assert d.first.updated_at.infinite?, 'timestamp should be infinite' + + d = Developer.find_by_sql("select '-infinity'::timestamp as updated_at") + time = d.first.updated_at + assert time.infinite?, 'timestamp should be infinite' + assert_operator time, :<, 0 + end + + def test_save_infinity_and_beyond + unless current_adapter?(:PostgreSQLAdapter) + return skip("only tested on postgresql") + end + + d = Developer.create!(:name => 'aaron', :updated_at => 1.0 / 0.0) + assert_equal(1.0 / 0.0, d.updated_at) + + d = Developer.create!(:name => 'aaron', :updated_at => -1.0 / 0.0) + assert_equal(-1.0 / 0.0, d.updated_at) + end + def test_saving_a_changed_record_updates_its_timestamp @developer.name = "Jack Bauer" @developer.save! -- cgit v1.2.3