Skip to content

Commit 262af75

Browse files
committed
feat: remove refresh of materialized_matrix and materialized_head_matrix
1 parent b2dc209 commit 262af75

17 files changed

+12
-240
lines changed

db/migrations/20180517_create_latest_pact_publication_ids.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# Keeping track of this in a table rather than having to calculate the
55
# latest revision speeds things up.
66
# We don't have to worry about updating it if a revision is deleted, because
7-
# you can't delete a single revision through the API - all the revisions
7+
# you can't delete a single pact revision through the API - all the revisions
88
# for a pact are deleted together when you delete the pact resource for that
99
# consumer version, and when that happens, this row will cascade delete.
1010
create_table(:latest_pact_publication_ids_by_consumer_versions, charset: 'utf8') do

db/migrations/20180521_create_latest_verification_ids.rb

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
# Latest verification_id for each pact version/provider version.
44
# Keeping track of this in a table rather than having to calculate the
55
# latest revision speeds queries up.
6+
# There is no way to delete an individual verification result yet, but when there
7+
# is, we'll need to re-calculate the latest.
68
create_table(:latest_verification_id_for_pact_version_and_provider_version, charset: 'utf8') do
79
foreign_key :pact_version_id, :pact_versions, nil: false, on_delete: :cascade
810
foreign_key :provider_version_id, :versions, nil: false, on_delete: :cascade

lib/pact_broker/api/resources/base_resource.rb

-7
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ def update_matrix_after_request?
3939
end
4040

4141
def finish_request
42-
if update_matrix_after_request?
43-
matrix_service.refresh(identifier_from_path)
44-
end
4542
PactBroker.configuration.after_resource.call(self)
4643
end
4744

@@ -153,10 +150,6 @@ def contract_validation_errors? contract, params
153150
invalid
154151
end
155152

156-
def with_matrix_refresh &block
157-
matrix_service.refresh(identifier_from_path, &block)
158-
end
159-
160153
def find_pacticipant name, role
161154
pacticipant_service.find_pacticipant_by_name(name).tap do | pacticipant |
162155
set_json_error_message("No #{role} with name '#{name}' found") if pacticipant.nil?

lib/pact_broker/api/resources/pact.rb

+1-3
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,7 @@ def to_html
8787
end
8888

8989
def delete_resource
90-
with_matrix_refresh do
91-
pact_service.delete(pact_params)
92-
end
90+
pact_service.delete(pact_params)
9391
true
9492
end
9593

lib/pact_broker/api/resources/pacticipant.rb

+1-3
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ def resource_exists?
4545
end
4646

4747
def delete_resource
48-
with_matrix_refresh do
49-
pacticipant_service.delete pacticipant_name
50-
end
48+
pacticipant_service.delete pacticipant_name
5149
true
5250
end
5351

lib/pact_broker/api/resources/tag.rb

+1-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ def from_json
2222
@tag = tag_service.create identifier_from_path
2323
# Make it return a 201 by setting the Location header
2424
response.headers["Location"] = tag_url(base_url, tag)
25-
matrix_service.refresh_tags(identifier_from_path)
2625
end
2726
response.body = to_json
2827
end
@@ -40,9 +39,7 @@ def tag
4039
end
4140

4241
def delete_resource
43-
matrix_service.refresh_tags(identifier_from_path) do
44-
tag_service.delete identifier_from_path
45-
end
42+
tag_service.delete identifier_from_path
4643
true
4744
end
4845
end

lib/pact_broker/api/resources/version.rb

+1-3
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ def to_json
2525
end
2626

2727
def delete_resource
28-
with_matrix_refresh do
29-
version_service.delete version
30-
end
28+
version_service.delete version
3129
true
3230
end
3331

lib/pact_broker/db/clean.rb

+2-7
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ def initialize database_connection, options = {}
1414
end
1515

