From e6ee6ab5a017a78273f9acc3234fa153ff3ab387 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Sat, 14 Aug 2021 15:29:02 +1000 Subject: [PATCH] fix: lazy load latest verification using select max, and eager load using the skynet query --- lib/pact_broker/index/service.rb | 10 +++---- lib/pact_broker/pacts/pact_publication.rb | 4 --- lib/pact_broker/pacts/pact_version.rb | 10 ++++--- .../pact_broker/pacts/pact_version_spec.rb | 27 ++++++++++++++++++- 4 files changed, 37 insertions(+), 14 deletions(-) diff --git a/lib/pact_broker/index/service.rb b/lib/pact_broker/index/service.rb index f17bb87c9..b4d0d006f 100644 --- a/lib/pact_broker/index/service.rb +++ b/lib/pact_broker/index/service.rb @@ -30,7 +30,7 @@ def self.find_all_index_items find_index_items(page_number: 1, page_size: 100000000000) end - # rubocop: disable Metrics/CyclomaticComplexity, Metrics/MethodLength + # rubocop: disable Metrics/CyclomaticComplexity def self.find_index_items options = {} latest_verifications_for_cv_tags = latest_verifications_for_consumer_version_tags(options) latest_pp_ids = latest_pact_publication_ids @@ -44,10 +44,9 @@ def self.find_index_items options = {} pact_publications = pact_publication_query .eager(:consumer) .eager(:provider) - .eager(:pact_version) + .eager(pact_version: { latest_verification: { provider_version: [{ current_deployed_versions: :environment }, { current_supported_released_versions: :environment }, :latest_version_for_branch, { tags: :head_tag } ] } }) .eager(integration: [{latest_verification: :provider_version}, :latest_triggered_webhooks]) .eager(consumer_version: [{ current_deployed_versions: :environment }, { current_supported_released_versions: :environment }, :latest_version_for_branch, { tags: :head_tag }]) - .eager(latest_verification: { provider_version: [{ current_deployed_versions: :environment }, { current_supported_released_versions: :environment }, :latest_version_for_branch, { tags: :head_tag } ] }) .eager(:head_pact_publications_for_tags) index_items = pact_publications.all.collect do | pact_publication | @@ -72,7 +71,7 @@ def self.find_index_items options = {} Page.new(index_items, pagination_record_count) end - # rubocop: enable Metrics/CyclomaticComplexity, Metrics/MethodLength + # rubocop: enable Metrics/CyclomaticComplexity # Worst. Code. Ever. # @@ -107,9 +106,8 @@ def self.find_index_items_for_api(consumer_name: nil, provider_name: nil, page_n pact_publications = head_pact_publications(consumer_name: consumer_name, provider_name: provider_name, tags: true, page_number: page_number, page_size: page_size) .eager(:consumer) .eager(:provider) - .eager(:pact_version) + .eager(pact_version: { latest_verification: { provider_version: [{ current_deployed_versions: :environment }, { current_supported_released_versions: :environment }, :latest_version_for_branch, { tags: :head_tag }]} }) .eager(consumer_version: [{ current_deployed_versions: :environment }, { current_supported_released_versions: :environment }, :latest_version_for_branch, { tags: :head_tag }]) - .eager(latest_verification: { provider_version: [{ current_deployed_versions: :environment }, { current_supported_released_versions: :environment }, :latest_version_for_branch, { tags: :head_tag }]}) .eager(:head_pact_publications_for_tags) pact_publications.all.collect do | pact_publication | diff --git a/lib/pact_broker/pacts/pact_publication.rb b/lib/pact_broker/pacts/pact_publication.rb index 68b38f243..4c07dac1f 100644 --- a/lib/pact_broker/pacts/pact_publication.rb +++ b/lib/pact_broker/pacts/pact_publication.rb @@ -24,10 +24,6 @@ class PactPublication < Sequel::Model(:pact_publications) associate(:many_to_one, :pact_version, class: "PactBroker::Pacts::PactVersion", :key => :pact_version_id, :primary_key => :id) associate(:many_to_one, :integration, class: "PactBroker::Integrations::Integration", key: [:consumer_id, :provider_id], primary_key: [:consumer_id, :provider_id]) - one_to_one(:latest_verification, class: "PactBroker::Domain::Verification", key: :pact_version_id, primary_key: :pact_version_id) do | ds | - ds.unlimited.latest_by_pact_version - end - # TODO rename to consumer_version_tags associate(:one_to_many, :tags, :class => "PactBroker::Domain::Tag", :key => :version_id, :primary_key => :consumer_version_id) diff --git a/lib/pact_broker/pacts/pact_version.rb b/lib/pact_broker/pacts/pact_version.rb index 9681035d3..744463104 100644 --- a/lib/pact_broker/pacts/pact_version.rb +++ b/lib/pact_broker/pacts/pact_version.rb @@ -14,9 +14,13 @@ class PactVersion < Sequel::Model(:pact_versions) associate(:many_to_one, :consumer, class: "PactBroker::Domain::Pacticipant", key: :consumer_id, primary_key: :id) associate(:many_to_many, :consumer_versions, class: "PactBroker::Domain::Version", join_table: :pact_publications, left_key: :pact_version_id, right_key: :consumer_version_id, order: :order) - one_to_one(:latest_verification, class: "PactBroker::Domain::Verification", key: :pact_version_id, primary_key: :id) do | ds | - ds.unlimited.latest_by_pact_version - end + one_to_one(:latest_verification, + class: "PactBroker::Domain::Verification", + read_only: true, + dataset: lambda { PactBroker::Domain::Verification.where(id: PactBroker::Domain::Verification.select(Sequel.function(:max, :id)).where(pact_version_id: id)) }, + key: :pact_version_id, primary_key: :id, + eager_block: lambda { | ds | ds.latest_by_pact_version } + ) # do not eager load this - it won't work because of the limit(1) one_through_one(:latest_consumer_version, class: "PactBroker::Domain::Version", join_table: :pact_publications, left_key: :pact_version_id, right_key: :consumer_version_id) do | ds | diff --git a/spec/lib/pact_broker/pacts/pact_version_spec.rb b/spec/lib/pact_broker/pacts/pact_version_spec.rb index e642f2f5e..9b317ef8a 100644 --- a/spec/lib/pact_broker/pacts/pact_version_spec.rb +++ b/spec/lib/pact_broker/pacts/pact_version_spec.rb @@ -3,7 +3,6 @@ module PactBroker module Pacts describe PactVersion do - describe "pacticipant names" do subject(:pact_version) do td.create_consumer("consumer") @@ -36,6 +35,32 @@ module Pacts end end + describe "#latest_verification" do + before do + td.create_pact_with_verification("Foo", "1", "Bar", "2") + .create_verification(provider_version: "3", number: 2) + .create_pact_with_verification("NotFoo", "1", "Bar", "4") + .create_verification(provider_version: "5", number: 3) + end + + context "lazy loading" do + it "lazy loads" do + expect(PactPublication.first.pact_version.latest_verification.provider_version_number).to eq "3" + expect(PactPublication.last.pact_version.latest_verification.provider_version_number).to eq "5" + end + end + + context "eager loading" do + let(:pact_version_1) { PactVersion.eager(:latest_verification).all.first } + let(:pact_version_2) { PactVersion.eager(:latest_verification).all.last } + + it "eager loads" do + expect(pact_version_1.associations[:latest_verification].provider_version_number).to eq "3" + expect(pact_version_2.associations[:latest_verification].provider_version_number).to eq "5" + end + end + end + describe "latest_consumer_version" do before do td.create_consumer("consumer")