run_transaction_hooks.rb

lib/sequel/extensions/run_transaction_hooks.rb
Last Update: 2020-04-10 15:48:07 -0700

The run_transaction_hooks extension allows for running after_commit or after_rollback extensions before commit or rollback. It then removes the hook after running it, so it will not be run twice.

This extension should only be used in transactional tests where the transaction always rolls back, to test the behavior of the after_commit and after_rollback hooks. Any other usage is probably a bad idea.

Example:

DB.extension :run_transaction_hooks
x = 1
DB.transaction(rollback: :always) do
  DB.after_rollback{x = 3}
  DB.after_commit{x = 2}

  x # => 1
  DB.run_after_rollback_hooks
  x # => 3
  DB.run_after_commit_hooks
  x # => 2
end
x # => 2