Skip to content

Commit

Permalink
fix: inefficient SQL loading latest verification for pact version
Browse files Browse the repository at this point in the history
Closes: #291
  • Loading branch information
bethesque committed Aug 8, 2019
1 parent 47c602e commit 301d9a5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
6 changes: 2 additions & 4 deletions lib/pact_broker/pacts/pact_version.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
require 'sequel'
require 'pact_broker/repositories/helpers'
require 'pact_broker/verifications/latest_verification_for_pact_version'

module PactBroker
module Pacts
class PactVersion < Sequel::Model(:pact_versions)
plugin :timestamps
one_to_many :pact_publications, reciprocal: :pact_version
one_to_many :verifications, reciprocal: :verification, order: :id, :class => "PactBroker::Domain::Verification"
one_to_one :latest_verification, class: "PactBroker::Verifications::LatestVerificationForPactVersion", key: :pact_version_id, primary_key: :id
associate(:many_to_one, :provider, class: "PactBroker::Domain::Pacticipant", key: :provider_id, primary_key: :id)
associate(:many_to_one, :consumer, class: "PactBroker::Domain::Pacticipant", key: :consumer_id, primary_key: :id)

Expand Down Expand Up @@ -40,10 +42,6 @@ def latest_pact_publication
.last
end

def latest_verification
verifications.last
end

def consumer_versions
PactBroker::Domain::Version.where(id: PactBroker::Pacts::PactPublication.select(:consumer_version_id).where(pact_version_id: id)).order(:order)
end
Expand Down
16 changes: 16 additions & 0 deletions spec/lib/pact_broker/pacts/pact_version_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,22 @@ module Pacts
expect(PactVersion.first.latest_consumer_version_number).to eq "1.0.1"
end
end

describe "#latest_verification" do
before do
td.create_pact_with_hierarchy
.create_verification
.create_verification(number: 2)
.create_verification(number: 3)
end
let(:pact_version) { PactVersion.last }

subject { pact_version.latest_verification }

it "returns the latest verification by execution date" do
expect(subject.number).to eq 3
end
end
end
end
end

0 comments on commit 301d9a5

Please sign in to comment.