diff --git a/db/ddl_statements/latest_tagged_pact_consumer_version_orders.rb b/db/ddl_statements/latest_tagged_pact_consumer_version_orders.rb new file mode 100644 index 000000000..f80505337 --- /dev/null +++ b/db/ddl_statements/latest_tagged_pact_consumer_version_orders.rb @@ -0,0 +1,23 @@ +def latest_tagged_pact_consumer_version_orders_v2(connection) + pp = :pact_publications + connection.from(pp) + .select_group( + Sequel[pp][:provider_id], + Sequel[:cv][:pacticipant_id].as(:consumer_id), + Sequel[:t][:name].as(:tag_name)) + .select_append{ max(order).as(latest_consumer_version_order) } + .join(:versions, { Sequel[pp][:consumer_version_id] => Sequel[:cv][:id] }, { table_alias: :cv} ) + .join(:tags, { Sequel[:t][:version_id] => Sequel[pp][:consumer_version_id] }, { table_alias: :t }) +end + +def latest_tagged_pact_consumer_version_orders_v3(connection) + view = Sequel.as(:latest_pact_publication_ids_for_consumer_versions, :lp) + connection.from(view) + .select_group( + Sequel[:lp][:provider_id], + Sequel[:cv][:pacticipant_id].as(:consumer_id), + Sequel[:t][:name].as(:tag_name)) + .select_append{ max(order).as(latest_consumer_version_order) } + .join(:versions, { Sequel[:lp][:consumer_version_id] => Sequel[:cv][:id] }, { table_alias: :cv} ) + .join(:tags, { Sequel[:t][:version_id] => Sequel[:lp][:consumer_version_id] }, { table_alias: :t }) +end diff --git a/db/migrations/20180311_optimise_head_matrix.rb b/db/migrations/20180311_optimise_head_matrix.rb index 6f20f98b3..034e97d7b 100644 --- a/db/migrations/20180311_optimise_head_matrix.rb +++ b/db/migrations/20180311_optimise_head_matrix.rb @@ -6,15 +6,7 @@ pp = :pact_publications # For each consumer_id/provider_id/tag_name, the version order of the latest version that has a pact create_or_replace_view(:latest_tagged_pact_consumer_version_orders, - from(:pact_publications) - .select_group( - Sequel[pp][:provider_id], - Sequel[:cv][:pacticipant_id].as(:consumer_id), - Sequel[:t][:name].as(:tag_name)) - .select_append{ max(order).as(latest_consumer_version_order) } - .join(:versions, { Sequel[pp][:consumer_version_id] => Sequel[:cv][:id] }, { table_alias: :cv} ) - .join(:tags, { Sequel[:t][:version_id] => Sequel[pp][:consumer_version_id] }, { table_alias: :t }) - ) + latest_tagged_pact_consumer_version_orders_v2(self)) # Add provider_version_order to original definition # The most recent verification for each pact_version diff --git a/db/migrations/20191028_optimise_latest_tagged_pact_cv_orders.rb b/db/migrations/20191028_optimise_latest_tagged_pact_cv_orders.rb new file mode 100644 index 000000000..da3b4abb2 --- /dev/null +++ b/db/migrations/20191028_optimise_latest_tagged_pact_cv_orders.rb @@ -0,0 +1,13 @@ +require_relative '../ddl_statements' + +Sequel.migration do + up do + create_or_replace_view(:latest_tagged_pact_consumer_version_orders, + latest_tagged_pact_consumer_version_orders_v3(self)) + end + + down do + create_or_replace_view(:latest_tagged_pact_consumer_version_orders, + latest_tagged_pact_consumer_version_orders_v2(self)) + end +end