From a1f5f37687985977079941a808236b25edaf1320 Mon Sep 17 00:00:00 2001 From: Oliver Tan Date: Wed, 2 Feb 2022 11:06:25 +1100 Subject: [PATCH] handle atttypmod being correct for decimals from v22.1 --- .../cockroachdb_adapter.rb | 36 ++++++++++--------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/lib/active_record/connection_adapters/cockroachdb_adapter.rb b/lib/active_record/connection_adapters/cockroachdb_adapter.rb index e0e478df..4623c8aa 100644 --- a/lib/active_record/connection_adapters/cockroachdb_adapter.rb +++ b/lib/active_record/connection_adapters/cockroachdb_adapter.rb @@ -232,7 +232,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." @@ -245,8 +245,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 @@ -291,21 +293,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 @crdb_version < 2210 + 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