From 824c516a068b543667a2ef5bb4d0301e979de657 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Tue, 5 Sep 2023 06:33:21 +1000 Subject: [PATCH] feat(matrix): optimise identification of the 'latest tag' --- lib/pact_broker/domain/verification.rb | 1 - lib/pact_broker/domain/version.rb | 16 ------------- lib/pact_broker/matrix/every_row.rb | 5 ++-- lib/pact_broker/matrix/matrix_row.rb | 6 ++--- .../matrix/matrix_row_dataset_module.rb | 4 ++-- lib/pact_broker/tags/tag_with_latest_flag.rb | 24 ------------------- lib/pact_broker/ui/view_models/matrix_line.rb | 8 +++---- .../decorators/verification_decorator_spec.rb | 2 +- .../verification_summary_decorator_spec.rb | 2 +- .../lib/pact_broker/matrix/repository_spec.rb | 2 +- .../ui/view_models/index_item_spec.rb | 4 ++-- 11 files changed, 16 insertions(+), 58 deletions(-) delete mode 100644 lib/pact_broker/tags/tag_with_latest_flag.rb diff --git a/lib/pact_broker/domain/verification.rb b/lib/pact_broker/domain/verification.rb index 71183bbc1..7506a3ea0 100644 --- a/lib/pact_broker/domain/verification.rb +++ b/lib/pact_broker/domain/verification.rb @@ -1,6 +1,5 @@ require "pact_broker/dataset" require "json" -require "pact_broker/tags/tag_with_latest_flag" require "pact_broker/pacts/content" require "sequel/extensions/symbol_aref_refinement" diff --git a/lib/pact_broker/domain/version.rb b/lib/pact_broker/domain/version.rb index 38182aa50..7e053710a 100644 --- a/lib/pact_broker/domain/version.rb +++ b/lib/pact_broker/domain/version.rb @@ -1,25 +1,9 @@ require "pact_broker/dataset" require "pact_broker/domain/order_versions" -require "pact_broker/tags/tag_with_latest_flag" require "pact_broker/versions/eager_loaders" module PactBroker module Domain - - # Same attributes as PactBroker::Tags::TagWithLatestFlag - class EagerTagWithLatestFlag < SimpleDelegator - attr_reader :latest - - def initialize(tag, latest) - super(tag) - @latest = latest - end - - def latest? - latest - end - end - VERSION_COLUMNS = [:id, :number, :repository_ref, :pacticipant_id, :order, :created_at, :updated_at, :build_url] class Version < Sequel::Model(Sequel::Model.db[:versions].select(*VERSION_COLUMNS.collect{ | column | Sequel.qualify(:versions, column) })) diff --git a/lib/pact_broker/matrix/every_row.rb b/lib/pact_broker/matrix/every_row.rb index b58b4e1cb..b63f893b9 100644 --- a/lib/pact_broker/matrix/every_row.rb +++ b/lib/pact_broker/matrix/every_row.rb @@ -36,9 +36,8 @@ class Verification < Sequel::Model(:verifications) associate(:many_to_one, :provider_version, :class => "PactBroker::Domain::Version", :key => :provider_version_id, :primary_key => :id) associate(:many_to_one, :pact_version, class: "PactBroker::Pacts::PactVersion", :key => :pact_version_id, :primary_key => :id) associate(:many_to_one, :verification, class: "PactBroker::Domain::Verification", :key => :verification_id, :primary_key => :id) - associate(:one_to_many, :consumer_version_tags, :class => "PactBroker::Tags::TagWithLatestFlag", primary_key: :consumer_version_id, key: :version_id) - associate(:one_to_many, :provider_version_tags, :class => "PactBroker::Tags::TagWithLatestFlag", primary_key: :provider_version_id, key: :version_id) - + associate(:one_to_many, :consumer_version_tags, :class => "PactBroker::Domain::Tag", primary_key: :consumer_version_id, key: :version_id) + associate(:one_to_many, :provider_version_tags, :class => "PactBroker::Domain::Tag", primary_key: :provider_version_id, key: :version_id) PACT_COLUMNS_WITH_ALIASES = [ Sequel[:p][:consumer_id], diff --git a/lib/pact_broker/matrix/matrix_row.rb b/lib/pact_broker/matrix/matrix_row.rb index e75c02993..abf66a3e3 100644 --- a/lib/pact_broker/matrix/matrix_row.rb +++ b/lib/pact_broker/matrix/matrix_row.rb @@ -4,8 +4,8 @@ require "pact_broker/domain/pacticipant" require "pact_broker/domain/version" require "pact_broker/domain/verification" +require "pact_broker/domain/tag" require "pact_broker/pacts/pact_publication" -require "pact_broker/tags/tag_with_latest_flag" require "pact_broker/matrix/matrix_row_dataset_module" require "pact_broker/matrix/matrix_row_instance_methods" require "pact_broker/matrix/matrix_row_verification_dataset_module" @@ -66,8 +66,8 @@ class Verification < Sequel::Model(Sequel.as(:latest_verification_id_for_pact_ve associate(:many_to_one, :provider_version, :class => "PactBroker::Domain::Version", :key => :provider_version_id, :primary_key => :id) associate(:many_to_one, :pact_version, class: "PactBroker::Pacts::PactVersion", :key => :pact_version_id, :primary_key => :id) associate(:many_to_one, :verification, class: "PactBroker::Domain::Verification", :key => :verification_id, :primary_key => :id) - associate(:one_to_many, :consumer_version_tags, :class => "PactBroker::Tags::TagWithLatestFlag", primary_key: :consumer_version_id, key: :version_id) - associate(:one_to_many, :provider_version_tags, :class => "PactBroker::Tags::TagWithLatestFlag", primary_key: :provider_version_id, key: :version_id) + associate(:one_to_many, :consumer_version_tags, :class => "PactBroker::Domain::Tag", primary_key: :consumer_version_id, key: :version_id) + associate(:one_to_many, :provider_version_tags, :class => "PactBroker::Domain::Tag", primary_key: :provider_version_id, key: :version_id) dataset_module do include PactBroker::Dataset diff --git a/lib/pact_broker/matrix/matrix_row_dataset_module.rb b/lib/pact_broker/matrix/matrix_row_dataset_module.rb index fd9b7946b..c70d01b8f 100644 --- a/lib/pact_broker/matrix/matrix_row_dataset_module.rb +++ b/lib/pact_broker/matrix/matrix_row_dataset_module.rb @@ -36,8 +36,8 @@ def eager_all_the_things :pact_version, consumer_version: EAGER_LOADED_RELATIONSHIPS_FOR_VERSION, provider_version: EAGER_LOADED_RELATIONSHIPS_FOR_VERSION, - consumer_version_tags: { version: :pacticipant }, - provider_version_tags: { version: :pacticipant } + consumer_version_tags: [:head_tag, { version: :pacticipant }], + provider_version_tags: [:head_tag, { version: :pacticipant }] ) end diff --git a/lib/pact_broker/tags/tag_with_latest_flag.rb b/lib/pact_broker/tags/tag_with_latest_flag.rb deleted file mode 100644 index 36b0c89a8..000000000 --- a/lib/pact_broker/tags/tag_with_latest_flag.rb +++ /dev/null @@ -1,24 +0,0 @@ -require "pact_broker/dataset" - -module PactBroker - module Tags - # The tag associated with the latest verification for a given tag - # TODO remove this class now we have eager loaders for head_tag - class TagWithLatestFlag < Sequel::Model(:tags_with_latest_flag) - associate(:many_to_one, :version, :class => "PactBroker::Domain::Version", :key => :version_id, :primary_key => :id) - dataset_module(PactBroker::Dataset) - - def latest? - !values[:latest].nil? - end - end - end -end - -# Table: tags_with_latest_flag -# Columns: -# name | text | -# version_id | integer | -# created_at | timestamp without time zone | -# updated_at | timestamp without time zone | -# latest | integer | diff --git a/lib/pact_broker/ui/view_models/matrix_line.rb b/lib/pact_broker/ui/view_models/matrix_line.rb index c4e10d2f8..687a9d90e 100644 --- a/lib/pact_broker/ui/view_models/matrix_line.rb +++ b/lib/pact_broker/ui/view_models/matrix_line.rb @@ -110,14 +110,14 @@ def consumer_version_branches def latest_consumer_version_tags @line.consumer_version_tags - .select(&:latest) + .select(&:latest?) .sort_by(&:created_at) .collect{ | tag | MatrixTag.new(tag.to_hash.merge(pacticipant_name: consumer_name, version_number: consumer_version_number)) } end def other_consumer_version_tags @line.consumer_version_tags - .reject(&:latest) + .reject(&:latest?) .sort_by(&:created_at) .collect{ | tag | MatrixTag.new(tag.to_hash.merge(pacticipant_name: consumer_name, version_number: consumer_version_number)) } end @@ -138,14 +138,14 @@ def provider_version_branches def latest_provider_version_tags @line.provider_version_tags - .select(&:latest) + .select(&:latest?) .sort_by(&:created_at) .collect{ | tag | MatrixTag.new(tag.to_hash.merge(pacticipant_name: provider_name, version_number: provider_version_number)) } end def other_provider_version_tags @line.provider_version_tags - .reject(&:latest) + .reject(&:latest?) .sort_by(&:created_at) .collect{ | tag | MatrixTag.new(tag.to_hash.merge(pacticipant_name: provider_name, version_number: provider_version_number)) } end diff --git a/spec/lib/pact_broker/api/decorators/verification_decorator_spec.rb b/spec/lib/pact_broker/api/decorators/verification_decorator_spec.rb index f8c5e74fe..172553374 100644 --- a/spec/lib/pact_broker/api/decorators/verification_decorator_spec.rb +++ b/spec/lib/pact_broker/api/decorators/verification_decorator_spec.rb @@ -35,7 +35,7 @@ module Decorators ) end - let(:provider_version_tags) { [instance_double(PactBroker::Tags::TagWithLatestFlag, name: "prod", latest?: true)] } + let(:provider_version_tags) { [instance_double(PactBroker::Domain::Tag, name: "prod", latest?: true)] } let(:options) { { user_options: { base_url: "http://example.org", pact: pact } } } diff --git a/spec/lib/pact_broker/api/decorators/verification_summary_decorator_spec.rb b/spec/lib/pact_broker/api/decorators/verification_summary_decorator_spec.rb index 45f92d1aa..af380a58c 100644 --- a/spec/lib/pact_broker/api/decorators/verification_summary_decorator_spec.rb +++ b/spec/lib/pact_broker/api/decorators/verification_summary_decorator_spec.rb @@ -30,7 +30,7 @@ module Decorators instance_double("PactBroker::Pacts::PactVersion", name: "Name") end - let(:provider_version_tags) { [instance_double(PactBroker::Tags::TagWithLatestFlag, name: "prod", latest?: true)] } + let(:provider_version_tags) { [instance_double(PactBroker::Domain::Tag, name: "prod", latest?: true)] } let(:pact) { instance_double("PactBroker::Domain::Pact", name: "Some pact", consumer_name: "Foo", provider_name: "Bar", consumer_version_number: "1.2.3", pact_version_sha: "1234") } let(:is_pending) { true } let(:options) { {base_url: "http://example.org", consumer_name: "Foo", consumer_version_number: "1.2.3", resource_url: "http://self"} } diff --git a/spec/lib/pact_broker/matrix/repository_spec.rb b/spec/lib/pact_broker/matrix/repository_spec.rb index 3efb32886..e89e99f0a 100644 --- a/spec/lib/pact_broker/matrix/repository_spec.rb +++ b/spec/lib/pact_broker/matrix/repository_spec.rb @@ -594,7 +594,7 @@ def shorten_rows rows end it "returns the tag information" do - expect(subject.first.provider_version_tags).to include_hash_matching name: "prod", latest: 1 + expect(subject.first.provider_version_tags).to include(have_attributes(name: "prod", latest?: true)) end end diff --git a/spec/lib/pact_broker/ui/view_models/index_item_spec.rb b/spec/lib/pact_broker/ui/view_models/index_item_spec.rb index 13b886d2d..1cd73fb6d 100644 --- a/spec/lib/pact_broker/ui/view_models/index_item_spec.rb +++ b/spec/lib/pact_broker/ui/view_models/index_item_spec.rb @@ -14,8 +14,8 @@ module ViewDomain let(:latest_verification) { instance_double("PactBroker::Domain::Verification") } let(:domain_relationship) { PactBroker::Domain::IndexItem.new(consumer, provider, consumer_version, latest_pact, latest, latest_verification, [], [], tags, latest_verification_latest_tags)} let(:tags) { [] } - let(:verification_tag_1) { instance_double("PactBroker::Tags::TagWithLatestFlag", name: "dev") } - let(:verification_tag_2) { instance_double("PactBroker::Tags::TagWithLatestFlag", name: "prod") } + let(:verification_tag_1) { instance_double("PactBroker::Domain::Tag", name: "dev") } + let(:verification_tag_2) { instance_double("PactBroker::Domain::Tag", name: "prod") } let(:latest_verification_latest_tags) { [verification_tag_1, verification_tag_2] } let(:latest) { true }