aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorTony Schneider <tonywok@gmail.com>2012-06-21 17:40:54 -0400
committerTony Schneider <tonywok@gmail.com>2012-06-22 14:18:44 -0400
commit6657ec9a0a05c1b3ddcb7aa679ebfdb7e0bc4959 (patch)
treeaa7b6b7dd270bddacc17e495a0e999b66ef47059 /activerecord/lib
parenta232831f2dc0dd2ccec0ab152a87614b1fc95fb0 (diff)
downloadrails-6657ec9a0a05c1b3ddcb7aa679ebfdb7e0bc4959.tar.gz
rails-6657ec9a0a05c1b3ddcb7aa679ebfdb7e0bc4959.tar.bz2
rails-6657ec9a0a05c1b3ddcb7aa679ebfdb7e0bc4959.zip
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
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb10
1 files changed, 10 insertions, 0 deletions
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