1616
def call
17-
db[:verifications].where(id: db[:materialized_head_matrix].select(:verification_id)).invert.delete
18-
pp_ids = db[:materialized_head_matrix].select(:pact_publication_id)
17+
db[:verifications].where(id: db[:head_matrix].select(:verification_id)).invert.delete
18+
pp_ids = db[:head_matrix].select(:pact_publication_id)
1919

2020
triggered_webhook_ids = db[:triggered_webhooks].where(pact_publication_id: pp_ids).invert.select(:id)
2121
db[:webhook_executions].where(triggered_webhook_id: triggered_webhook_ids).delete
@@ -33,11 +33,6 @@ def call
3333

3434
db[:tags].where(version_id: referenced_version_ids).invert.delete
3535
db[:versions].where(id: referenced_version_ids).invert.delete
36-
37-
db[:materialized_matrix].delete
38-
db[:materialized_matrix].insert(db[:matrix].select_all)
39-
db[:materialized_head_matrix].delete
40-
db[:materialized_head_matrix].insert(db[:head_matrix].select_all)
4136
end
4237

4338
private

lib/pact_broker/matrix/head_row.rb

+1-20
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module Matrix
55
# A row for each of the overall latest pacts, and a row for each of the latest tagged pacts
66
# Rows with a nil consumer_tag_name are the overall latest
77
class HeadRow < Row
8-
set_dataset(:materialized_head_matrix)
8+
set_dataset(:head_matrix)
99

1010
# one_to_one :latest_verification_for_consumer_version_tag,
1111
# :class => "PactBroker::Verifications::LatestVerificationForConsumerVersionTag",
@@ -29,25 +29,6 @@ class HeadRow < Row
2929
end
3030
end
3131
end)
32-
33-
dataset_module do
34-
include PactBroker::Repositories::Helpers
35-
include PactBroker::Logging
36-
37-
def refresh ids
38-
return super unless ids[:tag_name]
39-
40-
logger.debug("Refreshing #{model.table_name} for #{ids}")
41-
db = model.db
42-
table_name = model.table_name
43-
criteria = { consumer_id: ids[:pacticipant_id], consumer_version_tag_name: ids[:tag_name] }
44-
db.transaction do
45-
db[table_name].where(criteria).delete
46-
new_rows = db[source_view_name].where(criteria)
47-
db[table_name].insert(new_rows)
48-
end
49-
end
50-
end
5132
end
5233
end
5334
end

lib/pact_broker/matrix/repository.rb

-18
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,6 @@ class Repository
2323
GROUP_BY_PROVIDER = [:consumer_name, :consumer_version_number, :provider_name]
2424
GROUP_BY_PACT = [:consumer_name, :provider_name]
2525

26-
# Use a block when the refresh is caused by a resource deletion
27-
# This allows us to store the correct object ids for use afterwards
28-
def refresh params
29-
criteria = find_ids_for_pacticipant_names(params)
30-
yield if block_given?
31-
PactBroker::Matrix::Row.refresh(criteria)
32-
PactBroker::Matrix::HeadRow.refresh(criteria)
33-
end
34-
35-
# Only need to update the HeadRow table when tags change
36-
# because it only changes which rows are the latest tagged ones -
37-
# it doesn't change the actual values in the underlying matrix.
38-
def refresh_tags params
39-
criteria = find_ids_for_pacticipant_names(params)
40-
yield if block_given?
41-
PactBroker::Matrix::HeadRow.refresh(criteria)
42-
end
43-
4426
def find_ids_for_pacticipant_names params
4527
criteria = {}
4628

lib/pact_broker/matrix/row.rb

+1-27
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
module PactBroker
99
module Matrix
1010

11-
class Row < Sequel::Model(:materialized_matrix)
11+
class Row < Sequel::Model(:matrix)
1212

