提交 52434e9a 编写于 作者: R Rafael Mendonça França

Merge pull request #15295 from sgrif/sg-deprecate-decimals

Deprecate decimal columns being automatically treated as integers
...@@ -396,6 +396,7 @@ def initialize_type_map(m) # :nodoc: ...@@ -396,6 +396,7 @@ def initialize_type_map(m) # :nodoc:
precision = extract_precision(sql_type) precision = extract_precision(sql_type)
if scale == 0 if scale == 0
# FIXME: Remove this class as well
Type::DecimalWithoutScale.new(precision: precision) Type::DecimalWithoutScale.new(precision: precision)
else else
Type::Decimal.new(precision: precision, scale: scale) Type::Decimal.new(precision: precision, scale: scale)
......
...@@ -478,6 +478,8 @@ def initialize_type_map(m) # :nodoc: ...@@ -478,6 +478,8 @@ def initialize_type_map(m) # :nodoc:
# places after decimal = fmod - 4 & 0xffff # places after decimal = fmod - 4 & 0xffff
# places before decimal = (fmod - 4) >> 16 & 0xffff # places before decimal = (fmod - 4) >> 16 & 0xffff
if fmod && (fmod - 4 & 0xffff).zero? if fmod && (fmod - 4 & 0xffff).zero?
# FIXME: Remove this class, and the second argument to
# lookups on PG
Type::DecimalWithoutScale.new(precision: precision) Type::DecimalWithoutScale.new(precision: precision)
else else
OID::Decimal.new(precision: precision, scale: scale) OID::Decimal.new(precision: precision, scale: scale)
......
...@@ -64,7 +64,19 @@ def property(name, cast_type) ...@@ -64,7 +64,19 @@ def property(name, cast_type)
# Returns an array of column objects for the table associated with this class. # Returns an array of column objects for the table associated with this class.
def columns def columns
@columns ||= add_user_provided_columns(connection.schema_cache.columns(table_name)) @columns ||= add_user_provided_columns(connection.schema_cache.columns(table_name)).each do |column|
if Type::DecimalWithoutScale === column.cast_type
ActiveSupport::Deprecation.warn <<-MESSAGE.strip_heredoc
Decimal columns with 0 scale being automatically treated as integers
is deprecated, and will be removed in a future version of Rails. If
you'd like to keep this behavior, add
property :#{column.name}, Type::Integer.new
to your #{name} model.
MESSAGE
end
end
end end
# Returns a hash of column objects for the table associated with this class. # Returns a hash of column objects for the table associated with this class.
......
...@@ -984,6 +984,10 @@ def test_geometric_content ...@@ -984,6 +984,10 @@ def test_geometric_content
class NumericData < ActiveRecord::Base class NumericData < ActiveRecord::Base
self.table_name = 'numeric_data' self.table_name = 'numeric_data'
property :world_population, Type::Integer.new
property :my_house_population, Type::Integer.new
property :atoms_in_universe, Type::Integer.new
end end
def test_big_decimal_conditions def test_big_decimal_conditions
......
...@@ -15,6 +15,10 @@ ...@@ -15,6 +15,10 @@
class NumericData < ActiveRecord::Base class NumericData < ActiveRecord::Base
self.table_name = 'numeric_data' self.table_name = 'numeric_data'
property :world_population, Type::Integer.new
property :my_house_population, Type::Integer.new
property :atoms_in_universe, Type::Integer.new
end end
class CalculationsTest < ActiveRecord::TestCase class CalculationsTest < ActiveRecord::TestCase
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
require 'cases/test_case' require 'cases/test_case'
require 'active_support/dependencies' require 'active_support/dependencies'
require 'active_support/logger' require 'active_support/logger'
require 'active_support/core_ext/string/strip'
require 'support/config' require 'support/config'
require 'support/connection' require 'support/connection'
......
...@@ -11,7 +11,10 @@ ...@@ -11,7 +11,10 @@
require MIGRATIONS_ROOT + "/rename/2_rename_things" require MIGRATIONS_ROOT + "/rename/2_rename_things"
require MIGRATIONS_ROOT + "/decimal/1_give_me_big_numbers" require MIGRATIONS_ROOT + "/decimal/1_give_me_big_numbers"
class BigNumber < ActiveRecord::Base; end class BigNumber < ActiveRecord::Base
property :world_population, Type::Integer.new
property :my_house_population, Type::Integer.new
end
class Reminder < ActiveRecord::Base; end class Reminder < ActiveRecord::Base; end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册