Skip to content

Commit 13b7c6e

Browse files
committed
feat: load latest verification for consumer/provider via relationship rather than repository
1 parent e366af4 commit 13b7c6e

9 files changed

+111
-19
lines changed

db/migrations/20180523_create_latest_verifications_for_consumer_version_tags.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Sequel.migration do
22
up do
33
# The latest verification id for each consumer version tag
4-
create_view(:latest_verifications_ids_for_consumer_version_tags,
4+
create_view(:latest_verification_ids_for_consumer_version_tags,
55
"select
66
pv.pacticipant_id as provider_id,
77
lpp.consumer_id,
@@ -27,7 +27,7 @@
2727
Sequel[:prv][:order].as(:provider_version_order),
2828
)
2929
.select_append{ verifications.* }
30-
.join(:latest_verifications_ids_for_consumer_version_tags,
30+
.join(:latest_verification_ids_for_consumer_version_tags,
3131
{
3232
Sequel[:verifications][:id] => Sequel[:lv][:latest_verification_id],
3333
}, { table_alias: :lv })
@@ -45,6 +45,6 @@
4545

4646
down do
4747
drop_view(:latest_verifications_for_consumer_version_tags)
48-
drop_view(:latest_verifications_ids_for_consumer_version_tags)
48+
drop_view(:latest_verification_ids_for_consumer_version_tags)
4949
end
5050
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
Sequel.migration do
2+
up do
3+
# The latest verification id for each consumer version tag
4+
create_view(:latest_verification_ids_for_consumer_and_provider,
5+
"select
6+
pv.pacticipant_id as provider_id,
7+
lpp.consumer_id,
8+
max(v.id) as latest_verification_id
9+
from verifications v
10+
join latest_pact_publications_by_consumer_versions lpp
11+
on v.pact_version_id = lpp.pact_version_id
12+
join versions pv
13+
on v.provider_version_id = pv.id
14+
group by pv.pacticipant_id, lpp.consumer_id")
15+
16+
# The most recent verification for each consumer/consumer version tag/provider
17+
latest_verifications = from(:verifications)
18+
.select(
19+
Sequel[:lv][:consumer_id],
20+
Sequel[:lv][:provider_id],
21+
Sequel[:pv][:sha].as(:pact_version_sha),
22+
Sequel[:prv][:number].as(:provider_version_number),
23+
Sequel[:prv][:order].as(:provider_version_order),
24+
)
25+
.select_append{ verifications.* }
26+
.join(:latest_verification_ids_for_consumer_and_provider,
27+
{
28+
Sequel[:verifications][:id] => Sequel[:lv][:latest_verification_id],
29+
}, { table_alias: :lv })
30+
.join(:versions,
31+
{
32+
Sequel[:verifications][:provider_version_id] => Sequel[:prv][:id]
33+
}, { table_alias: :prv })
34+
.join(:pact_versions,
35+
{
36+
Sequel[:verifications][:pact_version_id] => Sequel[:pv][:id]
37+
}, { table_alias: :pv })
38+
39+
create_or_replace_view(:latest_verifications_for_consumer_and_provider, latest_verifications)
40+
end
41+
42+
down do
43+
drop_view(:latest_verifications_for_consumer_and_provider)
44+
drop_view(:latest_verification_ids_for_consumer_and_provider)
45+
end
46+
end

lib/pact_broker/index/service.rb

-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ def self.find_index_items options = {}
1818
.select_all_qualified
1919
.eager(:latest_triggered_webhooks)
2020
.eager(:webhooks)
21-
.order(:consumer_name, :provider_name)
22-
#.eager(verification: [:provider_version, :pact_version])
2321

2422
if !options[:tags]
2523
rows = rows.where(consumer_version_tag_name: nil)

lib/pact_broker/matrix/aggregated_row.rb

+2-5
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,8 @@ def latest_verification_for_consumer_version_tag row
5555
end
5656

5757
def overall_latest_verification
58-
# This causes the
59-
# SELECT "latest_verifications".* FROM "latest_verifications"
60-
# query in the logs for the tagged index.
61-
# Given it only happens rarely, it's ok to not lazy load it.
62-
PactBroker::Verifications::Repository.new.find_latest_verification_for(consumer_name, provider_name)
58+
# not eager loaded because it shouldn't be called that often
59+
first_row.latest_verification_for_consumer_and_provider
6360
end
6461

6562
def first_row

lib/pact_broker/matrix/row.rb

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require 'pact_broker/tags/tag_with_latest_flag'
44
require 'pact_broker/logging'
55
require 'pact_broker/verifications/latest_verification_for_consumer_version_tag'
6+
require 'pact_broker/verifications/latest_verification_for_consumer_and_provider'
67

78
module PactBroker
89
module Matrix
@@ -17,6 +18,10 @@ class Row < Sequel::Model(:materialized_matrix)
1718
associate(:one_to_many, :consumer_version_tags, :class => "PactBroker::Tags::TagWithLatestFlag", primary_key: :consumer_version_id, key: :version_id)
1819
associate(:one_to_many, :provider_version_tags, :class => "PactBroker::Tags::TagWithLatestFlag", primary_key: :provider_version_id, key: :version_id)
1920

21+
many_to_one :latest_verification_for_consumer_and_provider,
22+
:class => "PactBroker::Verifications::LatestVerificationForConsumerAndProvider",
23+
primary_key: [:provider_id, :consumer_id], key: [:provider_id, :consumer_id]
24+
2025
dataset_module do
2126
include PactBroker::Repositories::Helpers
2227
include PactBroker::Logging
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
require 'pact_broker/domain/verification'
2+
3+
module PactBroker
4+
module Verifications
5+
6+
include PactBroker::Repositories::Helpers
7+
8+
class LatestVerificationForConsumerAndProvider < PactBroker::Domain::Verification
9+
set_dataset(:latest_verifications_for_consumer_and_provider)
10+
11+
# Don't need to load the pact_version as we do in the superclass,
12+
# as pact_version_sha is included in the view for convenience
13+
def pact_version_sha
14+
values[:pact_version_sha]
15+
end
16+
17+
def provider_version_number
18+
values[:provider_version_number]
19+
end
20+
21+
def provider_version_order
22+
values[:provider_version_order]
23+
end
24+
end
25+
end
26+
end

lib/pact_broker/verifications/latest_verification_for_consumer_version_tag.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ module Verifications
55
class LatestVerificationForConsumerVersionTag < PactBroker::Domain::Verification
66
set_dataset(:latest_verifications_for_consumer_version_tags)
77

8+
# Don't need to load the pact_version as we do in the superclass,
9+
# as pact_version_sha is included in the view for convenience
810
def pact_version_sha
9-
# Don't need to load the pact_version as we do in the superclass,
10-
# as pact_version_sha is included in the view for convenience
1111
values[:pact_version_sha]
1212
end
1313

spec/lib/pact_broker/matrix/aggregated_row_spec.rb

+6-7
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,21 @@ module Matrix
4545
end
4646

4747
context "when there is no verification for any of the rows or any of the pacts with the same tag" do
48-
before do
49-
allow_any_instance_of(PactBroker::Verifications::Repository).to receive(:find_latest_verification_for).and_return(overall_latest_verification)
50-
end
51-
52-
let(:overall_latest_verification) { instance_double('PactBroker::Domain::Verification', id: 5) }
5348
let(:verification_1) { nil }
5449
let(:verification_2) { nil }
5550
let(:tag_verification_1) { nil }
5651
let(:tag_verification_2) { nil }
5752

5853
context "when one of the rows is the overall latest" do
5954
let(:consumer_version_tag_name_1) { nil }
55+
let(:overall_latest_verification) { instance_double('PactBroker::Domain::Verification', id: 1) }
56+
before do
57+
allow(row_1).to receive(:latest_verification_for_consumer_and_provider).and_return(overall_latest_verification)
58+
end
6059

6160
it "looks up the overall latest verification" do
62-
expect_any_instance_of(PactBroker::Verifications::Repository).to receive(:find_latest_verification_for).with("Foo", "Bar")
63-
subject
61+
expect(row_1).to receive(:latest_verification_for_consumer_and_provider)
62+
subject
6463
end
6564

6665
it "returns the overall latest verification" do

spec/lib/pact_broker/matrix/row_spec.rb

+21
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,27 @@ module Matrix
55
describe Row do
66
let(:td) { TestDataBuilder.new }
77

8+
describe "latest_verification_for_consumer_and_provider" do
9+
before do
10+
td.create_pact_with_hierarchy("Foo", "1", "Bar")
11+
.create_verification(provider_version: "9")
12+
.create_consumer_version("2")
13+
.create_consumer_version_tag("prod")
14+
.create_pact
15+
.create_verification(provider_version: "10")
16+
.create_consumer("Wiffle")
17+
.create_consumer_version("4")
18+
.create_pact
19+
.create_verification(provider_version: "11")
20+
end
21+
22+
subject { Row.where(consumer_name: "Foo", provider_name: "Bar").all.collect(&:latest_verification_for_consumer_and_provider) }
23+
24+
it "returns the latest verification for the consumer and provider" do
25+
expect(subject.collect(&:provider_version_number)).to eq ["10", "10"]
26+
end
27+
end
28+
829
describe "refresh", migration: true do
930
let(:td) { TestDataBuilder.new(auto_refresh_matrix: false) }
1031

0 commit comments

Comments
 (0)