Sha256: fa7e5680b4dab24160586cd0e6f391b2d427042be20d406576acd8c6d324d5ca

Contents?: true

Size: 1.44 KB

Versions: 18

Compression:

Stored size: 1.44 KB

Contents

require 'sequel'

module PactBroker
  module Versions
    class Sequence < Sequel::Model(:version_sequence_number)

      dataset_module do
        # The easiest way to implement a cross database compatible sequence.
        # Sad, I know.
        def next_val
          if PactBroker::Repositories::Helpers.postgres?
            db.execute("SELECT nextval('version_order_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, after we've truncated all the
                # tables after a test.
                # 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_version_order = PactBroker::Domain::Version.max(:order)
                value = max_version_order ? max_version_order + 100 : 1
                insert(value: value)
                value
              end
            end
          end
        end
      end
    end
  end
end

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

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
pact_broker-2.79.1 lib/pact_broker/versions/sequence.rb
pact_broker-2.79.0 lib/pact_broker/versions/sequence.rb
pact_broker-2.78.1 lib/pact_broker/versions/sequence.rb
pact_broker-2.78.0 lib/pact_broker/versions/sequence.rb
pact_broker-2.77.0 lib/pact_broker/versions/sequence.rb
pact_broker-2.76.2 lib/pact_broker/versions/sequence.rb
pact_broker-2.76.1 lib/pact_broker/versions/sequence.rb
pact_broker-2.76.0 lib/pact_broker/versions/sequence.rb
pact_broker-2.75.0 lib/pact_broker/versions/sequence.rb
pact_broker-2.74.1 lib/pact_broker/versions/sequence.rb
pact_broker-2.74.0 lib/pact_broker/versions/sequence.rb
pact_broker-2.73.0 lib/pact_broker/versions/sequence.rb
pact_broker-2.72.0 lib/pact_broker/versions/sequence.rb
pact_broker-2.71.0 lib/pact_broker/versions/sequence.rb
pact_broker-2.70.0 lib/pact_broker/versions/sequence.rb
pact_broker-2.69.0 lib/pact_broker/versions/sequence.rb
pact_broker-2.68.1 lib/pact_broker/versions/sequence.rb
pact_broker-2.68.0 lib/pact_broker/versions/sequence.rb