1313
# Used when using table_print to output query results
1414
TP_COLS = [ :consumer_version_number, :pact_revision_number, :provider_version_number, :verification_number]
@@ -26,32 +26,6 @@ class Row < Sequel::Model(:materialized_matrix)
2626
include PactBroker::Repositories::Helpers
2727
include PactBroker::Logging
2828

29-
def refresh ids
30-
logger.debug("Refreshing #{model.table_name} for #{ids}")
31-
32-
db = model.db
33-
table_name = model.table_name
34-
35-
if ids[:pacticipant_id]
36-
db.transaction do
37-
db[table_name].where(consumer_id: ids[:pacticipant_id]).or(provider_id: ids[:pacticipant_id]).delete
38-
new_rows = db[source_view_name].where(consumer_id: ids[:pacticipant_id]).or(provider_id: ids[:pacticipant_id]).distinct
39-
db[table_name].insert(new_rows)
40-
end
41-
elsif ids.any?
42-
accepted_columns = [:consumer_id, :consumer_name, :provider_id, :provider_name]
43-
criteria = ids.reject{ |k, v| !accepted_columns.include?(k) }
44-
db.transaction do
45-
db[table_name].where(criteria).delete
46-
db[table_name].insert(db[source_view_name].where(criteria))
47-
end
48-
end
49-
end
50-
51-
def source_view_name
52-
model.table_name.to_s.gsub('materialized_', '').to_sym
53-
end
54-
5529
def matching_selectors selectors
5630
if selectors.size == 1
5731
where_consumer_or_provider_is(selectors.first)

lib/pact_broker/matrix/service.rb

-8
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,6 @@ module Service
1010
extend PactBroker::Repositories
1111
extend PactBroker::Services
1212

13-
def refresh params, &block
14-
matrix_repository.refresh(params, &block)
15-
end
16-
17-
def refresh_tags params, &block
18-
matrix_repository.refresh_tags(params, &block)
19-
end
20-
2113
def find selectors, options = {}
2214
query_results = matrix_repository.find selectors, options
2315
pacticipant_names = selectors.collect{ | s| s[:pacticipant_name] }

spec/lib/pact_broker/db/clean_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ module DB
4444

4545
it "deletes rows that aren't the latest or latest tagged" do
4646
subject
47-
expect(db[:materialized_matrix].where(consumer_version_number: "2").count).to eq 0
47+
expect(db[:matrix].where(consumer_version_number: "2").count).to eq 0
4848
end
4949

5050
it "deletes orphan pact_versions" do

spec/lib/pact_broker/matrix/head_row_spec.rb

-44
Original file line numberDiff line numberDiff line change
@@ -57,50 +57,6 @@ module Matrix
5757
end
5858
end
5959
end
60-
describe "refresh", migration: true do
61-
before do
62-
PactBroker::Database.migrate
63-
end
64-
65-
let(:td) { TestDataBuilder.new(auto_refresh_matrix: false) }
66-
67-
before do
68-
td.create_pact_with_hierarchy("Foo", "1", "Bar")
69-
end
70-
71-
context "with a consumer pacticipant_id and a consumer tag_name" do
72-
before do
73-
td.create_consumer_version_tag("prod")
74-
Row.refresh(ids)
75-
end
76-
let(:ids) { { pacticipant_id: td.consumer.id, tag_name: "prod"} }
77-
78-
subject { HeadRow.refresh(ids) }
79-
80-
it "refreshes the data for the consumer and consumer tag in the head matrix" do
81-
subject
82-
expect(HeadRow.all.collect(&:values)).to contain_hash(provider_name: "Bar", consumer_name: "Foo", consumer_version_tag_name: "prod")
83-
end
84-
end
85-
86-
context "with a provider pacticipant_id and a provider tag_name" do
87-
before do
88-
td.create_verification(provider_version: "2")
89-
.use_provider_version("2")
90-
.create_provider_version_tag("prod")
91-
Row.refresh(ids)
92-
end
93-
94-
let(:ids) { { pacticipant_id: td.consumer.id, tag_name: "prod" } }
95-
96-
subject { HeadRow.refresh(ids) }
97-
98-
it "does not update the head matrix as the head matrix only contains consumer tags" do
99-
subject
100-
expect(HeadRow.count).to eq 0
101-
end
102-
end
103-
end
10460
end
10561
end
10662
end

