Sha256: 4acce68eb9dc6653fe0cbe2b609e7ba043c7ba8e1a18d34059b7c63ba48d0461

Contents?: true

Size: 1.46 KB

Versions: 44

Compression:

Stored size: 1.46 KB

Contents

require "pact_broker/constants"
require "sequel"
require "ostruct"

module Rack
  module PactBroker
    class DatabaseTransaction

      REQUEST_METHOD = "REQUEST_METHOD".freeze
      TRANS_METHODS = %w{POST PUT PATCH DELETE}.freeze

      def initialize app, database_connection
        @app = app
        @database_connection = database_connection
        @default_database_connector = ->(&block) {
          database_connection.synchronize do
            block.call
          end
        }
      end

      def call env
        if use_transaction? env
          call_with_transaction(add_database_connector(env))
        else
          call_without_transaction(add_database_connector(env))
        end
      end

      def add_database_connector(env)
        # maintain any existing one set by previous middleware
        { "pactbroker.database_connector" => @default_database_connector }.merge(env)
      end

      def use_transaction? env
        TRANS_METHODS.include? env[REQUEST_METHOD]
      end

      def call_without_transaction env
        @app.call(env)
      end

      def call_with_transaction env
        response = nil
        @database_connection.transaction do
          response = @app.call(env)
          if response.first == 500
            raise Sequel::Rollback unless do_not_rollback?(response)
          end
        end
        response
      end

      def do_not_rollback? response
        response[1].delete(::PactBroker::DO_NOT_ROLLBACK)
      end
    end
  end
end

Version data entries

44 entries across 44 versions & 1 rubygems

Version Path
pact_broker-2.112.0 lib/rack/pact_broker/database_transaction.rb
pact_broker-2.111.0 lib/rack/pact_broker/database_transaction.rb
pact_broker-2.109.1 lib/rack/pact_broker/database_transaction.rb
pact_broker-2.109.0 lib/rack/pact_broker/database_transaction.rb
pact_broker-2.108.0 lib/rack/pact_broker/database_transaction.rb
pact_broker-2.107.1 lib/rack/pact_broker/database_transaction.rb
pact_broker-2.107.0 lib/rack/pact_broker/database_transaction.rb
pact_broker-2.107.0.beta.1 lib/rack/pact_broker/database_transaction.rb
pact_broker-2.106.0 lib/rack/pact_broker/database_transaction.rb
pact_broker-2.105.0 lib/rack/pact_broker/database_transaction.rb
pact_broker-2.104.0 lib/rack/pact_broker/database_transaction.rb
pact_broker-2.103.0 lib/rack/pact_broker/database_transaction.rb
pact_broker-2.102.2 lib/rack/pact_broker/database_transaction.rb
pact_broker-2.102.1 lib/rack/pact_broker/database_transaction.rb
pact_broker-2.102.0 lib/rack/pact_broker/database_transaction.rb
pact_broker-2.101.0 lib/rack/pact_broker/database_transaction.rb
pact_broker-2.100.0 lib/rack/pact_broker/database_transaction.rb
pact_broker-2.99.0 lib/rack/pact_broker/database_transaction.rb
pact_broker-2.98.0 lib/rack/pact_broker/database_transaction.rb
pact_broker-2.97.0 lib/rack/pact_broker/database_transaction.rb