Sha256: c96c6b17ecbe427033633653e654ae29bcd06a8eb5cd83cdfaa1f9324738c97d

Contents?: true

Size: 1.72 KB

Versions: 1

Compression:

Stored size: 1.72 KB

Contents

require 'database_cleaner/active_record/base'
require 'database_cleaner/generic/transaction'

module DatabaseCleaner::ActiveRecord
  class Transaction
    include ::DatabaseCleaner::ActiveRecord::Base
    include ::DatabaseCleaner::Generic::Transaction

    def start
      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
      else
        connection_class.connection.begin_db_transaction
      end
    end


    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)
        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

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

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
database_cleaner-1.0.0.RC1 lib/database_cleaner/active_record/transaction.rb