Skip to content

Commit 57c0729

Browse files
carlallenrafiss
authored andcommitted
Fix Decimal type when precision and scale are unspecified
1 parent 3824f86 commit 57c0729

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

lib/active_record/connection_adapters/cockroachdb_adapter.rb

+3-6
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class CockroachDBAdapter < PostgreSQLAdapter
9898
st_polygon: {},
9999
}
100100

101-
# http://postgis.17.x6.nabble.com/Default-SRID-td5001115.html
101+
# http://postgis.17.x6.nabble.com/Default-SRID-td5001115.html
102102
DEFAULT_SRID = 0
103103

104104
include CockroachDB::SchemaStatements
@@ -272,12 +272,9 @@ def initialize_type_map(m = type_map)
272272
precision = extract_precision(sql_type)
273273
scale = extract_scale(sql_type)
274274

275-
# TODO(#178) this should never use DecimalWithoutScale since scale
276-
# is assumed to be 0 if it is not explicitly defined.
277-
#
278275
# If fmod is -1, that means that precision is defined but not
279276
# scale, or neither is defined.
280-
if fmod && fmod == -1
277+
if fmod && fmod == -1 && !precision.nil?
281278
# Below comment is from ActiveRecord
282279
# FIXME: Remove this class, and the second argument to
283280
# lookups on PG
@@ -390,7 +387,7 @@ def extract_time_from_default(default)
390387
# In general, it is hard to parse that, but it is easy to handle the common
391388
# case of an empty array.
392389
def extract_empty_array_from_default(default)
393-
return unless supports_string_to_array_coercion?
390+
return unless supports_string_to_array_coercion?
394391
return unless default =~ /\AARRAY\[\]\z/
395392
return "{}"
396393
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require "cases/helper_cockroachdb"
2+
3+
# Load dependencies from ActiveRecord test suite
4+
require "support/schema_dumping_helper"
5+
6+
module CockroachDB
7+
class PostgresqlNumberTest < ActiveRecord::PostgreSQLTestCase
8+
include SchemaDumpingHelper
9+
10+
class PostgresqlNumber < ActiveRecord::Base; end
11+
12+
setup do
13+
@connection = ActiveRecord::Base.connection
14+
@connection.create_table("postgresql_numbers", force: true) do |t|
15+
t.decimal 'decimal_default'
16+
end
17+
end
18+
19+
teardown do
20+
@connection.drop_table "postgresql_decimals", if_exists: true
21+
end
22+
23+
def test_decimal_values
24+
record = PostgresqlNumber.new(decimal_default: 111.222)
25+
assert_equal record.decimal_default, 111.222
26+
end
27+
end
28+
end

0 commit comments

Comments
 (0)