@@ -103,6 +103,7 @@ def is_a_row_for_this_integration_required?(specified_pacticipant_names, consume
103
103
specified_pacticipant_names . include? ( consumer_name )
104
104
end
105
105
106
+ # It would be nicer to do this in the SQL, but it requires time that I don't have at the moment
106
107
def apply_latestby options , selectors , lines
107
108
return lines unless options [ :latestby ]
108
109
group_by_columns = case options [ :latestby ]
@@ -119,6 +120,7 @@ def apply_latestby options, selectors, lines
119
120
. flatten
120
121
end
121
122
123
+ # It would be nicer to do this in the SQL, but it requires time that I don't have at the moment
122
124
def remove_overwritten_revisions lines
123
125
latest_revisions_keys = { }
124
126
latest_revisions = [ ]
@@ -143,7 +145,7 @@ def query_matrix selectors, options
143
145
end
144
146
145
147
def resolve_selectors ( specified_selectors , options )
146
- resolved_specified_selectors = resolve_versions_and_add_ids ( specified_selectors , :specified )
148
+ resolved_specified_selectors = resolve_versions_and_add_ids ( specified_selectors , :specified , options [ :latestby ] )
147
149
if options [ :latest ] || options [ :tag ]
148
150
add_inferred_selectors ( resolved_specified_selectors , options )
149
151
else
@@ -152,19 +154,19 @@ def resolve_selectors(specified_selectors, options)
152
154
end
153
155
154
156
# Find the version number for selectors with the latest and/or tag specified
155
- def resolve_versions_and_add_ids ( selectors , selector_type )
157
+ def resolve_versions_and_add_ids ( selectors , selector_type , latestby )
156
158
selectors . collect do | selector |
157
159
pacticipant = PactBroker ::Domain ::Pacticipant . find ( name : selector [ :pacticipant_name ] )
158
160
versions = find_versions_for_selector ( selector )
159
- build_selectors_for_pacticipant_and_versions ( pacticipant , versions , selector , selector_type )
161
+ build_selectors_for_pacticipant_and_versions ( pacticipant , versions , selector , selector_type , latestby )
160
162
end . flatten
161
163
end
162
164
163
- def build_selectors_for_pacticipant_and_versions ( pacticipant , versions , original_selector , selector_type )
165
+ def build_selectors_for_pacticipant_and_versions ( pacticipant , versions , original_selector , selector_type , latestby )
164
166
if versions
165
167
versions . collect do | version |
166
168
if version
167
- selector_for_version ( pacticipant , version , original_selector , selector_type )
169
+ selector_for_version ( pacticipant , version , original_selector , selector_type , latestby )
168
170
else
169
171
selector_for_non_existing_version ( pacticipant , original_selector , selector_type )
170
172
end
@@ -192,23 +194,6 @@ def find_versions_for_selector(selector)
192
194
end
193
195
end
194
196
195
- def add_ids_to_selector ( selector )
196
- if selector [ :pacticipant_name ]
197
- pacticipant = PactBroker ::Domain ::Pacticipant . find ( name : selector [ :pacticipant_name ] )
198
- selector [ :pacticipant_id ] = pacticipant ? pacticipant . id : nil
199
- end
200
-
201
- if selector [ :pacticipant_name ] && selector [ :pacticipant_version_number ] && !selector [ :pacticipant_version_id ]
202
- version = version_repository . find_by_pacticipant_name_and_number ( selector [ :pacticipant_name ] , selector [ :pacticipant_version_number ] )
203
- selector [ :pacticipant_version_id ] = version ? version . id : nil
204
- end
205
-
206
- if !selector . key? ( :pacticipant_version_id )
207
- selector [ :pacticipant_version_id ] = nil
208
- end
209
- selector
210
- end
211
-
212
197
# When only one selector is specified, (eg. checking to see if Foo version 2 can be deployed to prod),
213
198
# need to look up all integrated pacticipants, and determine their relevant (eg. latest prod) versions
214
199
def add_inferred_selectors ( resolved_specified_selectors , options )
@@ -228,7 +213,7 @@ def build_inferred_selectors(inferred_pacticipant_names, options)
228
213
selector [ :latest ] = options [ :latest ] if options [ :latest ]
229
214
selector
230
215
end
231
- resolve_versions_and_add_ids ( selectors , :inferred )
216
+ resolve_versions_and_add_ids ( selectors , :inferred , options [ :latestby ] )
232
217
end
233
218
234
219
def all_pacticipant_names_in_specified_matrix ( selectors )
@@ -242,8 +227,27 @@ def selector_for_non_existing_version(pacticipant, original_selector, selector_t
242
227
ResolvedSelector . for_pacticipant_and_non_existing_version ( pacticipant , original_selector , selector_type )
243
228
end
244
229
245
- def selector_for_version ( pacticipant , version , original_selector , selector_type )
246
- ResolvedSelector . for_pacticipant_and_version ( pacticipant , version , original_selector , selector_type )
230
+ def selector_for_version ( pacticipant , version , original_selector , selector_type , latestby )
231
+ pact_publication_ids , verification_ids = nil , nil
232
+
233
+ # Querying for the "pre-filtered" pact publications and verifications directly, rather than just using the consumer
234
+ # and provider versions allows us to remove the "overwritten" pact revisions and verifications from the
235
+ # matrix result set, making the final matrix query more efficient and stopping the query limit from
236
+ # removing relevant data (eg. https://github.com/pact-foundation/pact_broker-client/issues/53)
237
+ if latestby
238
+ pact_publication_ids = PactBroker ::Pacts ::LatestPactPublicationsByConsumerVersion
239
+ . select ( :id )
240
+ . where ( consumer_version_id : version . id )
241
+ . collect { |pact_publication | pact_publication [ :id ] }
242
+
243
+ verification_ids = PactBroker ::Verifications ::LatestVerificationIdForPactVersionAndProviderVersion
244
+ . select ( :verification_id )
245
+ . distinct
246
+ . where ( provider_version_id : version . id )
247
+ . collect { |pact_publication | pact_publication [ :verification_id ] }
248
+ end
249
+
250
+ ResolvedSelector . for_pacticipant_and_version ( pacticipant , version , pact_publication_ids , verification_ids , original_selector , selector_type )
247
251
end
248
252
249
253
def selector_without_version ( pacticipant , selector_type )
0 commit comments