Skip to content

Commit ca16b6a

Browse files
authored
handle scientific notation default values (#224)
* handle scientific notation default values * fix CI issue
1 parent 19a9ecd commit ca16b6a

3 files changed

Lines changed: 32 additions & 2 deletions

File tree

build/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
FROM cockroachdb/example-orms-builder:20200413-1918
55

66
# Native dependencies for libxml-ruby and sqlite3.
7-
RUN apt-get update -y && apt-get install -y \
7+
RUN apt-get --allow-releaseinfo-change update -y && apt-get install -y \
88
libxslt-dev \
99
libxml2-dev \
1010
libsqlite3-dev \

lib/active_record/connection_adapters/cockroachdb_adapter.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,8 @@ def extract_value_from_default(default)
352352
super ||
353353
extract_escaped_string_from_default(default) ||
354354
extract_time_from_default(default) ||
355-
extract_empty_array_from_default(default)
355+
extract_empty_array_from_default(default) ||
356+
extract_decimal_from_default(default)
356357
end
357358

358359
# Both PostgreSQL and CockroachDB use C-style string escapes under the
@@ -403,6 +404,14 @@ def extract_empty_array_from_default(default)
403404
return "{}"
404405
end
405406

407+
# This method exists to extract the decimal defaults (e.g. scientific notation)
408+
# that don't get parsed correctly
409+
def extract_decimal_from_default(default)
410+
Float(default).to_s
411+
rescue
412+
nil
413+
end
414+
406415
# override
407416
# This method makes a query to gather information about columns
408417
# in a table. It returns an array of arrays (one for each col) and

test/cases/defaults_test.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,25 @@ class DefaultExpressionTest < ActiveRecord::TestCase
2121
assert_match %r/t\.datetime\s+"modified_time_function",\s+default: -> { "now\(\)" }/, output
2222
end
2323
end
24+
25+
class DefaultNumbersTest < ActiveRecord::TestCase
26+
class DefaultNumber < ActiveRecord::Base; end
27+
28+
setup do
29+
@connection = ActiveRecord::Base.connection
30+
@connection.create_table :default_numbers do |t|
31+
t.decimal :decimal_number, precision: 32, scale: 16, default: 0
32+
end
33+
end
34+
35+
teardown do
36+
@connection.drop_table :default_numbers, if_exists: true
37+
end
38+
39+
def test_default_decimal_number_in_scientific_notation
40+
record = DefaultNumber.new
41+
assert_equal 0.0, record.decimal_number
42+
assert_equal "0.0", record.decimal_number_before_type_cast
43+
end
44+
end
2445
end

0 commit comments

Comments
 (0)