Skip to content

Fix Decimal type when precision and scale are unspecified #197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions lib/active_record/connection_adapters/cockroachdb_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class CockroachDBAdapter < PostgreSQLAdapter
st_polygon: {},
}

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

include CockroachDB::SchemaStatements
Expand Down Expand Up @@ -272,12 +272,9 @@ def initialize_type_map(m = type_map)
precision = extract_precision(sql_type)
scale = extract_scale(sql_type)

# TODO(#178) this should never use DecimalWithoutScale since scale
# is assumed to be 0 if it is not explicitly defined.
#
Comment on lines -275 to -277
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this comment should still be here since ideally we wouldn't need this conditional and OID::Decimal would properly handle the case where precision is defined, but the scale is not.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I removed it because it's not in the original rails code and the new logic is much closer to the original logic in rails.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you're right. I think issue #178 can be closed since this handles the case described in that issue (DECIMAL where only precision is specified).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, can you add this back in now? I'm going to add to that issue to reflect the problem we saw with precision being improperly handled.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I verified it does the same behavior in vanilla activerecord.

# If fmod is -1, that means that precision is defined but not
# scale, or neither is defined.
if fmod && fmod == -1
if fmod && fmod == -1 && !precision.nil?
# Below comment is from ActiveRecord
# FIXME: Remove this class, and the second argument to
# lookups on PG
Expand Down Expand Up @@ -390,7 +387,7 @@ def extract_time_from_default(default)
# In general, it is hard to parse that, but it is easy to handle the common
# case of an empty array.
def extract_empty_array_from_default(default)
return unless supports_string_to_array_coercion?
return unless supports_string_to_array_coercion?
return unless default =~ /\AARRAY\[\]\z/
return "{}"
end
Expand Down
28 changes: 28 additions & 0 deletions test/cases/adapters/postgresql/numeric_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require "cases/helper_cockroachdb"

# Load dependencies from ActiveRecord test suite
require "support/schema_dumping_helper"

module CockroachDB
class PostgresqlNumberTest < ActiveRecord::PostgreSQLTestCase
include SchemaDumpingHelper

class PostgresqlNumber < ActiveRecord::Base; end

setup do
@connection = ActiveRecord::Base.connection
@connection.create_table("postgresql_numbers", force: true) do |t|
t.decimal 'decimal_default'
end
end

teardown do
@connection.drop_table "postgresql_decimals", if_exists: true
end

def test_decimal_values
record = PostgresqlNumber.new(decimal_default: 111.222)
assert_equal record.decimal_default, 111.222
end
end
end