From 6657ec9a0a05c1b3ddcb7aa679ebfdb7e0bc4959 Mon Sep 17 00:00:00 2001 From: Tony Schneider Date: Thu, 21 Jun 2012 17:40:54 -0400 Subject: Allow precision option for postgresql datetimes This patch addresses the difficulty of retrieving datetime fields. By default, the database holds a higher precision than the time as a String. This issue is discussed at length at the following links: - [#3519](https://github.com/rails/rails/issues/3519) - [#3520](https://github.com/rails/rails/issues/3520) Also, kudos to @mattscilipoti --- .../active_record/connection_adapters/postgresql_adapter.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 43e243581c..a9940209fa 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -187,6 +187,7 @@ module ActiveRecord case sql_type when /^bigint/i; 8 when /^smallint/i; 2 + when /^timestamp/i; nil else super end end @@ -201,6 +202,8 @@ module ActiveRecord def extract_precision(sql_type) if sql_type == 'money' self.class.money_precision + elsif sql_type =~ /timestamp/i + $1.to_i if sql_type =~ /\((\d+)\)/ else super end @@ -1285,6 +1288,13 @@ module ActiveRecord when 5..8; 'bigint' else raise(ActiveRecordError, "No integer type has byte size #{limit}. Use a numeric with precision 0 instead.") end + when 'datetime' + return super unless precision + + case precision + when 0..6; "timestamp(#{precision})" + else raise(ActiveRecordError, "No timestamp type has precision of #{precision}. The allowed range of precision is from 0 to 6") + end else super end -- cgit v1.2.3