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

Class Method Details

.setup_fixtures(test_object) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/test_prof/before_all/adapters/active_record.rb', line 137

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



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/test_prof/before_all/adapters/active_record.rb', line 95

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
18
19
20
21
22
23
24
# File 'lib/test_prof/before_all/adapters/active_record.rb', line 12

def begin_transaction
  # don't rely on connection_handler.connection_pool_list,
  # track pinned pools ourselves
  # see https://github.com/rails/rails/pull/58489
  pinned_pools = []
  pinned_pools_stack.push(pinned_pools)
  subscribe! if pinned_pools_stack.size == 1

  ::ActiveRecord::Base.connection_handler.connection_pool_list(:writing).each do |pool|
    pin_pool!(pool)
    pinned_pools << pool
  end
end

#log_pool_connection_error(pool, error) ⇒ Object



114
115
116
# File 'lib/test_prof/before_all/adapters/active_record.rb', line 114

def log_pool_connection_error(pool, error)
  warn "Could not connect to pool #{pool.connection_class.name}. #{error.class}: #{error.message}"
end

#pin_pool!(pool) ⇒ Object



69
70
71
72
# File 'lib/test_prof/before_all/adapters/active_record.rb', line 69

def pin_pool!(pool)
  pool.pin_connection!(true)
  pool.lease_connection.begin_transaction(joinable: false, _lazy: false)
end

#pinned_pools_stack ⇒ Object



33
34
35
# File 'lib/test_prof/before_all/adapters/active_record.rb', line 33

def pinned_pools_stack
  Thread.current[:before_all_pinned_pools_stack] ||= []
end

#pool_connection_errors ⇒ Object



110
111
112
# File 'lib/test_prof/before_all/adapters/active_record.rb', line 110

def pool_connection_errors
  @pool_connection_errors ||= []
end

#rollback_transaction ⇒ Object



26
27
28
29
30
31
# File 'lib/test_prof/before_all/adapters/active_record.rb', line 26

def rollback_transaction
  pinned_pools = pinned_pools_stack.pop
  pinned_pools&.each { |pool| unpin_pool!(pool) }
ensure
  unsubscribe! if pinned_pools_stack.empty?
end

#subscribe! ⇒ Object



37
38
39
40
41
42
43
44
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 37

def subscribe!
  # notifications are not Thread-pinned, so
  # we must keep a reference to the current stack
  stack = pinned_pools_stack

  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)
    # Use the role of the established connection, not the current one
    role = payload.fetch(:role, ::ActiveRecord::Base.current_role)
    next unless connection_name

    pool = ::ActiveRecord::Base.connection_handler.retrieve_connection_pool(connection_name, role: role, shard: shard)
    next unless pool && pool.role == :writing

    pinned_pools = stack.last
    next if pinned_pools.nil? || pinned_pools.include?(pool)

    pin_pool!(pool)
    pinned_pools << pool
  end
end

#unpin_pool!(pool) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/test_prof/before_all/adapters/active_record.rb', line 74

def unpin_pool!(pool)
  connection = pool.lease_connection
  if connection.transaction_open?
    connection.rollback_transaction
  else
    warn "!!! before_all transaction has been already rollbacked and could work incorrectly"
    connection.reset!
  end
ensure
  pool.unpin_connection!
end

#unsubscribe! ⇒ Object



60
61
62
63
64
65
# File 'lib/test_prof/before_all/adapters/active_record.rb', line 60

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