diff --git a/db/ddl_statements/head_pact_tags.rb b/db/ddl_statements/head_pact_tags.rb index 08e698547..49a2bbc92 100644 --- a/db/ddl_statements/head_pact_tags.rb +++ b/db/ddl_statements/head_pact_tags.rb @@ -5,6 +5,21 @@ def head_pact_tags_v1(connection) Sequel[:lp][:consumer_id] => Sequel[:o][:consumer_id], Sequel[:lp][:provider_id] => Sequel[:o][:provider_id], Sequel[:cv][:order] => Sequel[:o][:latest_consumer_version_order] - }, { table_alias: :o}) + }, { table_alias: :o }) .select(Sequel[:o][:tag_name].as(:name), Sequel[:lp][:pact_publication_id]) end + +def head_pact_tags_v2(connection) + connection.from(Sequel.as(:latest_pact_publication_ids_for_consumer_versions, :lp)) + .join(:versions,{ Sequel[:lp][:consumer_version_id] => Sequel[:cv][:id]}, { table_alias: :cv }) + .join(:latest_tagged_pact_consumer_version_orders, { + Sequel[:lp][:consumer_id] => Sequel[:o][:consumer_id], + Sequel[:lp][:provider_id] => Sequel[:o][:provider_id], + Sequel[:cv][:order] => Sequel[:o][:latest_consumer_version_order] + }, { table_alias: :o } ) + .join(:tags, { + Sequel[:tags][:version_id] => Sequel[:cv][:id], + Sequel[:tags][:name] => Sequel[:o][:tag_name] + }) + .select(Sequel[:o][:tag_name].as(:name), Sequel[:lp][:pact_publication_id], Sequel[:tags][:created_at]) +end diff --git a/db/migrations/20210202_add_created_at_to_head_pact_tags.rb b/db/migrations/20210202_add_created_at_to_head_pact_tags.rb new file mode 100644 index 000000000..7279e1570 --- /dev/null +++ b/db/migrations/20210202_add_created_at_to_head_pact_tags.rb @@ -0,0 +1,11 @@ +require_relative '../ddl_statements' + +Sequel.migration do + up do + create_or_replace_view(:head_pact_tags, head_pact_tags_v2(self)) + end + + down do + create_or_replace_view(:head_pact_tags, head_pact_tags_v1(self)) + end +end diff --git a/lib/pact_broker/index/service.rb b/lib/pact_broker/index/service.rb index 55cbb8cf9..26f53a898 100644 --- a/lib/pact_broker/index/service.rb +++ b/lib/pact_broker/index/service.rb @@ -72,7 +72,7 @@ def self.find_index_items options = {} latest_verification, webhook ? [webhook]: [], pact_publication.integration.latest_triggered_webhooks, - consumer_version_tags(pact_publication, options[:tags]), + consumer_version_tags(pact_publication, options[:tags]).sort_by(&:created_at).collect(&:name), options[:tags] && latest_verification ? latest_verification.provider_version.tags_with_latest_flag.select(&:latest?) : [] ) end.sort @@ -98,9 +98,9 @@ def self.latest_verification_for_pseudo_branch(pact_publication, is_overall_late def self.consumer_version_tags(pact_publication, tags_option) if tags_option == true - pact_publication.head_pact_tags.collect(&:name) + pact_publication.head_pact_tags elsif tags_option.is_a?(Array) - pact_publication.head_pact_tags.collect(&:name) & tags_option + pact_publication.head_pact_tags.select { |tag| tags_option.include?(tag.name) } else [] end @@ -133,7 +133,7 @@ def self.find_index_items_for_api(consumer_name: nil, provider_name: nil, **igno pact_publication.latest_verification, [], [], - pact_publication.head_pact_tags.collect(&:name), + pact_publication.head_pact_tags.sort_by(&:created_at).collect(&:name), pact_publication.latest_verification ? pact_publication.latest_verification.provider_version.tags_with_latest_flag.select(&:latest?) : [] ) end.sort