Skip to content

Commit

Permalink
handle atttypmod being correct for decimals from v22.1
Browse files Browse the repository at this point in the history
  • Loading branch information
otan committed Feb 2, 2022
1 parent 64bd3ee commit 38cc9fe
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions lib/active_record/connection_adapters/cockroachdb_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ def supports_string_to_array_coercion?
@crdb_version >= 2020
end

def needs_numeric_override?
@crdb_version < 2210
end

def supports_partitioned_indexes?
false
end
Expand All @@ -232,7 +236,7 @@ def initialize(connection, logger, conn_params, config)
if crdb_version_string.include? "v1."
version_num = 1
elsif crdb_version_string.include? "v2."
version_num 2
version_num = 2
elsif crdb_version_string.include? "v19.1."
version_num = 1910
elsif crdb_version_string.include? "v19.2."
Expand All @@ -245,8 +249,10 @@ def initialize(connection, logger, conn_params, config)
version_num = 2110
elsif crdb_version_string.include? "v21.2.0"
version_num = 2120
else
elsif crdb_version_string.include? "v21.2."
version_num = 2121
else
version_num = 2210
end
@crdb_version = version_num

Expand Down Expand Up @@ -291,21 +297,23 @@ def initialize_type_map(m = type_map)
# Once that PR is merged, we can call super at the top.
super(m)

# Override numeric type. This is almost identical to the default,
# Override numeric type pre-22.1. This is almost identical to the default,
# except that the conditional based on the fmod is changed.
m.register_type "numeric" do |_, fmod, sql_type|
precision = extract_precision(sql_type)
scale = extract_scale(sql_type)

# If fmod is -1, that means that precision is defined but not
# scale, or neither is defined.
if fmod && fmod == -1 && !precision.nil?
# Below comment is from ActiveRecord
# FIXME: Remove this class, and the second argument to
# lookups on PG
Type::DecimalWithoutScale.new(precision: precision)
else
OID::Decimal.new(precision: precision, scale: scale)
if needs_numeric_override?
m.register_type "numeric" do |_, fmod, sql_type|
precision = extract_precision(sql_type)
scale = extract_scale(sql_type)

# If fmod is -1, that means that precision is defined but not
# scale, or neither is defined.
if fmod && fmod == -1 && !precision.nil?
# Below comment is from ActiveRecord
# FIXME: Remove this class, and the second argument to
# lookups on PG
Type::DecimalWithoutScale.new(precision: precision)
else
OID::Decimal.new(precision: precision, scale: scale)
end
end
end
end
Expand Down

0 comments on commit 38cc9fe

Please sign in to comment.