Skip to content

Commit 1db5b7b

Browse files
committed
feat: keep track of latest pact revision in table rather than calculating it
1 parent a4ce4c2 commit 1db5b7b

4 files changed

+79
-1
lines changed

db/migrations/000028_create_all_pact_publications.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
group by provider_id, consumer_id, consumer_version_order"
2222
)
2323

24-
# Latest pact_publication details for each consumer version
24+
# Latest pact_publication (revision) for each provider/consumer version
25+
# updated in 20180519_recreate_views.rb
2526
create_view(:latest_pact_publications_by_consumer_versions,
2627
"select app.*
2728
from all_pact_publications app
@@ -33,6 +34,8 @@
3334
)
3435

3536

37+
# updated in 20180519_recreate_views.rb
38+
# This view tells us the latest consumer version with a pact for a consumer/provider pair
3639
create_or_replace_view(:latest_pact_consumer_version_orders,
3740
"select provider_id, consumer_id, max(consumer_version_order) as latest_consumer_version_order
3841
from all_pact_publications
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Sequel.migration do
2+
up do
3+
# Latest pact_publication (revision) for each provider/consumer version.
4+
# Keeping track of this in a table rather than having to calculate the
5+
# latest revision speeds things up.
6+
# 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
8+
# for a pact are deleted together when you delete the pact resource for that
9+
# consumer version, and when that happens, this row will cascade delete.
10+
create_table(:latest_pact_publication_ids_by_consumer_versions, charset: 'utf8') do
11+
foreign_key :consumer_version_id, :versions, nil: false, on_delete: :cascade
12+
foreign_key :provider_id, :pacticipants, nil: false, on_delete: :cascade
13+
foreign_key :pact_publication_id, :pact_publications, nil: false, on_delete: :cascade
14+
index [:provider_id, :consumer_version_id], unique: true, name: "unq_latest_ppid_prov_conver"
15+
end
16+
end
17+
18+
down do
19+
drop_table(:latest_pact_publication_ids_by_consumer_versions)
20+
end
21+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Sequel.migration do
2+
up do
3+
# The danger with this migration is that a pact publication created by an old node will be lost
4+
rows = from(:latest_pact_publications_by_consumer_versions).select(:consumer_version_id, :provider_id, :id)
5+
from(:latest_pact_publication_ids_by_consumer_versions).insert(rows)
6+
end
7+
8+
down do
9+
10+
end
11+
end
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
Sequel.migration do
2+
up do
3+
# Latest pact_publication details for each provider/consumer version
4+
create_or_replace_view(:latest_pact_publications_by_consumer_versions,
5+
"select app.*
6+
from latest_pact_publication_ids_by_consumer_versions lpp
7+
inner join all_pact_publications app
8+
on lpp.consumer_version_id = app.consumer_version_id
9+
and lpp.pact_publication_id = app.id
10+
and lpp.provider_id = app.provider_id"
11+
)
12+
13+
# Latest consumer version order for consumer/provider
14+
# Recreate latest_pact_publication_ids_by_consumer_versions view
15+
lpp = :latest_pact_publication_ids_by_consumer_versions
16+
latest_pact_consumer_version_orders = from(lpp).select_group(
17+
Sequel[lpp][:provider_id],
18+
Sequel[:cv][:pacticipant_id].as(:consumer_id))
19+
.select_append{ max(order).as(latest_consumer_version_order) }
20+
.join(:versions, { Sequel[lpp][:consumer_version_id] => Sequel[:cv][:id] }, { table_alias: :cv })
21+
22+
create_or_replace_view(:latest_pact_consumer_version_orders, latest_pact_consumer_version_orders)
23+
end
24+
25+
down do
26+
# Latest pact_publication details for each provider/consumer version
27+
create_or_replace_view(:latest_pact_publications_by_consumer_versions,
28+
"select app.*
29+
from all_pact_publications app
30+
inner join latest_pact_publication_revision_numbers lr
31+
on app.consumer_id = lr.consumer_id
32+
and app.provider_id = lr.provider_id
33+
and app.consumer_version_order = lr.consumer_version_order
34+
and app.revision_number = lr.latest_revision_number"
35+
)
36+
37+
create_or_replace_view(:latest_pact_consumer_version_orders,
38+
"select provider_id, consumer_id, max(consumer_version_order) as latest_consumer_version_order
39+
from all_pact_publications
40+
group by provider_id, consumer_id"
41+
)
42+
end
43+
end

0 commit comments

Comments
 (0)