Skip to content

Commit 8ec5c87

Browse files
authored
Merge pull request rails#29869 from kamipo/make_type_map_to_private
Make `type_map` to private because it is only used in the connection adapter
2 parents 81d7b98 + 2ec2075 commit 8ec5c87

File tree

9 files changed

+28
-31
lines changed

9 files changed

+28
-31
lines changed

activerecord/lib/active_record/connection_adapters/abstract_adapter.rb

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -453,12 +453,6 @@ def close
453453
pool.checkin self
454454
end
455455

456-
def type_map # :nodoc:
457-
@type_map ||= Type::TypeMap.new.tap do |mapping|
458-
initialize_type_map(mapping)
459-
end
460-
end
461-
462456
def column_name_for_operation(operation, node) # :nodoc:
463457
visitor.accept(node, collector).value
464458
end
@@ -486,8 +480,13 @@ def default_index_type?(index) # :nodoc:
486480
end
487481

488482
private
483+
def type_map
484+
@type_map ||= Type::TypeMap.new.tap do |mapping|
485+
initialize_type_map(mapping)
486+
end
487+
end
489488

490-
def initialize_type_map(m)
489+
def initialize_type_map(m = type_map)
491490
register_class_with_limit m, %r(boolean)i, Type::Boolean
492491
register_class_with_limit m, %r(char)i, Type::String
493492
register_class_with_limit m, %r(binary)i, Type::Binary
@@ -520,7 +519,7 @@ def initialize_type_map(m)
520519

521520
def reload_type_map
522521
type_map.clear
523-
initialize_type_map(type_map)
522+
initialize_type_map
524523
end
525524

526525
def register_class_with_limit(mapping, key, klass)

activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ def without_sql_mode(mode)
547547
execute("SET @@SESSION.sql_mode = #{sql_mode}")
548548
end
549549

550-
def initialize_type_map(m)
550+
def initialize_type_map(m = type_map)
551551
super
552552

553553
register_class_with_limit m, %r(char)i, MysqlString

activerecord/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ def run(records)
3131
composites.each { |row| register_composite_type(row) }
3232
end
3333

34-
def query_conditions_for_initial_load(type_map)
35-
known_type_names = type_map.keys.map { |n| "'#{n}'" }
34+
def query_conditions_for_initial_load
35+
known_type_names = @store.keys.map { |n| "'#{n}'" }
3636
known_type_types = %w('r' 'e' 'd')
3737
<<-SQL % [known_type_names.join(", "), known_type_types.join(", ")]
3838
WHERE

activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ def initialize(connection, logger, connection_parameters, config)
218218
add_pg_decoders
219219

220220
@type_map = Type::HashLookupTypeMap.new
221-
initialize_type_map(type_map)
221+
initialize_type_map
222222
@local_tz = execute("SHOW TIME ZONE", "SCHEMA").first["TimeZone"]
223223
@use_insert_returning = @config.key?(:insert_returning) ? self.class.type_cast_config_to_boolean(@config[:insert_returning]) : true
224224
end
@@ -427,7 +427,7 @@ def translate_exception(exception, message)
427427

428428
def get_oid_type(oid, fmod, column_name, sql_type = "".freeze)
429429
if !type_map.key?(oid)
430-
load_additional_types(type_map, [oid])
430+
load_additional_types([oid])
431431
end
432432

433433
type_map.fetch(oid, fmod, sql_type) {
@@ -438,7 +438,7 @@ def get_oid_type(oid, fmod, column_name, sql_type = "".freeze)
438438
}
439439
end
440440

441-
def initialize_type_map(m)
441+
def initialize_type_map(m = type_map)
442442
register_class_with_limit m, "int2", Type::Integer
443443
register_class_with_limit m, "int4", Type::Integer
444444
register_class_with_limit m, "int8", Type::Integer
@@ -505,7 +505,7 @@ def initialize_type_map(m)
505505
end
506506
end
507507

508-
load_additional_types(m)
508+
load_additional_types
509509
end
510510

511511
def extract_limit(sql_type)
@@ -554,7 +554,7 @@ def has_default_function?(default_value, default)
554554
!default_value && %r{\w+\(.*\)|\(.*\)::\w+|CURRENT_DATE|CURRENT_TIMESTAMP}.match?(default)
555555
end
556556

557-
def load_additional_types(type_map, oids = nil)
557+
def load_additional_types(oids = nil)
558558
initializer = OID::TypeMapInitializer.new(type_map)
559559

