Skip to content

Commit 301b30c

Browse files
committed
feat: optimise query to find latest verification for consumer version tag
1 parent 2b7f8e2 commit 301b30c

3 files changed

+52
-17
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# The latest verification id for each consumer version tag
2+
# This is not:
3+
# find latest pacticipant version with given tag
4+
# -> find the latest pact
5+
# -> find the latest verification
6+
# because the latest pacticipant version with the tag might not have a pact,
7+
# and the latest pact might not have a verification.
8+
9+
# This is:
10+
# join the tags and the pacticipant versions and the verifications and find the "latest" row
11+
12+
LATEST_VERIFICATION_IDS_FOR_CONSUMER_VERSION_TAGS_V1 = "select
13+
pv.pacticipant_id as provider_id,
14+
lpp.consumer_id,
15+
t.name as consumer_version_tag_name,
16+
max(v.id) as latest_verification_id
17+
from verifications v
18+
join latest_pact_publications_by_consumer_versions lpp
19+
on v.pact_version_id = lpp.pact_version_id
20+
join tags t
21+
on lpp.consumer_version_id = t.version_id
22+
join versions pv
23+
on v.provider_version_id = pv.id
24+
group by pv.pacticipant_id, lpp.consumer_id, t.name"
25+
26+
LATEST_VERIFICATION_IDS_FOR_CONSUMER_VERSION_TAGS_V2 = "select
27+
pv.pacticipant_id as provider_id,
28+
lpp.consumer_id,
29+
t.name as consumer_version_tag_name,
30+
max(v.id) as latest_verification_id
31+
from verifications v
32+
join latest_pact_publication_ids_for_consumer_versions lpp
33+
on v.pact_version_id = lpp.pact_version_id
34+
join tags t
35+
on lpp.consumer_version_id = t.version_id
36+
join versions pv
37+
on v.provider_version_id = pv.id
38+
group by pv.pacticipant_id, lpp.consumer_id, t.name"

db/migrations/20180523_create_latest_verifications_for_consumer_version_tags.rb

+3-17
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,8 @@
1+
require_relative '../ddl_statements'
2+
13
Sequel.migration do
24
up do
3-
# The latest verification id for each consumer version tag
4-
# This is not the latest verification for the latest pact with a given tag,
5-
# this is the latest verification for any pact with the tag
6-
create_view(:latest_verification_ids_for_consumer_version_tags,
7-
"select
8-
pv.pacticipant_id as provider_id,
9-
lpp.consumer_id,
10-
t.name as consumer_version_tag_name,
11-
max(v.id) as latest_verification_id
12-
from verifications v
13-
join latest_pact_publications_by_consumer_versions lpp
14-
on v.pact_version_id = lpp.pact_version_id
15-
join tags t
16-
on lpp.consumer_version_id = t.version_id
17-
join versions pv
18-
on v.provider_version_id = pv.id
19-
group by pv.pacticipant_id, lpp.consumer_id, t.name")
5+
create_view(:latest_verification_ids_for_consumer_version_tags, LATEST_VERIFICATION_IDS_FOR_CONSUMER_VERSION_TAGS_V1)
206

217
# The most recent verification for each consumer/consumer version tag/provider
228
latest_verifications = from(:verifications)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
require_relative '../ddl_statements'
2+
3+
Sequel.migration do
4+
up do
5+
create_or_replace_view(:latest_verification_ids_for_consumer_version_tags, LATEST_VERIFICATION_IDS_FOR_CONSUMER_VERSION_TAGS_V2)
6+
end
7+
8+
down do
9+
create_or_replace_view(:latest_verification_ids_for_consumer_version_tags, LATEST_VERIFICATION_IDS_FOR_CONSUMER_VERSION_TAGS_V1)
10+
end
11+
end

0 commit comments

Comments
 (0)