diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index e57515fb11..dafcdbea52 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Recognize PostgreSQL NOW() default as equivalent to CURRENT_TIMESTAMP or CURRENT_DATE, depending on the column's type. #2256 [mat ] + * Extensive documentation for the abstract database adapter. #2250 [François Beausoleil ] * 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 ] diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 037f5f6869..cee3483e35 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -369,7 +369,7 @@ module ActiveRecord return value if value =~ /^[0-9]+(\.[0-9]*)?/ # Date / Time magic values - return Time.now.to_s if value =~ /^\('now'::text\)::(date|timestamp)/ + return Time.now.to_s if value =~ /^now\(\)|^\('now'::text\)::(date|timestamp)/i # Fixed dates / times return $1 if value =~ /^'(.+)'::(date|timestamp)/ diff --git a/activerecord/test/base_test.rb b/activerecord/test/base_test.rb index 3e73915e30..50d1673361 100755 --- a/activerecord/test/base_test.rb +++ b/activerecord/test/base_test.rb @@ -438,18 +438,16 @@ class BasicsTest < Test::Unit::TestCase assert_nil topic.last_read end - def test_utc_as_time_zone - # Oracle does not have a TIME datatype. - if ActiveRecord::ConnectionAdapters.const_defined? :OracleAdapter - return true if ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters::OracleAdapter) + # Oracle does not have a TIME datatype. + unless 'OCI' == ActiveRecord::Base.connection.adapter_name + def test_utc_as_time_zone + Topic.default_timezone = :utc + attributes = { "bonus_time" => "5:42:00AM" } + topic = Topic.find(1) + topic.attributes = attributes + assert_equal Time.utc(2000, 1, 1, 5, 42, 0), topic.bonus_time + Topic.default_timezone = :local end - - Topic.default_timezone = :utc - attributes = { "bonus_time" => "5:42:00AM" } - topic = Topic.find(1) - topic.attributes = attributes - assert_equal Time.utc(2000, 1, 1, 5, 42, 0), topic.bonus_time - Topic.default_timezone = :local end def test_default_values_on_empty_strings @@ -682,14 +680,21 @@ class BasicsTest < Test::Unit::TestCase assert_equal 2147483647, company.rating end - def test_default - if Default.connection.class.name == 'ActiveRecord::ConnectionAdapters::PostgreSQLAdapter' + # TODO: extend defaults tests to other databases! + if 'PostgreSQL' == ActiveRecord::Base.connection.adapter_name + def test_default default = Default.new - # dates / timestamps + # CURRENT_TIMESTAMP and NOW() timestamps time_format = "%m/%d/%Y %H:%M" - assert_equal Time.now.strftime(time_format), default.modified_time.strftime(time_format) - assert_equal Date.today, default.modified_date + now = Time.now.strftime(time_format) + assert_equal now, default.modified_time.strftime(time_format) + assert_equal now, default.modified_time_function.strftime(time_format) + + # CURRENT_DATE and NOW() dates + today = Date.today + assert_equal today, default.modified_date + assert_equal today, default.modified_date_function # fixed dates / times assert_equal Date.new(2004, 1, 1), default.fixed_date diff --git a/activerecord/test/fixtures/db_definitions/postgresql.sql b/activerecord/test/fixtures/db_definitions/postgresql.sql index 21ca82a4ab..c8cfcb779c 100644 --- a/activerecord/test/fixtures/db_definitions/postgresql.sql +++ b/activerecord/test/fixtures/db_definitions/postgresql.sql @@ -102,8 +102,10 @@ CREATE TABLE booleantests ( CREATE TABLE defaults ( id serial, modified_date date default CURRENT_DATE, + modified_date_function date default now(), fixed_date date default '2004-01-01', modified_time timestamp default CURRENT_TIMESTAMP, + modified_time_function timestamp default now(), fixed_time timestamp default '2004-01-01 00:00:00.000000-00', char1 char(1) default 'Y', char2 character varying(50) default 'a varchar field',