560560
if supports_ranges?
@@ -573,7 +573,7 @@ def load_additional_types(type_map, oids = nil)
573573
if oids
574574
query += "WHERE t.oid::integer IN (%s)" % oids.join(", ")
575575
else
576-
query += initializer.query_conditions_for_initial_load(type_map)
576+
query += initializer.query_conditions_for_initial_load
577577
end
578578

579579
execute_and_clear(query, "SCHEMA", []) do |records|

activerecord/test/cases/adapters/postgresql/composite_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def serialize(value)
106106
def setup
107107
super
108108

109-
@connection.type_map.register_type "full_address", FullAddressType.new
109+
@connection.send(:type_map).register_type "full_address", FullAddressType.new
110110
end
111111

112112
def test_column

activerecord/test/cases/adapters/postgresql/type_lookup_test.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,25 @@ class PostgresqlTypeLookupTest < ActiveRecord::PostgreSQLTestCase
88
end
99

1010
test "array delimiters are looked up correctly" do
11-
box_array = @connection.type_map.lookup(1020)
12-
int_array = @connection.type_map.lookup(1007)
11+
box_array = @connection.send(:type_map).lookup(1020)
12+
int_array = @connection.send(:type_map).lookup(1007)
1313

1414
assert_equal ";", box_array.delimiter
1515
assert_equal ",", int_array.delimiter
1616
end
1717

1818
test "array types correctly respect registration of subtypes" do
19-
int_array = @connection.type_map.lookup(1007, -1, "integer[]")
20-
bigint_array = @connection.type_map.lookup(1016, -1, "bigint[]")
19+
int_array = @connection.send(:type_map).lookup(1007, -1, "integer[]")
20+
bigint_array = @connection.send(:type_map).lookup(1016, -1, "bigint[]")
2121
big_array = [123456789123456789]
2222

2323
assert_raises(ActiveModel::RangeError) { int_array.serialize(big_array) }
2424
assert_equal "{123456789123456789}", @connection.type_cast(bigint_array.serialize(big_array))
2525
end
2626

2727
test "range types correctly respect registration of subtypes" do
28-
int_range = @connection.type_map.lookup(3904, -1, "int4range")
29-
bigint_range = @connection.type_map.lookup(3926, -1, "int8range")
28+
int_range = @connection.send(:type_map).lookup(3904, -1, "int4range")
29+
bigint_range = @connection.send(:type_map).lookup(3926, -1, "int8range")
3030
big_range = 0..123456789123456789
3131

3232
assert_raises(ActiveModel::RangeError) { int_range.serialize(big_range) }

activerecord/test/cases/connection_adapters/mysql_type_lookup_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def test_integer_types
4949
private
5050

5151
def assert_lookup_type(type, lookup)
52-
cast_type = @connection.type_map.lookup(lookup)
52+
cast_type = @connection.send(:type_map).lookup(lookup)
5353
assert_equal type, cast_type.type
5454
end
5555

activerecord/test/cases/connection_adapters/type_lookup_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def test_integer_types
8282
end
8383

8484
def test_bigint_limit
85-
cast_type = @connection.type_map.lookup("bigint")
85+
cast_type = @connection.send(:type_map).lookup("bigint")
8686
if current_adapter?(:OracleAdapter)
8787
assert_equal 19, cast_type.limit
8888
else
@@ -100,7 +100,7 @@ def test_decimal_without_scale
100100
{ decimal: %w{decimal(2) decimal(2,0) numeric(2) numeric(2,0) number(2) number(2,0)} }
101101
end.each do |expected_type, types|
102102
types.each do |type|
103-
cast_type = @connection.type_map.lookup(type)
103+
cast_type = @connection.send(:type_map).lookup(type)
104104

105105
assert_equal expected_type, cast_type.type
106106
assert_equal 2, cast_type.cast(2.1)
@@ -111,7 +111,7 @@ def test_decimal_without_scale
111111
private
112112

113113
def assert_lookup_type(type, lookup)
114-
cast_type = @connection.type_map.lookup(lookup)
114+
cast_type = @connection.send(:type_map).lookup(lookup)
115115
assert_equal type, cast_type.type
116116
end
117117
end

activerecord/test/cases/query_cache_test.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -402,10 +402,8 @@ def test_query_cached_even_when_types_are_reset
402402
# Warm the cache
403403
Task.find(1)
404404

405-
Task.connection.type_map.clear
406-
407405
# Preload the type cache again (so we don't have those queries issued during our assertions)
408-
Task.connection.send(:initialize_type_map, Task.connection.type_map)
406+
Task.connection.send(:reload_type_map)
409407

410408
# Clear places where type information is cached
411409
Task.reset_column_information

0 commit comments

Comments
 (0)