Sha256: 9fc75730ef7318041c4d2bacbd68e03ade25dc19248dcf937124521764003731

Contents?: true

Size: 1.98 KB

Versions: 5

Compression:

Stored size: 1.98 KB

Contents

# frozen-string-literal: true
#
# 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

#
class Sequel::Database
  module RunTransactionHooks
    # Run all savepoint and transaction after_commit hooks for the current transaction,
    # and remove the hooks after running them.
    # Options:
    # :server :: The server/shard to use.
    def run_after_commit_hooks(opts=OPTS)
      _run_transaction_hooks(:after_commit, opts)
    end

    # Run all savepoint and transaction after_rollback hooks for the current transaction,
    # and remove the hooks after running them.
    # Options:
    # :server :: The server/shard to use.
    def run_after_rollback_hooks(opts=OPTS)
      _run_transaction_hooks(:after_rollback, opts)
    end

    private

    def _run_transaction_hooks(type, opts)
      synchronize(opts[:server]) do |conn|
        unless h = _trans(conn)
          raise Error, "Cannot call run_#{type}_hooks outside of a transaction"
        end

        if hooks = h[type]
          hooks.each(&:call)
          hooks.clear
        end

        if (savepoints = h[:savepoints])
          savepoints.each do |savepoint|
            if hooks = savepoint[type]
              hooks.each(&:call)
              hooks.clear
            end
          end
        end
      end
    end
  end

  register_extension(:run_transaction_hooks, RunTransactionHooks)
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
tdiary-5.1.3 vendor/bundle/ruby/2.6.0/gems/sequel-5.32.0/lib/sequel/extensions/run_transaction_hooks.rb
sequel-5.34.0 lib/sequel/extensions/run_transaction_hooks.rb
sequel-5.33.0 lib/sequel/extensions/run_transaction_hooks.rb
tdiary-5.1.2 vendor/bundle/ruby/2.7.0/gems/sequel-5.32.0/lib/sequel/extensions/run_transaction_hooks.rb
sequel-5.32.0 lib/sequel/extensions/run_transaction_hooks.rb