Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

supporting infinity and -infinity for timestamps in PG. fixes #544

  • Loading branch information...
commit dc2b9990e4b24979b0670d82e6765962803dbc38 1 parent e4f48b9
@tenderlove tenderlove authored
View
13 activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -43,6 +43,16 @@ def initialize(name, default, sql_type = nil, null = true)
# :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:
@@ -327,6 +337,9 @@ def quote(value, column = nil) #:nodoc:
if value.kind_of?(String) && column.type == :binary
"'#{escape_bytea(value)}'"
+ elsif Float === value && column.type == :datetime
+ return super unless value.infinite?
+ "'#{value.to_s.downcase}'"
elsif value.kind_of?(String) && column.sql_type == 'xml'
"xml '#{quote_string(value)}'"
elsif value.kind_of?(Numeric) && column.sql_type == 'money'
View
26 activerecord/test/cases/timestamp_test.rb
@@ -14,6 +14,32 @@ def setup
@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!
Please sign in to comment.
Something went wrong with that request. Please try again.