From b4817192aa5489014d04ee6290e4b7aac668819b Mon Sep 17 00:00:00 2001 From: Giles Alexander Date: Sat, 30 May 2009 17:44:50 +1000 Subject: [PATCH] Schema dumper now records scale 0 decimal columns as decimal not integer. The schema dumper would dump out any decimal or numeric column that had a zero scale as an integer column. This will cause problems for very large precision columns on some DBMSs, particularly PostgreSQL. It also looks strange to see your column change type after moving through schema.rb. --- activerecord/lib/active_record/schema_dumper.rb | 8 ++++++-- activerecord/test/cases/schema_dumper_test.rb | 5 +++++ activerecord/test/schema/schema.rb | 1 + 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/activerecord/lib/active_record/schema_dumper.rb b/activerecord/lib/active_record/schema_dumper.rb index de530a3..d4b8195 100644 --- a/activerecord/lib/active_record/schema_dumper.rb +++ b/activerecord/lib/active_record/schema_dumper.rb @@ -99,8 +99,12 @@ HEADER next if column.name == pk spec = {} spec[:name] = column.name.inspect - spec[:type] = column.type.to_s - spec[:limit] = column.limit.inspect if column.limit != @types[column.type][:limit] && column.type != :decimal + spec[:type] = if column.type == :integer && [/^numeric/, /^decimal/].any? { |e| e.match(column.sql_type) } + 'decimal' + else + column.type.to_s + end + spec[:limit] = column.limit.inspect if column.limit != @types[column.type][:limit] && spec[:type] != 'decimal' spec[:precision] = column.precision.inspect if !column.precision.nil? spec[:scale] = column.scale.inspect if !column.scale.nil? spec[:null] = 'false' if !column.null diff --git a/activerecord/test/cases/schema_dumper_test.rb b/activerecord/test/cases/schema_dumper_test.rb index f9ad7f3..f68f9f0 100644 --- a/activerecord/test/cases/schema_dumper_test.rb +++ b/activerecord/test/cases/schema_dumper_test.rb @@ -190,4 +190,9 @@ class SchemaDumperTest < ActiveRecord::TestCase output = stream.string assert_match %r{:precision => 3,[[:space:]]+:scale => 2,[[:space:]]+:default => 2.78}, output end + + def test_schema_dump_keeps_large_precision_integer_columns_as_decimal + output = standard_dump + assert_match %r{t.decimal\s+"atoms_in_universe",\s+:precision => 80,\s+:scale => 0}, output + end end diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index 6e8813d..6aba8a5 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -273,6 +273,7 @@ ActiveRecord::Schema.define do t.decimal :my_house_population, :precision => 2, :scale => 0 t.decimal :decimal_number_with_default, :precision => 3, :scale => 2, :default => 2.78 t.float :temperature + t.decimal :atoms_in_universe, :precision => 80, :scale => 0 end create_table :orders, :force => true do |t| -- 1.6.0.4