Skip to content

Commit eb67511

Browse files
committed
feat: create view for latest verifications for consumer version tags
1 parent c062b2e commit eb67511

File tree

5 files changed

+91
-3
lines changed

5 files changed

+91
-3
lines changed

db/migrations/20180311_optimise_head_matrix.rb

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
)
1717

1818
# Add provider_version_order to original definition
19+
# The most recent verification for each pact version
1920
v = :verifications
2021
create_or_replace_view(:latest_verifications,
2122
from(v)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Sequel.migration do
2+
up do
3+
# The latest verification id for each consumer version tag
4+
create_view(:latest_verifications_ids_for_consumer_version_tags,
5+
"select t.name as consumer_version_tag_name, max(lv.id) as latest_verification_id
6+
from verifications lv
7+
join latest_pact_publications_by_consumer_versions lpp
8+
on lv.pact_version_id = lpp.pact_version_id
9+
join tags t on lpp.consumer_version_id = t.version_id
10+
group by t.name")
11+
12+
# The latest verification for each consumer version tag
13+
create_view(:latest_verifications_for_consumer_version_tags,
14+
"select v.*, lv.consumer_version_tag_name
15+
from verifications v
16+
join latest_verifications_ids_for_consumer_version_tags lv
17+
on lv.latest_verification_id = v.id")
18+
end
19+
20+
down do
21+
drop_view(:latest_verifications_for_consumer_version_tags)
22+
drop_view(:latest_verifications_ids_for_consumer_version_tags)
23+
end
24+
end

lib/pact_broker/matrix/row.rb

+14
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
require 'pact_broker/webhooks/latest_triggered_webhook'
33
require 'pact_broker/tags/tag_with_latest_flag'
44
require 'pact_broker/logging'
5+
require 'pact_broker/verifications/latest_verification_for_consumer_version_tag'
56

67
module PactBroker
78
module Matrix
9+
810
class Row < Sequel::Model(:materialized_matrix)
911

1012
# Used when using table_print to output query results
@@ -14,6 +16,18 @@ class Row < Sequel::Model(:materialized_matrix)
1416
associate(:one_to_many, :webhooks, :class => "PactBroker::Webhooks::Webhook", primary_key: [:consumer_id, :provider_id], key: [:consumer_id, :provider_id])
1517
associate(:one_to_many, :consumer_version_tags, :class => "PactBroker::Tags::TagWithLatestFlag", primary_key: :consumer_version_id, key: :version_id)
1618
associate(:one_to_many, :provider_version_tags, :class => "PactBroker::Tags::TagWithLatestFlag", primary_key: :provider_version_id, key: :version_id)
19+
associate(:many_to_one, :verification, :class => "PactBroker::Domain::Verification", primary_key: :id, key: :verification_id)
20+
21+
one_to_one :latest_verification_for_consumer_version_tag, primary_keys: [], key: [], :eager_loader=>(proc do |eo_opts|
22+
tag_to_row = eo_opts[:rows].each_with_object({}) { | row, map | row.consumer_version_tags.each{ | tag | map[tag.name] = row } }
23+
eo_opts[:rows].each{|row| row.associations[:latest_verification_for_consumer_version_tag] = nil}
24+
25+
PactBroker::Verifications::LatestVerificationForConsumerVersionTag.each do | verification |
26+
if tag_to_row[verification.consumer_version_tag_name]
27+
tag_to_row[verification.consumer_version_tag_name].associations[:latest_verification_for_consumer_version_tag] = verification
28+
end
29+
end
30+
end)
1731

1832
dataset_module do
1933
include PactBroker::Repositories::Helpers
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require 'pact_broker/domain/verification'
2+
3+
module PactBroker
4+
module Verifications
5+
class LatestVerificationForConsumerVersionTag < PactBroker::Domain::Verification
6+
set_dataset(:latest_verifications_for_consumer_version_tags)
7+
end
8+
end
9+
end

spec/lib/pact_broker/matrix/row_spec.rb

+43-3
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,54 @@
33
module PactBroker
44
module Matrix
55
describe Row do
6-
describe "refresh", migration: true do
7-
before do
8-
PactBroker::Database.migrate
6+
let(:td) { TestDataBuilder.new }
7+
8+
describe "latest_verification_for_consumer_version_tag" do
9+
context "when the pact with a given tag has been verified" do
10+
before do
11+
td.create_pact_with_hierarchy("Foo", "1", "Bar")
12+
.create_consumer_version_tag("prod")
13+
.create_verification(provider_version: "10")
14+
.create_consumer_version("2")
15+
.create_consumer_version_tag("prod")
16+
.create_pact
17+
.create_verification(provider_version: "11", number: 1)
18+
.create_verification(provider_version: "12", number: 2)
19+
end
20+
21+
subject { Row.eager(:consumer_version_tags).eager(:latest_verification_for_consumer_version_tag).order(:pact_publication_id, :verification_id).all }
22+
23+
it "returns its most recent verification" do
24+
expect(subject.last.provider_version_number).to eq "12"
25+
expect(subject.last.latest_verification_for_consumer_version_tag.provider_version.number).to eq "12"
26+
end
27+
end
28+
29+
context "when the most recent pact with a given tag has not been verified, but a previous version with the same tag has" do
30+
before do
31+
td.create_pact_with_hierarchy("Foo", "1", "Bar")
32+
.create_consumer_version_tag("prod")
33+
.create_verification(provider_version: "10")
34+
.create_verification(provider_version: "11", number: 2, comment: "this is the latest verification for a pact with cv tag prod")
35+
.create_consumer_version("2")
36+
.create_consumer_version_tag("prod")
37+
.create_pact
38+
end
39+
40+
subject { Row.eager(:consumer_version_tags).eager(:latest_verification_for_consumer_version_tag).order(:pact_publication_id).all }
41+
42+
it "returns the most recent verification for the previous version with the same tag" do
43+
expect(subject.last.verification_id).to be nil # this pact version has not been verified directly
44+
expect(subject.last.latest_verification_for_consumer_version_tag.provider_version.number).to eq "11"
45+
end
946
end
47+
end
1048

49+
describe "refresh", migration: true do
1150
let(:td) { TestDataBuilder.new(auto_refresh_matrix: false) }
1251

1352
before do
53+
PactBroker::Database.migrate
1454
td.create_pact_with_hierarchy("Foo", "1", "Bar")
1555
end
1656

0 commit comments

Comments
 (0)