From a49adcebfdf9ad16fe46ca26b75f31a6ce8ea282 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Mon, 21 Mar 2022 15:51:44 +1100 Subject: [PATCH] chore: update lazy loading logic --- lib/pact_broker/index/service.rb | 2 +- lib/pact_broker/pacts/lazy_loaders.rb | 26 ++++++++++++++++++ lib/pact_broker/pacts/pact_publication.rb | 32 +++-------------------- lib/pact_broker/pacts/repository.rb | 4 +-- lib/pact_broker/repositories/helpers.rb | 4 +++ 5 files changed, 37 insertions(+), 31 deletions(-) diff --git a/lib/pact_broker/index/service.rb b/lib/pact_broker/index/service.rb index 7cdfbbe0a..dccaeb481 100644 --- a/lib/pact_broker/index/service.rb +++ b/lib/pact_broker/index/service.rb @@ -108,7 +108,7 @@ def self.find_index_items options = {} # rubocop: disable Metrics/CyclomaticComplexity def self.latest_verification_for_pseudo_branch(pact_publication, is_overall_latest, latest_verifications_for_cv_tags, tags_option, options) if options[:view] == "branch" || (options[:view] == "all" && pact_publication.consumer_version.branch_heads.any?) - pact_publication.latest_verification || pact_publication.latest_verification_for_consumer_branches(forbid_lazy_load: false) + pact_publication.latest_verification || pact_publication.latest_verification_for_consumer_branches elsif tags_option == true latest_verifications_for_cv_tags .select{ | v | v.consumer_id == pact_publication.consumer_id && v.provider_id == pact_publication.provider_id && pact_publication.head_pact_tags.collect(&:name).include?(v.consumer_version_tag_name) } diff --git a/lib/pact_broker/pacts/lazy_loaders.rb b/lib/pact_broker/pacts/lazy_loaders.rb index 626da6228..b1e873f2f 100644 --- a/lib/pact_broker/pacts/lazy_loaders.rb +++ b/lib/pact_broker/pacts/lazy_loaders.rb @@ -9,6 +9,32 @@ module LazyLoaders .latest_for_consumer_tag(consumer_version_tag_names) .from_self.order_by(:tag_name) } + + LATEST_VERIFICATION_FOR_CONSUMER_BRANCHES = lambda { + bv_pp_join = { + Sequel[:branch_versions][:version_id] => Sequel[:pact_publications][:consumer_version_id], + Sequel[:pact_publications][:provider_id] => provider_id + } + + verifications_join = { + Sequel[:verifications][:pact_version_id] => Sequel[:pact_publications][:pact_version_id] + } + + branch_ids = PactBroker::Versions::BranchVersion + .select(:branch_id) + .where(version_id: consumer_version_id) + + + latest_verification_id = PactBroker::Versions::BranchVersion + .select(Sequel[:verifications][:id]) + .where(Sequel[:branch_versions][:branch_id] => branch_ids) + .join(:pact_publications, bv_pp_join) + .join(:verifications, verifications_join) + .order(Sequel.desc(Sequel[:verifications][:id])) + .limit(1) + + PactBroker::Domain::Verification.where(id: latest_verification_id) + } end end end diff --git a/lib/pact_broker/pacts/pact_publication.rb b/lib/pact_broker/pacts/pact_publication.rb index 098f76e68..2d37535d5 100644 --- a/lib/pact_broker/pacts/pact_publication.rb +++ b/lib/pact_broker/pacts/pact_publication.rb @@ -34,43 +34,19 @@ class PactPublication < Sequel::Model(:pact_publications) read_only: true, key: :id, primary_key: :id, - dataset: lambda { - - bv_pp_join = { - Sequel[:branch_versions][:version_id] => Sequel[:pact_publications][:consumer_version_id], - Sequel[:pact_publications][:provider_id] => provider_id - } - - verifications_join = { - Sequel[:verifications][:pact_version_id] => Sequel[:pact_publications][:pact_version_id] - } - - branch_ids = PactBroker::Versions::BranchVersion - .select(:branch_id) - .where(version_id: consumer_version_id) - - - latest_verification_id = PactBroker::Versions::BranchVersion - .select(Sequel[:verifications][:id]) - .where(Sequel[:branch_versions][:branch_id] => branch_ids) - .join(:pact_publications, bv_pp_join) - .join(:verifications, verifications_join) - .order(Sequel.desc(Sequel[:verifications][:id])) - .limit(1) - - PactBroker::Domain::Verification.where(id: latest_verification_id) - }, + forbid_lazy_load: false, + dataset: PactBroker::Pacts::LazyLoaders::LATEST_VERIFICATION_FOR_CONSUMER_BRANCHES, eager_loader: proc do | _ | raise NotImplementedError end ) - one_to_many(:head_pact_publications_for_tags, class: PactPublication, read_only: true, dataset: PactBroker::Pacts::LazyLoaders::HEAD_PACT_PUBLICATIONS_FOR_TAGS, - eager_loader: PactBroker::Pacts::EagerLoaders::HeadPactPublicationsForTags + eager_loader: PactBroker::Pacts::EagerLoaders::HeadPactPublicationsForTags, + forbid_lazy_load: false ) plugin :upsert, identifying_columns: [:consumer_version_id, :provider_id, :revision_number] diff --git a/lib/pact_broker/pacts/repository.rb b/lib/pact_broker/pacts/repository.rb index eead2b641..74f91db4b 100644 --- a/lib/pact_broker/pacts/repository.rb +++ b/lib/pact_broker/pacts/repository.rb @@ -250,14 +250,14 @@ def find_pact consumer_name, consumer_version_number, provider_name, pact_versio if consumer_version_number && !pact_version_sha pact_publication_by_consumer_version .eager(:tags) + .all_allowing_lazy_load .collect(&:to_domain_with_content).first elsif pact_version_sha && !consumer_version_number latest_pact_publication_by_sha .eager(:tags) .collect(&:to_domain_with_content).first elsif consumer_version_number && pact_version_sha - pact_publication = pact_publication_by_consumer_version.all.first - pact_publication&.allow_lazy_load + pact_publication = pact_publication_by_consumer_version.all_allowing_lazy_load.first if pact_publication && pact_publication.pact_version.sha == pact_version_sha pact_publication.tags pact_publication.to_domain_with_content diff --git a/lib/pact_broker/repositories/helpers.rb b/lib/pact_broker/repositories/helpers.rb index 6fba9a7f4..de5b0d710 100644 --- a/lib/pact_broker/repositories/helpers.rb +++ b/lib/pact_broker/repositories/helpers.rb @@ -10,6 +10,10 @@ def all_forbidding_lazy_load all.each{ | row | row.forbid_lazy_load if row.respond_to?(:forbid_lazy_load) } end + def all_allowing_lazy_load + all.each{ | row | row.allow_lazy_load if row.respond_to?(:allow_lazy_load) } + end + def name_like column_name, value if PactBroker.configuration.use_case_sensitive_resource_names if mysql?