Skip to content

Commit 44e78ad

Browse files
committed
feat(matrix): optimise query to determine integrations, again
1 parent dca0ad4 commit 44e78ad

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

lib/pact_broker/matrix/quick_row.rb

+36-4
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,15 @@ class QuickRow < Sequel::Model(Sequel.as(:latest_pact_publication_ids_for_consum
8484
select *SELECT_PACTICIPANT_IDS_ARGS
8585

8686
def distinct_integrations selectors
87-
select_pacticipant_ids
88-
.distinct
89-
.matching_selectors(selectors)
90-
.from_self(alias: :pacticipant_ids)
87+
query = if selectors.size == 1
88+
pacticipant_ids_matching_one_selector_optimised(selectors)
89+
else
90+
select_pacticipant_ids
91+
.distinct
92+
.matching_multiple_selectors(selectors)
93+
end
94+
95+
query.from_self(alias: :pacticipant_ids)
9196
.select(
9297
:consumer_id,
9398
Sequel[:c][:name].as(:consumer_name),
@@ -144,6 +149,29 @@ def matching_one_selector(selectors)
144149
}
145150
end
146151

152+
def pacticipant_ids_matching_one_selector_optimised(selectors)
153+
query_ids = QueryIds.from_selectors(selectors)
154+
distinct_pacticipant_ids_where_consumer_or_consumer_version_matches(query_ids)
155+
.union(distinct_pacticipant_ids_where_provider_or_provider_version_matches(query_ids))
156+
end
157+
158+
def distinct_pacticipant_ids_where_consumer_or_consumer_version_matches(query_ids)
159+
select_pacticipant_ids
160+
.distinct
161+
.where {
162+
QueryBuilder.consumer_or_consumer_version_matches(query_ids, :p)
163+
}
164+
end
165+
166+
def distinct_pacticipant_ids_where_provider_or_provider_version_matches(query_ids)
167+
select_pacticipant_ids
168+
.distinct
169+
.inner_join_verifications
170+
.where {
171+
QueryBuilder.provider_or_provider_version_matches(query_ids, :v)
172+
}
173+
end
174+
147175
# When the user has specified multiple selectors, we only want to join the verifications for
148176
# the specified selectors. This is because of the behaviour of the left outer join.
149177
# Imagine a pact has been verified by a provider version that was NOT specified in the selectors.
@@ -215,6 +243,10 @@ def join_provider_versions
215243
def join_verifications
216244
left_outer_join(LV, LP_LV_JOIN, { table_alias: :v } )
217245
end
246+
247+
def inner_join_verifications
248+
join(LV, LP_LV_JOIN, { table_alias: :v } )
249+
end
218250
end # end dataset_module
219251

220252
def success

0 commit comments

Comments
 (0)