Skip to content
This repository
Browse code

Ticket 2256 - Recognize PostgreSQL NOW() default as equivalent to CUR…

…RENT_TIMESTAMP/CURRENT_DATE

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2378 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 0bd11857ef47d9c8228d9c1d8268a7a35768966b 1 parent e1024e5
Jeremy Kemper jeremy authored
2  activerecord/CHANGELOG
... ... @@ -1,5 +1,7 @@
1 1 *SVN*
2 2
  3 +* Recognize PostgreSQL NOW() default as equivalent to CURRENT_TIMESTAMP or CURRENT_DATE, depending on the column's type. #2256 [mat <mat@absolight.fr>]
  4 +
3 5 * Extensive documentation for the abstract database adapter. #2250 [François Beausoleil <fbeausoleil@ftml.net>]
4 6
5 7 * Clean up Fixtures.reset_sequences for PostgreSQL. Handle tables with no rows and models with custom primary keys. #2174, #2183 [jay@jay.fm, Blair Zajac <blair@orcaware.com>]
2  activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -369,7 +369,7 @@ def default_value(value)
369 369 return value if value =~ /^[0-9]+(\.[0-9]*)?/
370 370
371 371 # Date / Time magic values
372   - return Time.now.to_s if value =~ /^\('now'::text\)::(date|timestamp)/
  372 + return Time.now.to_s if value =~ /^now\(\)|^\('now'::text\)::(date|timestamp)/i
373 373
374 374 # Fixed dates / times
375 375 return $1 if value =~ /^'(.+)'::(date|timestamp)/
37 activerecord/test/base_test.rb
@@ -438,18 +438,16 @@ def test_default_values
438 438 assert_nil topic.last_read
439 439 end
440 440
441   - def test_utc_as_time_zone
442   - # Oracle does not have a TIME datatype.
443   - if ActiveRecord::ConnectionAdapters.const_defined? :OracleAdapter
444   - return true if ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters::OracleAdapter)
  441 + # Oracle does not have a TIME datatype.
  442 + unless 'OCI' == ActiveRecord::Base.connection.adapter_name
  443 + def test_utc_as_time_zone
  444 + Topic.default_timezone = :utc
  445 + attributes = { "bonus_time" => "5:42:00AM" }
  446 + topic = Topic.find(1)
  447 + topic.attributes = attributes
  448 + assert_equal Time.utc(2000, 1, 1, 5, 42, 0), topic.bonus_time
  449 + Topic.default_timezone = :local
445 450 end
446   -
447   - Topic.default_timezone = :utc
448   - attributes = { "bonus_time" => "5:42:00AM" }
449   - topic = Topic.find(1)
450   - topic.attributes = attributes
451   - assert_equal Time.utc(2000, 1, 1, 5, 42, 0), topic.bonus_time
452   - Topic.default_timezone = :local
453 451 end
454 452
455 453 def test_default_values_on_empty_strings
@@ -682,14 +680,21 @@ def test_bignum
682 680 assert_equal 2147483647, company.rating
683 681 end
684 682
685   - def test_default
686   - if Default.connection.class.name == 'ActiveRecord::ConnectionAdapters::PostgreSQLAdapter'
  683 + # TODO: extend defaults tests to other databases!
  684 + if 'PostgreSQL' == ActiveRecord::Base.connection.adapter_name
  685 + def test_default
687 686 default = Default.new
688 687
689   - # dates / timestamps
  688 + # CURRENT_TIMESTAMP and NOW() timestamps
690 689 time_format = "%m/%d/%Y %H:%M"
691   - assert_equal Time.now.strftime(time_format), default.modified_time.strftime(time_format)
692   - assert_equal Date.today, default.modified_date
  690 + now = Time.now.strftime(time_format)
  691 + assert_equal now, default.modified_time.strftime(time_format)
  692 + assert_equal now, default.modified_time_function.strftime(time_format)
  693 +
  694 + # CURRENT_DATE and NOW() dates
  695 + today = Date.today
  696 + assert_equal today, default.modified_date
  697 + assert_equal today, default.modified_date_function
693 698
694 699 # fixed dates / times
695 700 assert_equal Date.new(2004, 1, 1), default.fixed_date
2  activerecord/test/fixtures/db_definitions/postgresql.sql
@@ -102,8 +102,10 @@ CREATE TABLE booleantests (
102 102 CREATE TABLE defaults (
103 103 id serial,
104 104 modified_date date default CURRENT_DATE,
  105 + modified_date_function date default now(),
105 106 fixed_date date default '2004-01-01',
106 107 modified_time timestamp default CURRENT_TIMESTAMP,
  108 + modified_time_function timestamp default now(),
107 109 fixed_time timestamp default '2004-01-01 00:00:00.000000-00',
108 110 char1 char(1) default 'Y',
109 111 char2 character varying(50) default 'a varchar field',

0 comments on commit 0bd1185

Please sign in to comment.
Something went wrong with that request. Please try again.