1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Ticket 2256 - Recognize PostgreSQL NOW() default as equivalent to CURRENT_TIMESTAMP/CURRENT_DATE

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2378 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Jeremy Kemper 2005-09-27 23:37:57 +00:00
parent e1024e50cf
commit 0bd11857ef
4 changed files with 26 additions and 17 deletions

View file

@ -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 <mat@absolight.fr>]
* Extensive documentation for the abstract database adapter. #2250 [François Beausoleil <fbeausoleil@ftml.net>]
* 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>]

View file

@ -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)/

View file

@ -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

View file

@ -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',