Skip to content

Commit

Permalink
fix: improve performance of query for latest verification for pact_ve…
Browse files Browse the repository at this point in the history
…rsion
  • Loading branch information
bethesque committed Aug 14, 2021
1 parent 8d9545e commit d63081d
Showing 1 changed file with 3 additions and 21 deletions.
24 changes: 3 additions & 21 deletions lib/pact_broker/domain/verification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
require "pact_broker/pacts/content"
require "sequel/extensions/symbol_aref_refinement"


module PactBroker
module Domain
class Verification < Sequel::Model
Expand Down Expand Up @@ -45,27 +44,10 @@ def latest_by_pact_version
base_query = base_query.select_all_qualified
end

base_query.left_join(base_query.select(Sequel[:verifications][:id], Sequel[:verifications][:pact_version_id]), base_join, { table_alias: :v2 }) do
Sequel[:v2][:id] > Sequel[:verifications][:id]
base_query.left_join(:latest_verification_id_for_pact_version_and_provider_version, base_join, { table_alias: :v2 }) do
Sequel[:v2][:verification_id] > Sequel[:verifications][:id]
end
.where(Sequel[:v2][:id] => nil)
end

def latest_verification_ids_for_all_consumer_version_tags
verif_pact_join = { Sequel[:verifications][:pact_version_id] => Sequel[:lpp][:pact_version_id] }
tag_join = { Sequel[:lpp][:consumer_version_id] => Sequel[:cvt][:version_id] }
verisons_join = { Sequel[:v][:provider_version_id] => Sequel[:pv][:id] }

db[:verifications]
.select_group(
Sequel[:pv][:pacticipant_id].as(:provider_id),
Sequel[:lpp][:consumer_id],
Sequel[:cvt][:name].as(:consumer_version_tag_name)
)
.select_append{ max(verifications[id]).as(latest_verification_id) }
.join(:latest_pact_publication_ids_for_consumer_versions, verif_pact_join, { table_alias: :lpp } )
.join(:tags, tag_join, { table_alias: :cvt })
.join(:versions, verisons_join, { table_alias: :pv })
.where(Sequel[:v2][:verification_id] => nil)
end

# Do not use this query. It performs worse than the view.
Expand Down

0 comments on commit d63081d

Please sign in to comment.