spec/lib/pact_broker/matrix/repository_spec.rb

-20
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,6 @@ def shorten_rows rows
1919
rows.collect{ |r| shorten_row(r) }
2020
end
2121

22-
describe "refresh" do
23-
before do
24-
td.create_pact_with_hierarchy("Foo", "1", "Bar")
25-
Row.refresh(pacticipant_id: td.provider.id)
26-
end
27-
28-
context "when deleting an object in the block" do
29-
it "removes the relevant lines from the matrix" do
30-
expect(Row.count).to_not eq 0
31-
Repository.new.refresh(pacticipant_name: "Bar") { PactBroker::Pacticipants::Service.delete("Bar") }
32-
expect(Row.count).to eq 0
33-
end
34-
35-
it "yields the block" do
36-
Repository.new.refresh(pacticipant_name: "Bar") { PactBroker::Pacticipants::Service.delete("Bar") }
37-
expect(PactBroker::Domain::Pacticipant.where(name: "Bar").count).to eq 0
38-
end
39-
end
40-
end
41-
4222
describe "find" do
4323
before do
4424
# A1 - B1

spec/lib/pact_broker/matrix/row_spec.rb

-56
Original file line numberDiff line numberDiff line change
@@ -26,62 +26,6 @@ module Matrix
2626
end
2727
end
2828

29-
describe "refresh", migration: true do
30-
let(:td) { TestDataBuilder.new(auto_refresh_matrix: false) }
31-
32-
before do
33-
PactBroker::Database.migrate
34-
td.create_pact_with_hierarchy("Foo", "1", "Bar")
35-
end
36-
37-
context "with only a consumer_id" do
38-
subject { Row.refresh(consumer_id: td.consumer.id) }
39-
40-
it "refreshes the data for the consumer" do
41-
subject
42-
expect(Row.all.collect(&:values)).to contain_hash(provider_name: "Bar", consumer_name: "Foo")
43-
end
44-
end
45-
46-
context "with only a provider_id" do
47-
subject { Row.refresh(provider_id: td.provider.id) }
48-
49-
it "refreshes the data for the provider" do
50-
subject
51-
expect(Row.all.collect(&:values)).to contain_hash(provider_name: "Bar", consumer_name: "Foo")
52-
end
53-
end
54-
55-
context "with both consumer_id and provider_id" do
56-
subject { Row.refresh(provider_id: td.provider.id, consumer_id: td.consumer.id) }
57-
58-
it "refreshes the data for the consumer and provider" do
59-
subject
60-
expect(Row.all.collect(&:values)).to contain_hash(provider_name: "Bar", consumer_name: "Foo")
61-
end
62-
end
63-
64-
context "when there was existing data" do
65-
it "deletes the existing data before inserting the new data" do
66-
Row.refresh(provider_id: td.provider.id, consumer_id: td.consumer.id)
67-
expect(Row.count).to eq 1
68-
td.create_consumer_version("2")
69-
.create_pact
70-
Row.refresh(provider_id: td.provider.id, consumer_id: td.consumer.id)
71-
expect(Row.count).to eq 2
72-
end
73-
end
74-
75-
context "with a pacticipant_id" do
76-
subject { Row.refresh(pacticipant_id: td.provider.id) }
77-
78-
it "refreshes the data for both consumer and provider roles" do
79-
subject
80-
expect(Row.all.collect(&:values)).to contain_hash(provider_name: "Bar", consumer_name: "Foo")
81-
end
82-
end
83-
end
84-
8529
describe "<=>" do
8630
let(:row_1) do
8731
Row.new(

0 commit comments

Comments
 (0)