提交 fa73cf72 编写于 作者: B Bogdan Gusiev

Fix postgresql adapter to handle bc timestamps correctly

上级 f058e565
## Rails 4.0.0 (unreleased) ## ## Rails 4.0.0 (unreleased) ##
* Fix postgresql adapter to handle BC timestamps correctly
HistoryEvent.create!(:name => "something", :occured_at => Date.new(0) - 5.years)
*Bogdan Gusiev*
* When running migrations on Postgresql, the `:limit` option for `binary` and `text` columns is silently dropped. * When running migrations on Postgresql, the `:limit` option for `binary` and `text` columns is silently dropped.
Previously, these migrations caused sql exceptions, because Postgresql doesn't support limits on these types. Previously, these migrations caused sql exceptions, because Postgresql doesn't support limits on these types.
......
...@@ -8,6 +8,8 @@ def string_to_time(string) ...@@ -8,6 +8,8 @@ def string_to_time(string)
case string case string
when 'infinity'; 1.0 / 0.0 when 'infinity'; 1.0 / 0.0
when '-infinity'; -1.0 / 0.0 when '-infinity'; -1.0 / 0.0
when / BC$/
super("-" + string.sub(/ BC$/, ""))
else else
super super
end end
......
...@@ -129,11 +129,15 @@ def quote_column_name(name) #:nodoc: ...@@ -129,11 +129,15 @@ def quote_column_name(name) #:nodoc:
# Quote date/time values for use in SQL input. Includes microseconds # Quote date/time values for use in SQL input. Includes microseconds
# if the value is a Time responding to usec. # if the value is a Time responding to usec.
def quoted_date(value) #:nodoc: def quoted_date(value) #:nodoc:
result = super
if value.acts_like?(:time) && value.respond_to?(:usec) if value.acts_like?(:time) && value.respond_to?(:usec)
"#{super}.#{sprintf("%06d", value.usec)}" result = "#{result}.#{sprintf("%06d", value.usec)}"
else end
super
if value.year < 0
result = result.sub(/^-/, "") + " BC"
end end
result
end end
end end
end end
......
...@@ -75,6 +75,15 @@ def test_postgres_agrees_with_activerecord_about_precision ...@@ -75,6 +75,15 @@ def test_postgres_agrees_with_activerecord_about_precision
assert_equal '4', pg_datetime_precision('foos', 'updated_at') assert_equal '4', pg_datetime_precision('foos', 'updated_at')
end end
def test_bc_timestamp
unless current_adapter?(:PostgreSQLAdapter)
return skip("only tested on postgresql")
end
date = Date.new(0) - 1.second
Developer.create!(:name => "aaron", :updated_at => date)
assert_equal date, Developer.find_by_name("aaron").updated_at
end
private private
def pg_datetime_precision(table_name, column_name) def pg_datetime_precision(table_name, column_name)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册