Skip to content

Commit

Permalink
fix: Eagerly load pact publication fields (#536)
Browse files Browse the repository at this point in the history
Calling `to_domain` on pact publication dataset elements is causing N+1
query problem. Simple preloading fixes the issue and improves
`ProviderPactsForVerification` resource performance.
  • Loading branch information
barthez committed Feb 21, 2022
1 parent 1946612 commit c3f6993
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/pact_broker/domain/pact.rb
Expand Up @@ -41,7 +41,7 @@ def provider_name
end

def consumer
consumer_version.pacticipant
@consumer || consumer_version.pacticipant
end

def consumer_version_tag_names
Expand Down
22 changes: 19 additions & 3 deletions lib/pact_broker/pacts/pacts_for_verification_repository.rb
Expand Up @@ -19,6 +19,13 @@ class PactsForVerificationRepository
include PactBroker::Repositories::Helpers
include PactBroker::Repositories::Scopes

PUBLICATION_ASSOCIATIONS_FOR_EAGER_LOAD = [
:provider,
:consumer,
:consumer_version,
pact_version: :latest_verification
]

def find(provider_name, consumer_version_selectors)
selected_pacts = find_pacts_by_selector(provider_name, consumer_version_selectors)
selected_pacts = selected_pacts + find_pacts_for_fallback_tags(selected_pacts, provider_name, consumer_version_selectors)
Expand Down Expand Up @@ -104,7 +111,7 @@ def find_pacts_by_selector(provider_name, consumer_version_selectors)

specified_selectors_or_defaults(consumer_version_selectors, provider).flat_map do | selector |
query = scope_for(PactPublication).for_provider_and_consumer_version_selector(provider, selector)
query.all.collect do | pact_publication |
query.eager(*PUBLICATION_ASSOCIATIONS_FOR_EAGER_LOAD).all.collect do | pact_publication |
create_selected_pact(pact_publication, selector)
end
end
Expand Down Expand Up @@ -265,7 +272,12 @@ def remove_non_wip_for_branch(pact_publications_query, provider, provider_versio
log_pact_publications("Ignoring pacts successfully verified by another provider branch when not WIP", verified_by_other_branch)
end

PactPublication.subtract(pact_publications_query.all, specified_explicitly.all, verified_by_this_branch.all, verified_by_other_branch.all)
PactPublication.subtract(
pact_publications_query.eager(*PUBLICATION_ASSOCIATIONS_FOR_EAGER_LOAD).all,
specified_explicitly.all,
verified_by_this_branch.all,
verified_by_other_branch.all
)
end

def remove_non_wip_for_tag(pact_publications_query, provider, tag, specified_pact_version_shas)
Expand All @@ -279,7 +291,11 @@ def remove_non_wip_for_tag(pact_publications_query, provider, tag, specified_pac
log_pact_publications("Ignoring pacts successfully verified by another provider tag when not WIP", verified_by_another_tag)
end

PactPublication.subtract(pact_publications_query.all, specified_explicitly.all, verified_by_this_tag.all, verified_by_another_tag.all)
PactPublication.subtract(
pact_publications_query.eager(*PUBLICATION_ASSOCIATIONS_FOR_EAGER_LOAD).all,
specified_explicitly.all,
verified_by_this_tag.all,
verified_by_another_tag.all)
end

def collect_consumer_name_and_version_number(pact_publications_query)
Expand Down

0 comments on commit c3f6993

Please sign in to comment.