Class: Sequent::Core::Transactions::ActiveRecordTransactionProvider

Inherits:
Object
  • Object
show all
Defined in:
lib/sequent/core/transactions/active_record_transaction_provider.rb

Overview

Always require a new transaction.

Read: api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html

Without this change, there is a potential bug:

“‘ruby ActiveRecord::Base.transaction do

Sequent.configuration.command_service.execute_commands command

end

on Command do

do.some.things
fail ActiveRecord::Rollback

end “‘

In this example, you might be surprised to find that ‘do.some.things` does not get rolled back! This is because AR doesn’t automatically make a “savepoint” for us when we call ‘.transaction` in a nested manner. In order to enable this behaviour, we have to call `.transaction` like this: `.transaction(requires_new: true)`.

Instance Method Summary collapse

Instance Method Details

#after_commit(&block) ⇒ Object



39
40
41
# File 'lib/sequent/core/transactions/active_record_transaction_provider.rb', line 39

def after_commit(&block)
  after_commit_queue << block
end

#transactional(&block) ⇒ Object



32
33
34
35
36
37
# File 'lib/sequent/core/transactions/active_record_transaction_provider.rb', line 32

def transactional(&block)
  Sequent::ApplicationRecord.transaction(requires_new: true, &block)
  after_commit_queue.pop.call until after_commit_queue.empty?
ensure
  clear_after_commit_queue
end