Skip to content

Commit

Permalink
feat: keep track of latest pact revision in table rather than calcula…
Browse files Browse the repository at this point in the history
…ting it
  • Loading branch information
bethesque committed Jun 3, 2018
1 parent a4ce4c2 commit 1db5b7b
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 1 deletion.
5 changes: 4 additions & 1 deletion db/migrations/000028_create_all_pact_publications.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
group by provider_id, consumer_id, consumer_version_order"
)

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


# updated in 20180519_recreate_views.rb
# This view tells us the latest consumer version with a pact for a consumer/provider pair
create_or_replace_view(:latest_pact_consumer_version_orders,
"select provider_id, consumer_id, max(consumer_version_order) as latest_consumer_version_order
from all_pact_publications
Expand Down
21 changes: 21 additions & 0 deletions db/migrations/20180517_create_latest_pact_publication_ids.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Sequel.migration do
up do
# Latest pact_publication (revision) for each provider/consumer version.
# Keeping track of this in a table rather than having to calculate the
# latest revision speeds things up.
# We don't have to worry about updating it if a revision is deleted, because
# you can't delete a single revision through the API - all the revisions
# for a pact are deleted together when you delete the pact resource for that
# consumer version, and when that happens, this row will cascade delete.
create_table(:latest_pact_publication_ids_by_consumer_versions, charset: 'utf8') do
foreign_key :consumer_version_id, :versions, nil: false, on_delete: :cascade
foreign_key :provider_id, :pacticipants, nil: false, on_delete: :cascade
foreign_key :pact_publication_id, :pact_publications, nil: false, on_delete: :cascade
index [:provider_id, :consumer_version_id], unique: true, name: "unq_latest_ppid_prov_conver"
end
end

down do
drop_table(:latest_pact_publication_ids_by_consumer_versions)
end
end
11 changes: 11 additions & 0 deletions db/migrations/20180518_migrate_latest_pact_publication_ids.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Sequel.migration do
up do
# The danger with this migration is that a pact publication created by an old node will be lost
rows = from(:latest_pact_publications_by_consumer_versions).select(:consumer_version_id, :provider_id, :id)
from(:latest_pact_publication_ids_by_consumer_versions).insert(rows)
end

down do

end
end
43 changes: 43 additions & 0 deletions db/migrations/20180519_recreate_views.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
Sequel.migration do
up do
# Latest pact_publication details for each provider/consumer version
create_or_replace_view(:latest_pact_publications_by_consumer_versions,
"select app.*
from latest_pact_publication_ids_by_consumer_versions lpp
inner join all_pact_publications app
on lpp.consumer_version_id = app.consumer_version_id
and lpp.pact_publication_id = app.id
and lpp.provider_id = app.provider_id"
)

# Latest consumer version order for consumer/provider
# Recreate latest_pact_publication_ids_by_consumer_versions view
lpp = :latest_pact_publication_ids_by_consumer_versions
latest_pact_consumer_version_orders = from(lpp).select_group(
Sequel[lpp][:provider_id],
Sequel[:cv][:pacticipant_id].as(:consumer_id))
.select_append{ max(order).as(latest_consumer_version_order) }
.join(:versions, { Sequel[lpp][:consumer_version_id] => Sequel[:cv][:id] }, { table_alias: :cv })

create_or_replace_view(:latest_pact_consumer_version_orders, latest_pact_consumer_version_orders)
end

down do
# Latest pact_publication details for each provider/consumer version
create_or_replace_view(:latest_pact_publications_by_consumer_versions,
"select app.*
from all_pact_publications app
inner join latest_pact_publication_revision_numbers lr
on app.consumer_id = lr.consumer_id
and app.provider_id = lr.provider_id
and app.consumer_version_order = lr.consumer_version_order
and app.revision_number = lr.latest_revision_number"
)

create_or_replace_view(:latest_pact_consumer_version_orders,
"select provider_id, consumer_id, max(consumer_version_order) as latest_consumer_version_order
from all_pact_publications
group by provider_id, consumer_id"
)
end
end

0 comments on commit 1db5b7b

Please sign in to comment.