Class: DatabaseCleaner::ActiveRecord::Transaction

Inherits:
Object
  • Object
show all
Includes:
Base, Generic::Transaction
Defined in:
lib/database_cleaner/active_record/transaction.rb

Instance Attribute Summary

Attributes included from Base

#connection_hash

Instance Method Summary collapse

Methods included from Generic::Transaction

#initialize

Methods included from Base

#connection_class, #db, #db=, #load_config

Methods included from Generic::Base

#cleaning, #db, included

Instance Method Details

#cleanObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/database_cleaner/active_record/transaction.rb', line 29

def clean
  return unless connection_class.connection.open_transactions > 0

  if connection_class.connection.respond_to?(:rollback_transaction)
    connection_class.connection.rollback_transaction
  else
    connection_class.connection.rollback_db_transaction
  end

  # The below is for handling after_commit hooks.. see https://github.com/bmabey/database_cleaner/issues/99
  if connection_class.connection.respond_to?(:rollback_transaction_records, true)
    connection_class.connection.send(:rollback_transaction_records, true)
  end

  if connection_maintains_transaction_count?
    if connection_class.connection.respond_to?(:decrement_open_transactions)
      connection_class.connection.decrement_open_transactions
    else
      connection_class.__send__(:decrement_open_transactions)
    end
  end
end

#connection_maintains_transaction_count?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/database_cleaner/active_record/transaction.rb', line 52

def connection_maintains_transaction_count?
  ActiveRecord::VERSION::MAJOR < 4
end

#startObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/database_cleaner/active_record/transaction.rb', line 9

def start
  # Hack to make sure that the connection is properly setup for
  # the clean code.
  connection_class.connection.transaction{ }

  if connection_maintains_transaction_count?
    if connection_class.connection.respond_to?(:increment_open_transactions)
      connection_class.connection.increment_open_transactions
    else
      connection_class.__send__(:increment_open_transactions)
    end
  end
  if connection_class.connection.respond_to?(:begin_transaction)
    connection_class.connection.begin_transaction joinable: false
  else
    connection_class.connection.begin_db_transaction
  end
end