Skip to content

Commit

Permalink
Merge pull request #20677 from jmondo/decimal-default-string
Browse files Browse the repository at this point in the history
Display decimal defaults as strings to keep precision
  • Loading branch information
sgrif committed Jun 26, 2015
2 parents 17439e3 + 4f58c50 commit 0bee410
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/type/decimal.rb
Expand Up @@ -8,7 +8,7 @@ def type
end

def type_cast_for_schema(value)
value.to_s
value.to_s.inspect
end

private
Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/cases/adapters/postgresql/money_test.rb
Expand Up @@ -56,7 +56,7 @@ def test_money_type_cast
def test_schema_dumping
output = dump_table_schema("postgresql_moneys")
assert_match %r{t\.money\s+"wealth",\s+scale: 2$}, output
assert_match %r{t\.money\s+"depth",\s+scale: 2,\s+default: 150\.55$}, output
assert_match %r{t\.money\s+"depth",\s+scale: 2,\s+default: "150\.55"$}, output
end

def test_create_and_update_money
Expand Down
5 changes: 5 additions & 0 deletions activerecord/test/cases/adapters/postgresql/schema_test.rb
Expand Up @@ -480,6 +480,7 @@ class DefaultsUsingMultipleSchemasAndDomainTest < ActiveRecord::PostgreSQLTestCa
@connection.create_table "defaults" do |t|
t.text "text_col", default: "some value"
t.string "string_col", default: "some value"
t.decimal "decimal_col", default: "3.14159265358979323846"
end
Default.reset_column_information
end
Expand All @@ -498,6 +499,10 @@ def test_string_defaults_in_new_schema_when_overriding_domain
assert_equal "some value", Default.new.string_col, "Default of string column was not correctly parsed"
end

def test_decimal_defaults_in_new_schema_when_overriding_domain
assert_equal BigDecimal.new("3.14159265358979323846"), Default.new.decimal_col, "Default of decimal column was not correctly parsed"
end

def test_bpchar_defaults_in_new_schema_when_overriding_domain
@connection.execute "ALTER TABLE defaults ADD bpchar_col bpchar DEFAULT 'some value'"
Default.reset_column_information
Expand Down
4 changes: 2 additions & 2 deletions activerecord/test/cases/schema_dumper_test.rb
Expand Up @@ -239,7 +239,7 @@ def test_schema_dumps_index_type

def test_schema_dump_includes_decimal_options
output = dump_all_table_schema([/^[^n]/])
assert_match %r{precision: 3,[[:space:]]+scale: 2,[[:space:]]+default: 2\.78}, output
assert_match %r{precision: 3,[[:space:]]+scale: 2,[[:space:]]+default: "2\.78"}, output
end

if current_adapter?(:PostgreSQLAdapter)
Expand All @@ -255,7 +255,7 @@ def test_schema_dump_includes_limit_on_array_type

def test_schema_dump_allows_array_of_decimal_defaults
output = standard_dump
assert_match %r{t\.decimal\s+"decimal_array_default",\s+default: \[1.23, 3.45\],\s+array: true}, output
assert_match %r{t\.decimal\s+"decimal_array_default",\s+default: \["1.23", "3.45"\],\s+array: true}, output
end

if ActiveRecord::Base.connection.supports_extensions?
Expand Down

0 comments on commit 0bee410

Please sign in to comment.