Navigation Menu

Skip to content

Commit

Permalink
Schema dumper now records scale 0 decimal columns as decimal not inte…
Browse files Browse the repository at this point in the history
…ger.

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.

Signed-off-by: Michael Koziarski <michael@koziarski.com>
[#2741 state:committed]
  • Loading branch information
gga authored and NZKoz committed Jun 1, 2009
1 parent b3839f1 commit 532219f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
11 changes: 9 additions & 2 deletions activerecord/lib/active_record/schema_dumper.rb
Expand Up @@ -99,8 +99,15 @@ def table(table, stream)
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

# AR has an optimisation which handles zero-scale decimals as integers. This
# code ensures that the dumper still dumps the column as a 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
Expand Down
5 changes: 5 additions & 0 deletions activerecord/test/cases/schema_dumper_test.rb
Expand Up @@ -190,4 +190,9 @@ def test_schema_dump_includes_decimal_options
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 => 55,\s+:scale => 0}, output
end
end
1 change: 1 addition & 0 deletions activerecord/test/schema/schema.rb
Expand Up @@ -273,6 +273,7 @@ def create_table(*args, &block)
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 => 55, :scale => 0
end

create_table :orders, :force => true do |t|
Expand Down

0 comments on commit 532219f

Please sign in to comment.