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
97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/test_prof/before_all/adapters/active_record.rb', line 97 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
55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/test_prof/before_all/adapters/active_record.rb', line 55 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
74 75 76 |
# File 'lib/test_prof/before_all/adapters/active_record.rb', line 74 def log_pool_connection_error(pool, error) warn "Could not connect to pool #{pool.connection_class.name}. #{error.class}: #{error.message}" end |
#pool_connection_errors ⇒ Object
70 71 72 |
# File 'lib/test_prof/before_all/adapters/active_record.rb', line 70 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 38 39 40 41 42 |
# File 'lib/test_prof/before_all/adapters/active_record.rb', line 26 def subscribe! Thread.current[:before_all_subscription_count] ||= 0 Thread.current[:before_all_subscription_count] += 1 return unless Thread.current[:before_all_subscription_count] == 1 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
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/test_prof/before_all/adapters/active_record.rb', line 44 def unsubscribe! return unless Thread.current[:before_all_subscription_count] Thread.current[:before_all_subscription_count] -= 1 return unless Thread.current[:before_all_subscription_count] == 0 && Thread.current[:before_all_connection_subscriber] ActiveSupport::Notifications.unsubscribe(Thread.current[:before_all_connection_subscriber]) Thread.current[:before_all_connection_subscriber] = nil end |