Skip to content

Commit

Permalink
feat: optimise query to find latest verification for pact
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Oct 20, 2020
1 parent 3628340 commit db17ef5
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions lib/pact_broker/verifications/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,27 @@ def find_latest_verifications_for_consumer_version consumer_name, consumer_versi
# belonging to the version with the largest consumer_version_order.

def find_latest_verification_for consumer_name, provider_name, consumer_version_tag = nil
query = LatestVerificationForPactVersion
consumer = pacticipant_repository.find_by_name!(consumer_name)
provider = pacticipant_repository.find_by_name!(provider_name)
join_cols = {
Sequel[:lp][:pact_version_id] => Sequel[:verifications][:pact_version_id],
Sequel[:lp][:consumer_id] => consumer.id,
Sequel[:lp][:provider_id] => provider.id
}
query = PactBroker::Domain::Verification
.select_all_qualified
.join(:all_pact_publications, pact_version_id: :pact_version_id)
.consumer(consumer_name)
.provider(provider_name)
.join(:latest_verification_ids_for_pact_versions, { Sequel[:verifications][:id] => Sequel[:lv][:latest_verification_id] }, { table_alias: :lv })
.join(:latest_pact_publication_ids_for_consumer_versions, join_cols, { table_alias: :lp })
.join(:versions, { Sequel[:cv][:id] => Sequel[:lp][:consumer_version_id] }, { table_alias: :cv })
if consumer_version_tag == :untagged
query = query.untagged
query = query.left_outer_join(:tags, { Sequel[:cv][:id] => Sequel[:tags][:version_id] })
.where(Sequel[:tags][:name] => nil)
elsif consumer_version_tag
query = query.tag(consumer_version_tag)
query = query.join(:tags, { Sequel[:cv][:id] => Sequel[:tags][:version_id], Sequel[:tags][:name] => consumer_version_tag })
end
query.reverse_order(
Sequel[:all_pact_publications][:consumer_version_order],
Sequel[:all_pact_publications][:revision_number],
Sequel[LatestVerificationForPactVersion.table_name][:number]
Sequel[:cv][:order],
Sequel[:verifications][:number]
).limit(1).single_record
end

Expand Down

0 comments on commit db17ef5

Please sign in to comment.