Commit 8ba2a598 authored by Kamil Trzciński's avatar Kamil Trzciński

Improve `BeforeAllAdapter` support

Usage of `connection_handlers` is not always correct
as we might end-up with misaligned transactions.

The usage of `AR::Base.establish_connection` is bad design
as it break `before_all_adapter` and is always a misaligned transaction.

The cases where it will break:
- `.establish_connection`
- `.pool.disconnect!`
- `.release_connection`
parent 905e6500
# frozen_string_literal: true # frozen_string_literal: true
class BeforeAllAdapter # rubocop:disable Gitlab/NamespacedClass class BeforeAllAdapter # rubocop:disable Gitlab/NamespacedClass
def self.all_connection_pools def self.all_connection_classes
::ActiveRecord::Base.connection_handler.all_connection_pools @all_connection_classes ||= [ActiveRecord::Base] + ActiveRecord::Base.descendants.select(&:connection_class?) # rubocop: disable Database/MultipleDatabases
end end
def self.begin_transaction def self.begin_transaction
self.all_connection_pools.each do |connection_pool| self.all_connection_classes.each do |connection_class|
connection_pool.connection.begin_transaction(joinable: false) connection_class.connection.begin_transaction(joinable: false)
end end
end end
def self.rollback_transaction def self.rollback_transaction
self.all_connection_pools.each do |connection_pool| self.all_connection_classes.each do |connection_class|
if connection_pool.connection.open_transactions.zero? if connection_class.connection.open_transactions.zero?
warn "!!! before_all transaction has been already rollbacked and " \ warn "!!! before_all transaction has been already rollbacked and " \
"could work incorrectly" "could work incorrectly"
next next
end end
connection_pool.connection.rollback_transaction connection_class.connection.rollback_transaction
end end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment