From e45e4f44e35d6ce9868542cc2a151b2a6c497e9b Mon Sep 17 00:00:00 2001 From: Sean Griffin Date: Wed, 21 May 2014 15:07:13 -0700 Subject: [PATCH] Move extract_scale to decimal type The only type that has a scale is decimal. There's a special case where decimal columns with 0 scale are type cast to integers if the scale is not specified. Appears to only affect schema dumping. --- activerecord/lib/active_record/connection_adapters/type.rb | 4 ++-- .../lib/active_record/connection_adapters/type/decimal.rb | 2 ++ .../lib/active_record/connection_adapters/type/value.rb | 5 +---- activerecord/test/cases/schema_dumper_test.rb | 4 ++-- 4 files changed, 7 insertions(+), 8 deletions(-) diff --git a/activerecord/lib/active_record/connection_adapters/type.rb b/activerecord/lib/active_record/connection_adapters/type.rb index 763176cb2b..9103ae85c0 100644 --- a/activerecord/lib/active_record/connection_adapters/type.rb +++ b/activerecord/lib/active_record/connection_adapters/type.rb @@ -22,8 +22,8 @@ module Type # :nodoc: class << self def extract_scale(sql_type) case sql_type - when /^(numeric|decimal|number)\((\d+)\)/i then 0 - when /^(numeric|decimal|number)\((\d+)(,(\d+))\)/i then $4.to_i + when /\((\d+)\)/ then 0 + when /\((\d+)(,(\d+))\)/ then $3.to_i end end end diff --git a/activerecord/lib/active_record/connection_adapters/type/decimal.rb b/activerecord/lib/active_record/connection_adapters/type/decimal.rb index ac5af4b963..a8cd1cf5b5 100644 --- a/activerecord/lib/active_record/connection_adapters/type/decimal.rb +++ b/activerecord/lib/active_record/connection_adapters/type/decimal.rb @@ -4,6 +4,8 @@ module Type class Decimal < Value # :nodoc: include Numeric + delegate :extract_scale, to: Type + def type :decimal end diff --git a/activerecord/lib/active_record/connection_adapters/type/value.rb b/activerecord/lib/active_record/connection_adapters/type/value.rb index 289c27f6d4..52d9ed9bc0 100644 --- a/activerecord/lib/active_record/connection_adapters/type/value.rb +++ b/activerecord/lib/active_record/connection_adapters/type/value.rb @@ -3,10 +3,7 @@ module ConnectionAdapters module Type class Value # :nodoc: def type; end - - def extract_scale(sql_type) - Type.extract_scale(sql_type) - end + def extract_scale(sql_type); end def type_cast(value) cast_value(value) unless value.nil? diff --git a/activerecord/test/cases/schema_dumper_test.rb b/activerecord/test/cases/schema_dumper_test.rb index fd0ef2f89f..61bca976f7 100644 --- a/activerecord/test/cases/schema_dumper_test.rb +++ b/activerecord/test/cases/schema_dumper_test.rb @@ -353,9 +353,9 @@ def test_schema_dump_keeps_large_precision_integer_columns_as_decimal output = standard_dump # Oracle supports precision up to 38 and it identifies decimals with scale 0 as integers if current_adapter?(:OracleAdapter) - assert_match %r{t.integer\s+"atoms_in_universe",\s+precision: 38,\s+scale: 0}, output + assert_match %r{t.integer\s+"atoms_in_universe",\s+precision: 38}, output else - assert_match %r{t.decimal\s+"atoms_in_universe",\s+precision: 55,\s+scale: 0}, output + assert_match %r{t.decimal\s+"atoms_in_universe",\s+precision: 55}, output end end -- GitLab