Skip to content

Commit

Permalink
fix: optimise query for calculating the latest overall pacts
Browse files Browse the repository at this point in the history
Co-authored-by: Dalton <[email protected]>
  • Loading branch information
dalthon and Dalton authored Dec 20, 2021
1 parent f008a6c commit f44aaa7
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions lib/pact_broker/pacts/pact_publication_dataset_module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,18 @@ def latest_by_consumer_branch
def overall_latest
self_join = {
Sequel[:pact_publications][:consumer_id] => Sequel[:pp2][:consumer_id],
Sequel[:pact_publications][:provider_id] => Sequel[:pp2][:provider_id]
Sequel[:pact_publications][:provider_id] => Sequel[:pp2][:provider_id],
Sequel[:pact_publications][:consumer_version_order] => Sequel[:pp2][:max_consumer_version_order]
}

base_query = self
base_query = base_query.select_all_qualified if no_columns_selected?

base_query.left_join(base_query.select(:consumer_id, :provider_id, :consumer_version_order), self_join, { table_alias: :pp2 } ) do
Sequel[:pp2][:consumer_version_order] > Sequel[:pact_publications][:consumer_version_order]
end
.where(Sequel[:pp2][:consumer_version_order] => nil)
base_query.join(
base_query.select_group(:consumer_id, :provider_id).select_append{ max(:consumer_version_order).as(:max_consumer_version_order) },
self_join,
table_alias: :pp2
)
.remove_overridden_revisions_from_complete_query
end

Expand Down

0 comments on commit f44aaa7

Please sign in to comment.