Skip to content
This repository

Postgresql adapter: fix handling of BC timestamps #6245

Merged
merged 1 commit into from over 1 year ago

3 participants

Bogdan Gusiev Steve Klabnik Rafael Mendonça França
Bogdan Gusiev
bogdan commented

Ruby and Postgresql representation of BC(before christ) time stamps is different:

Ruby serializes it with negative number of years. Postgresql adds BC suffix instead.
This cause a bug.

This patch fixes datetime conversion in postgresql adapter to handle this edge case correctly.

If you know a better way of fixing it - please advice.

...cord/test/cases/adapters/postgresql/timestamp_test.rb
... ... @@ -27,4 +27,13 @@ def test_save_infinity_and_beyond
27 27 d = Developer.create!(:name => 'aaron', :updated_at => -1.0 / 0.0)
28 28 assert_equal(-1.0 / 0.0, d.updated_at)
29 29 end
  30 +
  31 + def test_bc_timestamp
  32 + unless current_adapter?(:PostgreSQLAdapter)
2
Steve Klabnik Collaborator

Is this actually needed? Since this is under the postgresql adapters test, won't it only be tested on postgresql?

Bogdan Gusiev
bogdan added a note

I saw this in the tests above in this file.
We can try to remove this, but I would address it to different pull or commit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Steve Klabnik
Collaborator

This will need a rebase.

Bogdan Gusiev bogdan reopened this
Rafael Mendonça França

Hey @bogdan your fixes seems fine. We will need a CHANGELOG entry and remove these two extra commits.

Bogdan Gusiev

done

Rafael Mendonça França rafaelfranca merged commit 1c0e7ab into from
Bogdan Gusiev bogdan deleted the branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Nov 21, 2012
Bogdan Gusiev bogdan Fix postgresql adapter to handle bc timestamps correctly fa73cf7
This page is out of date. Refresh to see the latest.
6 activerecord/CHANGELOG.md
Source Rendered
... ... @@ -1,5 +1,11 @@
1 1 ## Rails 4.0.0 (unreleased) ##
2 2
  3 +* Fix postgresql adapter to handle BC timestamps correctly
  4 +
  5 + HistoryEvent.create!(:name => "something", :occured_at => Date.new(0) - 5.years)
  6 +
  7 + *Bogdan Gusiev*
  8 +
3 9 * When running migrations on Postgresql, the `:limit` option for `binary` and `text` columns is silently dropped.
4 10 Previously, these migrations caused sql exceptions, because Postgresql doesn't support limits on these types.
5 11
2  activerecord/lib/active_record/connection_adapters/postgresql/cast.rb
@@ -8,6 +8,8 @@ def string_to_time(string)
8 8 case string
9 9 when 'infinity'; 1.0 / 0.0
10 10 when '-infinity'; -1.0 / 0.0
  11 + when / BC$/
  12 + super("-" + string.sub(/ BC$/, ""))
11 13 else
12 14 super
13 15 end
10 activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb
@@ -129,11 +129,15 @@ def quote_column_name(name) #:nodoc:
129 129 # Quote date/time values for use in SQL input. Includes microseconds
130 130 # if the value is a Time responding to usec.
131 131 def quoted_date(value) #:nodoc:
  132 + result = super
132 133 if value.acts_like?(:time) && value.respond_to?(:usec)
133   - "#{super}.#{sprintf("%06d", value.usec)}"
134   - else
135   - super
  134 + result = "#{result}.#{sprintf("%06d", value.usec)}"
  135 + end
  136 +
  137 + if value.year < 0
  138 + result = result.sub(/^-/, "") + " BC"
136 139 end
  140 + result
137 141 end
138 142 end
139 143 end
9 activerecord/test/cases/adapters/postgresql/timestamp_test.rb
@@ -75,6 +75,15 @@ def test_postgres_agrees_with_activerecord_about_precision
75 75 assert_equal '4', pg_datetime_precision('foos', 'updated_at')
76 76 end
77 77
  78 + def test_bc_timestamp
  79 + unless current_adapter?(:PostgreSQLAdapter)
  80 + return skip("only tested on postgresql")
  81 + end
  82 + date = Date.new(0) - 1.second
  83 + Developer.create!(:name => "aaron", :updated_at => date)
  84 + assert_equal date, Developer.find_by_name("aaron").updated_at
  85 + end
  86 +
78 87 private
79 88
80 89 def pg_datetime_precision(table_name, column_name)

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.