From 301d9a58e9087c45bb83c9017b4966fbc4dc22b0 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Thu, 8 Aug 2019 16:51:30 +1000 Subject: [PATCH] fix: inefficient SQL loading latest verification for pact version Closes: https://github.com/pact-foundation/pact_broker/issues/291 --- lib/pact_broker/pacts/pact_version.rb | 6 ++---- spec/lib/pact_broker/pacts/pact_version_spec.rb | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/pact_broker/pacts/pact_version.rb b/lib/pact_broker/pacts/pact_version.rb index 197f61f62..3ac5c506d 100644 --- a/lib/pact_broker/pacts/pact_version.rb +++ b/lib/pact_broker/pacts/pact_version.rb @@ -1,5 +1,6 @@ require 'sequel' require 'pact_broker/repositories/helpers' +require 'pact_broker/verifications/latest_verification_for_pact_version' module PactBroker module Pacts @@ -7,6 +8,7 @@ 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) @@ -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 diff --git a/spec/lib/pact_broker/pacts/pact_version_spec.rb b/spec/lib/pact_broker/pacts/pact_version_spec.rb index 2dd57b797..11ead67c7 100644 --- a/spec/lib/pact_broker/pacts/pact_version_spec.rb +++ b/spec/lib/pact_broker/pacts/pact_version_spec.rb @@ -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