Skip to content

Commit fbfb52a

Browse files
committed
Lazy load type mappings
This appears to fix issue #2256. I believe it should have the same memory implications as the intention in #2199. The type maps are loaded once when they are first called. Only one instance of the mapping is ever created. Fortunately this was enough to solve the problem, but if there were a need, this could easily be worked into the clear_cache! API to reset it at a later point in the application just by setting the @type_map to nil.
1 parent 04841ad commit fbfb52a

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

lib/active_record/connection_adapters/oracle_enhanced_adapter.rb

+15-3
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,11 @@ def reconnect! # :nodoc:
459459
@logger.warn "#{adapter_name} automatic reconnection failed: #{e.message}" if @logger
460460
end
461461

462+
def clear_cache!
463+
self.class.clear_type_map!
464+
super
465+
end
466+
462467
def reset!
463468
clear_cache!
464469
super
@@ -697,6 +702,15 @@ def check_version
697702
end
698703

699704
class << self
705+
def type_map
706+
@type_map ||= Type::TypeMap.new.tap { |m| initialize_type_map(m) }
707+
@type_map
708+
end
709+
710+
def clear_type_map!
711+
@type_map = nil
712+
end
713+
700714
private
701715
def initialize_type_map(m)
702716
super
@@ -730,10 +744,8 @@ def initialize_type_map(m)
730744
end
731745
end
732746

733-
TYPE_MAP = Type::TypeMap.new.tap { |m| initialize_type_map(m) }
734-
735747
def type_map
736-
TYPE_MAP
748+
self.class.type_map
737749
end
738750

739751
def extract_value_from_default(default)

spec/active_record/oracle_enhanced/type/integer_spec.rb

+8
Original file line numberDiff line numberDiff line change
@@ -87,5 +87,13 @@ class ::Test2Employee < ActiveRecord::Base
8787
create_employee2
8888
expect(@employee2.is_manager).to be_a(Integer)
8989
end
90+
91+
it "should return Integer value from NUMBER(1) column if emulate_booleans is set to false" do
92+
ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.emulate_booleans = false
93+
ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.clear_type_map!
94+
ActiveRecord::Base.clear_cache!
95+
create_employee2
96+
expect(@employee2.is_manager).to be_a(Integer)
97+
end
9098
end
9199
end

0 commit comments

Comments
 (0)