提交 03035d69 编写于 作者: R Rafael Mendonça França

Merge pull request #15184 from sgrif/sg-remove-timestamp-type

Remove :timestamp column type
* Remove unused `:timestamp` type. Transparently alias it to `:datetime`
in all cases. Fixes inconsistencies when column types are sent outside of
`ActiveRecord`, such as for XML Serialization.
*Sean Griffin*
* Fix bug that added `table_name_prefix` and `table_name_suffix` to
extension names in PostgreSQL when migrating.
......
......@@ -149,7 +149,7 @@ def instantiate_time_object(set_values)
end
def read_time
# If column is a :time (and not :date or :timestamp) there is no need to validate if
# If column is a :time (and not :date or :datetime) there is no need to validate if
# there are year/month/day fields
if column.type == :time
# if the column is a time set the values to their defaults as January 1, 1970, but only if they're nil
......
......@@ -35,7 +35,7 @@ def #{method_name}
extend ActiveSupport::Concern
ATTRIBUTE_TYPES_CACHED_BY_DEFAULT = [:datetime, :timestamp, :time, :date]
ATTRIBUTE_TYPES_CACHED_BY_DEFAULT = [:datetime, :time, :date]
included do
class_attribute :attribute_types_cached_by_default, instance_writer: false
......@@ -52,7 +52,7 @@ def cache_attributes(*attribute_names)
end
# Returns the attributes which are cached. By default time related columns
# with datatype <tt>:datetime, :timestamp, :time, :date</tt> are cached.
# with datatype <tt>:datetime, :time, :date</tt> are cached.
def cached_attributes
@cached_attributes ||= columns.select { |c| cacheable_column?(c) }.map { |col| col.name }.to_set
end
......
......@@ -28,7 +28,7 @@ def type
module ClassMethods
protected
# Defined for all +datetime+ and +timestamp+ attributes when +time_zone_aware_attributes+ are enabled.
# Defined for all +datetime+ attributes when +time_zone_aware_attributes+ are enabled.
# This enhanced write method will automatically convert the time passed to it to the zone stored in Time.zone.
def define_method_attribute=(attr_name)
if create_time_zone_conversion_attribute?(attr_name, columns_hash[attr_name])
......@@ -51,7 +51,7 @@ def #{attr_name}=(time)
def create_time_zone_conversion_attribute?(name, column)
time_zone_aware_attributes &&
!self.skip_time_zone_conversion_for_attributes.include?(name.to_sym) &&
(:datetime == column.type || :timestamp == column.type)
(:datetime == column.type)
end
end
end
......
......@@ -262,6 +262,7 @@ def references(*args)
alias :belongs_to :references
def new_column_definition(name, type, options) # :nodoc:
type = aliased_types[type] || type
column = create_column_definition name, type
limit = options.fetch(:limit) do
native[type][:limit] if native[type].is_a?(Hash)
......@@ -292,6 +293,12 @@ def primary_key_column_name
def native
@native
end
def aliased_types
HashWithIndifferentAccess.new(
timestamp: :datetime,
)
end
end
class AlterTable # :nodoc:
......
......@@ -382,7 +382,7 @@ def initialize_type_map(m) # :nodoc:
m.alias_type %r(clob)i, 'text'
m.register_type %r(date)i, Type::Date.new
m.register_type %r(time)i, Type::Time.new
m.register_type %r(timestamp)i, Type::Timestamp.new
m.alias_type %r(timestamp)i, 'datetime'
m.register_type %r(datetime)i, Type::DateTime.new
m.alias_type %r(numeric)i, 'decimal'
m.alias_type %r(number)i, 'decimal'
......
......@@ -163,7 +163,6 @@ def missing_default_forged_as_empty_string?(default)
:float => { :name => "float" },
:decimal => { :name => "decimal" },
:datetime => { :name => "datetime" },
:timestamp => { :name => "datetime" },
:time => { :name => "time" },
:date => { :name => "date" },
:binary => { :name => "blob" },
......
......@@ -63,7 +63,7 @@ def klass
when :integer then Fixnum
when :float then Float
when :decimal then BigDecimal
when :datetime, :timestamp, :time then Time
when :datetime, :time then Time
when :date then Date
when :text, :string, :binary then String
when :boolean then Object
......@@ -110,7 +110,7 @@ def type_cast(value)
when :integer then klass.value_to_integer(value)
when :float then value.to_f
when :decimal then klass.value_to_decimal(value)
when :datetime, :timestamp then klass.string_to_time(value)
when :datetime then klass.string_to_time(value)
when :time then klass.string_to_dummy_time(value)
when :date then klass.value_to_date(value)
when :binary then klass.binary_to_string(value)
......
......@@ -216,7 +216,6 @@ class Table < ActiveRecord::ConnectionAdapters::Table
float: { name: "float" },
decimal: { name: "decimal" },
datetime: { name: "timestamp" },
timestamp: { name: "timestamp" },
time: { name: "time" },
date: { name: "date" },
daterange: { name: "daterange" },
......
......@@ -69,7 +69,6 @@ class SQLite3Adapter < AbstractAdapter
float: { name: "float" },
decimal: { name: "decimal" },
datetime: { name: "datetime" },
timestamp: { name: "datetime" },
time: { name: "time" },
date: { name: "date" },
binary: { name: "blob" },
......
......@@ -9,7 +9,6 @@
require 'active_record/connection_adapters/type/string'
require 'active_record/connection_adapters/type/text'
require 'active_record/connection_adapters/type/time'
require 'active_record/connection_adapters/type/timestamp'
require 'active_record/connection_adapters/type/type_map'
module ActiveRecord
......
require 'active_record/connection_adapters/type/timestamp'
module ActiveRecord
module ConnectionAdapters
module Type
class DateTime < Timestamp # :nodoc:
class DateTime < Value # :nodoc:
def type
:datetime
end
......
module ActiveRecord
module ConnectionAdapters
module Type
class Timestamp < Value # :nodoc:
def type
:timestamp
end
end
end
end
end
......@@ -516,7 +516,7 @@ def test_raises_dangerous_attribute_error_when_defining_activerecord_method_in_m
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
......
......@@ -109,19 +109,14 @@ def test_type_cast_time
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
......
......@@ -21,7 +21,7 @@ def test_registering_types
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 @@ def test_aliasing_types
def test_changing_type_changes_aliases
time = Time.new
timestamp = Timestamp.new
timestamp = DateTime.new
mapping = TypeMap.new
mapping.register_type(/timestamp/i, time)
......
......@@ -45,14 +45,11 @@ def test_time_types
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
......
......@@ -233,6 +233,23 @@ def test_add_column_not_null_with_default
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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册