Sha256: 818505b4d5c01c85f7ef4cc51038560324ec9e6ae22257bd45e39d7988fbf1c3

Contents?: true

Size: 1.99 KB

Versions: 77

Compression:

Stored size: 1.99 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 Sequel::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

77 entries across 66 versions & 2 rubygems

Version Path
sequel-5.88.0 lib/sequel/extensions/run_transaction_hooks.rb
sequel-5.87.0 lib/sequel/extensions/run_transaction_hooks.rb
sequel-5.86.0 lib/sequel/extensions/run_transaction_hooks.rb
sequel-5.85.0 lib/sequel/extensions/run_transaction_hooks.rb
sequel-5.84.0 lib/sequel/extensions/run_transaction_hooks.rb
sequel-5.83.1 lib/sequel/extensions/run_transaction_hooks.rb
sequel-5.83.0 lib/sequel/extensions/run_transaction_hooks.rb
sequel-5.82.0 lib/sequel/extensions/run_transaction_hooks.rb
sequel-5.81.0 lib/sequel/extensions/run_transaction_hooks.rb
sequel-5.80.0 lib/sequel/extensions/run_transaction_hooks.rb
sequel-5.79.0 lib/sequel/extensions/run_transaction_hooks.rb
sequel-5.78.0 lib/sequel/extensions/run_transaction_hooks.rb
sequel-5.77.0 lib/sequel/extensions/run_transaction_hooks.rb
sequel-5.76.0 lib/sequel/extensions/run_transaction_hooks.rb
sequel-5.75.0 lib/sequel/extensions/run_transaction_hooks.rb
sequel-5.74.0 lib/sequel/extensions/run_transaction_hooks.rb
sequel-5.73.0 lib/sequel/extensions/run_transaction_hooks.rb
sequel-5.72.0 lib/sequel/extensions/run_transaction_hooks.rb
sequel-5.71.0 lib/sequel/extensions/run_transaction_hooks.rb
sequel-5.70.0 lib/sequel/extensions/run_transaction_hooks.rb