Skip to content

Commit

Permalink
chore: update lazy loading logic
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Mar 21, 2022
1 parent 8a38092 commit a49adce
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 31 deletions.
2 changes: 1 addition & 1 deletion lib/pact_broker/index/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down
26 changes: 26 additions & 0 deletions lib/pact_broker/pacts/lazy_loaders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
32 changes: 4 additions & 28 deletions lib/pact_broker/pacts/pact_publication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions lib/pact_broker/pacts/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/pact_broker/repositories/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down

0 comments on commit a49adce

Please sign in to comment.