class PactBroker::Verifications::Sequence

Public Instance Methods

next_val() click to toggle source

The easiest way to implement a cross database compatible sequence. Sad, I know.

# File lib/pact_broker/verifications/sequence.rb, line 7
def next_val
  if PactBroker::Dataset::Helpers.postgres?
    db.execute("SELECT nextval('verification_number_sequence') as val") { |v| v.first["val"].to_i }
  else
    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