Skip to content

Commit

Permalink
feat: optimise query to find latest verification for consumer version…
Browse files Browse the repository at this point in the history
… tag
  • Loading branch information
bethesque committed Oct 24, 2019
1 parent 2b7f8e2 commit 301b30c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# The latest verification id for each consumer version tag
# This is not:
# find latest pacticipant version with given tag
# -> find the latest pact
# -> find the latest verification
# because the latest pacticipant version with the tag might not have a pact,
# and the latest pact might not have a verification.

# This is:
# join the tags and the pacticipant versions and the verifications and find the "latest" row

LATEST_VERIFICATION_IDS_FOR_CONSUMER_VERSION_TAGS_V1 = "select
pv.pacticipant_id as provider_id,
lpp.consumer_id,
t.name as consumer_version_tag_name,
max(v.id) as latest_verification_id
from verifications v
join latest_pact_publications_by_consumer_versions lpp
on v.pact_version_id = lpp.pact_version_id
join tags t
on lpp.consumer_version_id = t.version_id
join versions pv
on v.provider_version_id = pv.id
group by pv.pacticipant_id, lpp.consumer_id, t.name"

LATEST_VERIFICATION_IDS_FOR_CONSUMER_VERSION_TAGS_V2 = "select
pv.pacticipant_id as provider_id,
lpp.consumer_id,
t.name as consumer_version_tag_name,
max(v.id) as latest_verification_id
from verifications v
join latest_pact_publication_ids_for_consumer_versions lpp
on v.pact_version_id = lpp.pact_version_id
join tags t
on lpp.consumer_version_id = t.version_id
join versions pv
on v.provider_version_id = pv.id
group by pv.pacticipant_id, lpp.consumer_id, t.name"
Original file line number Diff line number Diff line change
@@ -1,22 +1,8 @@
require_relative '../ddl_statements'

Sequel.migration do
up do
# The latest verification id for each consumer version tag
# This is not the latest verification for the latest pact with a given tag,
# this is the latest verification for any pact with the tag
create_view(:latest_verification_ids_for_consumer_version_tags,
"select
pv.pacticipant_id as provider_id,
lpp.consumer_id,
t.name as consumer_version_tag_name,
max(v.id) as latest_verification_id
from verifications v
join latest_pact_publications_by_consumer_versions lpp
on v.pact_version_id = lpp.pact_version_id
join tags t
on lpp.consumer_version_id = t.version_id
join versions pv
on v.provider_version_id = pv.id
group by pv.pacticipant_id, lpp.consumer_id, t.name")
create_view(:latest_verification_ids_for_consumer_version_tags, LATEST_VERIFICATION_IDS_FOR_CONSUMER_VERSION_TAGS_V1)

# The most recent verification for each consumer/consumer version tag/provider
latest_verifications = from(:verifications)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require_relative '../ddl_statements'

Sequel.migration do
up do
create_or_replace_view(:latest_verification_ids_for_consumer_version_tags, LATEST_VERIFICATION_IDS_FOR_CONSUMER_VERSION_TAGS_V2)
end

down do
create_or_replace_view(:latest_verification_ids_for_consumer_version_tags, LATEST_VERIFICATION_IDS_FOR_CONSUMER_VERSION_TAGS_V1)
end
end

0 comments on commit 301b30c

Please sign in to comment.