From d0f8c46e1962b28d77209f367f12c2d2c77f4b12 Mon Sep 17 00:00:00 2001 From: Sean Griffin <sean@thoughtbot.com> Date: Mon, 19 May 2014 10:59:57 -0700 Subject: Remove :timestamp column type The `:timestamp` type for columns is unused. All database adapters treat them as the same database type. All code in `ActiveRecord` which changes its behavior based on the column's type acts the same in both cases. However, when the type is passed to code that checks for the `:datetime` type, but not `:timestamp` (such as XML serialization), the result is unexpected behavior. Existing schema definitions will continue to work, and the `timestamp` type is transparently aliased to `datetime`. --- activerecord/test/cases/attribute_methods_test.rb | 2 +- activerecord/test/cases/column_test.rb | 21 ++++++++------------- .../cases/connection_adapters/type/type_map_test.rb | 4 ++-- .../cases/connection_adapters/type_lookup_test.rb | 7 ++----- .../test/cases/migration/change_schema_test.rb | 17 +++++++++++++++++ 5 files changed, 30 insertions(+), 21 deletions(-) (limited to 'activerecord/test/cases') diff --git a/activerecord/test/cases/attribute_methods_test.rb b/activerecord/test/cases/attribute_methods_test.rb index 38e93288e4..495b43c9e5 100644 --- a/activerecord/test/cases/attribute_methods_test.rb +++ b/activerecord/test/cases/attribute_methods_test.rb @@ -516,7 +516,7 @@ class AttributeMethodsTest < ActiveRecord::TestCase end def test_only_time_related_columns_are_meant_to_be_cached_by_default - expected = %w(datetime timestamp time date).sort + expected = %w(datetime time date).sort assert_equal expected, ActiveRecord::Base.attribute_types_cached_by_default.map(&:to_s).sort end diff --git a/activerecord/test/cases/column_test.rb b/activerecord/test/cases/column_test.rb index 91605a52f9..fffcb19e56 100644 --- a/activerecord/test/cases/column_test.rb +++ b/activerecord/test/cases/column_test.rb @@ -109,19 +109,14 @@ module ActiveRecord end def test_type_cast_datetime_and_timestamp - columns = [ - Column.new("field", nil, Type::DateTime.new), - Column.new("field", nil, Type::Timestamp.new), - ] - columns.each do |column| - assert_equal nil, column.type_cast(nil) - assert_equal nil, column.type_cast('') - assert_equal nil, column.type_cast(' ') - assert_equal nil, column.type_cast('ABC') - - datetime_string = Time.now.utc.strftime("%FT%T") - assert_equal datetime_string, column.type_cast(datetime_string).strftime("%FT%T") - end + column = Column.new("field", nil, Type::DateTime.new) + assert_equal nil, column.type_cast(nil) + assert_equal nil, column.type_cast('') + assert_equal nil, column.type_cast(' ') + assert_equal nil, column.type_cast('ABC') + + datetime_string = Time.now.utc.strftime("%FT%T") + assert_equal datetime_string, column.type_cast(datetime_string).strftime("%FT%T") end def test_type_cast_date diff --git a/activerecord/test/cases/connection_adapters/type/type_map_test.rb b/activerecord/test/cases/connection_adapters/type/type_map_test.rb index 565f44eef8..300c2ed225 100644 --- a/activerecord/test/cases/connection_adapters/type/type_map_test.rb +++ b/activerecord/test/cases/connection_adapters/type/type_map_test.rb @@ -21,7 +21,7 @@ module ActiveRecord def test_overriding_registered_types time = Time.new - timestamp = Timestamp.new + timestamp = DateTime.new mapping = TypeMap.new mapping.register_type(/time/i, time) @@ -51,7 +51,7 @@ module ActiveRecord def test_changing_type_changes_aliases time = Time.new - timestamp = Timestamp.new + timestamp = DateTime.new mapping = TypeMap.new mapping.register_type(/timestamp/i, time) diff --git a/activerecord/test/cases/connection_adapters/type_lookup_test.rb b/activerecord/test/cases/connection_adapters/type_lookup_test.rb index a5b01260d3..18df30faf5 100644 --- a/activerecord/test/cases/connection_adapters/type_lookup_test.rb +++ b/activerecord/test/cases/connection_adapters/type_lookup_test.rb @@ -45,14 +45,11 @@ module ActiveRecord assert_lookup_type :time, 'TIME' end - def test_timestamp_types - assert_lookup_type :timestamp, 'timestamp' - assert_lookup_type :timestamp, 'TIMESTAMP' - end - def test_datetime_types assert_lookup_type :datetime, 'datetime' assert_lookup_type :datetime, 'DATETIME' + assert_lookup_type :datetime, 'timestamp' + assert_lookup_type :datetime, 'TIMESTAMP' end def test_decimal_types diff --git a/activerecord/test/cases/migration/change_schema_test.rb b/activerecord/test/cases/migration/change_schema_test.rb index cbad718b43..b63dcf4459 100644 --- a/activerecord/test/cases/migration/change_schema_test.rb +++ b/activerecord/test/cases/migration/change_schema_test.rb @@ -233,6 +233,23 @@ module ActiveRecord end end + def test_add_column_with_timestamp_type + connection.create_table :testings do |t| + t.column :foo, :timestamp + end + + klass = Class.new(ActiveRecord::Base) + klass.table_name = 'testings' + + assert_equal :datetime, klass.columns_hash['foo'].type + + if current_adapter?(:PostgreSQLAdapter) + assert_equal 'timestamp without time zone', klass.columns_hash['foo'].sql_type + else + assert_equal klass.connection.type_to_sql('datetime'), klass.columns_hash['foo'].sql_type + end + end + def test_change_column_quotes_column_names connection.create_table :testings do |t| t.column :select, :string -- cgit v1.2.3