Class: CypressRails::ManagesTransactions

Inherits:
Object
  • Object
show all
Defined in:
lib/cypress-rails/manages_transactions.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.instanceObject



5
6
7
# File 'lib/cypress-rails/manages_transactions.rb', line 5

def self.instance
  @instance ||= new
end

Instance Method Details

#begin_transactionObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/cypress-rails/manages_transactions.rb', line 9

def begin_transaction
  @connections = gather_connections
  @connections.each do |connection|
    connection.begin_transaction joinable: false, _lazy: false
    connection.pool.lock_thread = true
  end

  # When connections are established in the future, begin a transaction too
  @connection_subscriber = ActiveSupport::Notifications.subscribe("!connection.active_record") { |_, _, _, _, payload|
    if payload.key?(:spec_name) && (spec_name = payload[:spec_name])
      setup_shared_connection_pool

      begin
        connection = ActiveRecord::Base.connection_handler.retrieve_connection(spec_name)
      rescue ActiveRecord::ConnectionNotEstablished
        connection = nil
      end

      if connection && !@connections.include?(connection)
        connection.begin_transaction joinable: false, _lazy: false
        connection.pool.lock_thread = true
        @connections << connection
      end
    end
  }

  @initializer_hooks.run(:after_transaction_start)
end

#rollback_transactionObject



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/cypress-rails/manages_transactions.rb', line 38

def rollback_transaction
  return unless @connections.present?

  ActiveSupport::Notifications.unsubscribe(@connection_subscriber) if @connection_subscriber

  @connections.each do |connection|
    connection.rollback_transaction if connection.transaction_open?
    connection.pool.lock_thread = false
  end
  @connections.clear

  ActiveRecord::Base.connection_handler.clear_active_connections!
end