Module: TestProf::BeforeAll::Adapters::ActiveRecord
- Defined in:
- lib/test_prof/before_all/adapters/active_record.rb
Overview
ActiveRecord adapter for ‘before_all`
Constant Summary collapse
- POOL_ARGS =
((::ActiveRecord::VERSION::MAJOR > 6) ? [:writing] : []).freeze
Class Method Summary collapse
Instance Method Summary collapse
- #all_connections ⇒ Object
- #begin_transaction ⇒ Object
- #log_pool_connection_error(pool, error) ⇒ Object
- #pool_connection_errors ⇒ Object
- #rollback_transaction ⇒ Object
- #subscribe! ⇒ Object
- #unsubscribe! ⇒ Object
Class Method Details
.setup_fixtures(test_object) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/test_prof/before_all/adapters/active_record.rb', line 87 def setup_fixtures(test_object) test_object.instance_eval do @@already_loaded_fixtures ||= {} @fixture_cache ||= {} config = ::ActiveRecord::Base if @@already_loaded_fixtures[self.class] @loaded_fixtures = @@already_loaded_fixtures[self.class] else @loaded_fixtures = load_fixtures(config) @@already_loaded_fixtures[self.class] = @loaded_fixtures end end end |
Instance Method Details
#all_connections ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/test_prof/before_all/adapters/active_record.rb', line 45 def all_connections @all_connections ||= if ::ActiveRecord::Base.respond_to? :connects_to ::ActiveRecord::Base.connection_handler.connection_pool_list(*POOL_ARGS).filter_map { |pool| begin pool.connection rescue *pool_connection_errors => error log_pool_connection_error(pool, error) nil end } else Array.wrap(::ActiveRecord::Base.connection) end end |
#begin_transaction ⇒ Object
12 13 14 15 16 17 |
# File 'lib/test_prof/before_all/adapters/active_record.rb', line 12 def begin_transaction subscribe! ::ActiveRecord::Base.connection_handler.connection_pool_list(:writing).each do |pool| pool.pin_connection!(true) end end |
#log_pool_connection_error(pool, error) ⇒ Object
64 65 66 |
# File 'lib/test_prof/before_all/adapters/active_record.rb', line 64 def log_pool_connection_error(pool, error) warn "Could not connect to pool #{pool.connection_class.name}. #{error.class}: #{error.}" end |
#pool_connection_errors ⇒ Object
60 61 62 |
# File 'lib/test_prof/before_all/adapters/active_record.rb', line 60 def pool_connection_errors @pool_connection_errors ||= [] end |
#rollback_transaction ⇒ Object
19 20 21 22 23 24 |
# File 'lib/test_prof/before_all/adapters/active_record.rb', line 19 def rollback_transaction ::ActiveRecord::Base.connection_handler.connection_pool_list(:writing).each do |pool| pool.unpin_connection! end unsubscribe! end |
#subscribe! ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/test_prof/before_all/adapters/active_record.rb', line 26 def subscribe! Thread.current[:before_all_connection_subscriber] = ActiveSupport::Notifications.subscribe("!connection.active_record") do |_, _, _, _, payload| connection_name = payload[:connection_name] if payload.key?(:connection_name) shard = payload[:shard] if payload.key?(:shard) next unless connection_name pool = ::ActiveRecord::Base.connection_handler.retrieve_connection_pool(connection_name, shard: shard) next unless pool && pool.role == :writing pool.pin_connection!(true) end end |
#unsubscribe! ⇒ Object
39 40 41 42 43 |
# File 'lib/test_prof/before_all/adapters/active_record.rb', line 39 def unsubscribe! return unless Thread.current[:before_all_connection_subscriber] ActiveSupport::Notifications.unsubscribe(Thread.current[:before_all_connection_subscriber]) Thread.current[:before_all_connection_subscriber] = nil end |