Sha256: f9a060ff38f2dd90f09ba3a9b4063a552da6da8be715dd7ccd51bbe298f55565

Contents?: true

Size: 1.19 KB

Versions: 48

Compression:

Stored size: 1.19 KB

Contents

require 'sequel'

module PactBroker
  module Verifications
    class Sequence < Sequel::Model(:verification_sequence_number)
      dataset_module do
        # The easiest way to implement a cross database compatible sequence.
        # Sad, I know.
        def next_val
          db.transaction do
            for_update.first
            select_all.update(value: Sequel[:value]+1)
            row = first
            if row
              row.value
            else
              # The first row should have been created in the migration, so this code
              # should only ever be executed in a test context.
              # There would be a risk of a race condition creating two rows if this
              # code executed in prod, as I don't think you can lock an empty table
              # to prevent another record being inserted.
              max_verification_number = PactBroker::Domain::Verification.max(:number)
              value = max_verification_number ? max_verification_number + 100 : 1
              insert(value: value)
              value
            end
          end
        end
      end
    end
  end
end

# Table: verification_sequence_number
# Columns:
#  value | integer | NOT NULL

Version data entries

48 entries across 48 versions & 1 rubygems

Version Path
pact_broker-2.67.0 lib/pact_broker/verifications/sequence.rb
pact_broker-2.66.0 lib/pact_broker/verifications/sequence.rb
pact_broker-2.65.0 lib/pact_broker/verifications/sequence.rb
pact_broker-2.64.0 lib/pact_broker/verifications/sequence.rb
pact_broker-2.63.0 lib/pact_broker/verifications/sequence.rb
pact_broker-2.62.0 lib/pact_broker/verifications/sequence.rb
pact_broker-2.61.0 lib/pact_broker/verifications/sequence.rb
pact_broker-2.60.1 lib/pact_broker/verifications/sequence.rb
pact_broker-2.60.0 lib/pact_broker/verifications/sequence.rb
pact_broker-2.59.2 lib/pact_broker/verifications/sequence.rb
pact_broker-2.59.1 lib/pact_broker/verifications/sequence.rb
pact_broker-2.59.0 lib/pact_broker/verifications/sequence.rb
pact_broker-2.58.3 lib/pact_broker/verifications/sequence.rb
pact_broker-2.58.2 lib/pact_broker/verifications/sequence.rb
pact_broker-2.58.0 lib/pact_broker/verifications/sequence.rb
pact_broker-2.57.0 lib/pact_broker/verifications/sequence.rb
pact_broker-2.56.1 lib/pact_broker/verifications/sequence.rb
pact_broker-2.56.0 lib/pact_broker/verifications/sequence.rb
pact_broker-2.55.0 lib/pact_broker/verifications/sequence.rb
pact_broker-2.54.0 lib/pact_broker/verifications/sequence.rb