Sha256: 20f230d4513a9b1cef4ea7dc02a7ed90b9eb4eaecfff139297c803d29b2d587c

Contents?: true

Size: 1.38 KB

Versions: 2

Compression:

Stored size: 1.38 KB

Contents

module Journaled
  module Connection
    class << self
      def available?
        Journaled.transactional_batching_enabled? && transaction_open?
      end

      def stage!(event)
        raise TransactionSafetyError, <<~MSG unless available?
          Transaction not available! By default, journaled event batching requires an open database transaction.
        MSG

        connection.current_transaction._journaled_staged_events << event
      end

      private

      def transaction_open?
        connection.transaction_open?
      end

      def connection
        if Journaled.queue_adapter.in? %w(delayed delayed_job)
          Delayed::Job.connection
        elsif Journaled.queue_adapter == 'good_job'
          GoodJob::BaseRecord.connection
        elsif Journaled.queue_adapter == 'que'
          Que::ActiveRecord::Model.connection
        elsif Journaled.queue_adapter == 'test' && Rails.env.test?
          ActiveRecord::Base.connection
        else
          raise "Unsupported adapter: #{Journaled.queue_adapter}"
        end
      end
    end

    module TestOnlyBehaviors
      def transaction_open?
        # Transactional fixtures wrap all tests in an outer, non-joinable transaction:
        super && (connection.open_transactions > 1 || connection.current_transaction.joinable?)
      end
    end

    class << self
      prepend TestOnlyBehaviors if Rails.env.test?
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
journaled-5.3.1 lib/journaled/connection.rb
journaled-5.3.0 lib/journaled/connection.rb