diff --git a/db/migrations/000028_create_all_pact_publications.rb b/db/migrations/000028_create_all_pact_publications.rb index 1cfb752c0..4a8683d92 100644 --- a/db/migrations/000028_create_all_pact_publications.rb +++ b/db/migrations/000028_create_all_pact_publications.rb @@ -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 @@ -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 diff --git a/db/migrations/20180517_create_latest_pact_publication_ids.rb b/db/migrations/20180517_create_latest_pact_publication_ids.rb new file mode 100644 index 000000000..634cc1e9c --- /dev/null +++ b/db/migrations/20180517_create_latest_pact_publication_ids.rb @@ -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 diff --git a/db/migrations/20180518_migrate_latest_pact_publication_ids.rb b/db/migrations/20180518_migrate_latest_pact_publication_ids.rb new file mode 100644 index 000000000..c3743febd --- /dev/null +++ b/db/migrations/20180518_migrate_latest_pact_publication_ids.rb @@ -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 diff --git a/db/migrations/20180519_recreate_views.rb b/db/migrations/20180519_recreate_views.rb new file mode 100644 index 000000000..adfe313ad --- /dev/null +++ b/db/migrations/20180519_recreate_views.rb @@ -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