From 53b171bdcdb6a426f8befe0869a134d90bd322f4 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Wed, 22 Feb 2023 13:18:38 +1100 Subject: [PATCH] fix: fix error raised when clean selectors include a selector with a max age and a selector with a max age and a branch Closes: https://github.com/pact-foundation/pact_broker/issues/579 --- lib/pact_broker/db/clean_incremental.rb | 2 +- .../pact_broker/db/clean_incremental_spec.rb | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/pact_broker/db/clean_incremental.rb b/lib/pact_broker/db/clean_incremental.rb index ed1ada374..3eb63d9fc 100644 --- a/lib/pact_broker/db/clean_incremental.rb +++ b/lib/pact_broker/db/clean_incremental.rb @@ -83,7 +83,7 @@ def versions_to_delete(columns = [:id]) end def version_ids_to_keep - @version_ids_to_keep ||= keep.collect { |selector| PactBroker::Domain::Version.select(:id).for_selector(selector) }.reduce(&:union) + @version_ids_to_keep ||= keep.collect { |selector| PactBroker::Domain::Version.for_selector(selector).select(:id) }.reduce(&:union) end def current_counts diff --git a/spec/lib/pact_broker/db/clean_incremental_spec.rb b/spec/lib/pact_broker/db/clean_incremental_spec.rb index 8a8b2ddec..ced2e0977 100644 --- a/spec/lib/pact_broker/db/clean_incremental_spec.rb +++ b/spec/lib/pact_broker/db/clean_incremental_spec.rb @@ -1,5 +1,6 @@ require "pact_broker/db/clean_incremental" require "pact_broker/matrix/unresolved_selector" +require "timecop" module PactBroker module DB @@ -130,6 +131,31 @@ def pact_publication_count_for(consumer_name, version_number) expect { subject }.to change { PactBroker::Pacts::PactVersion.count }.by(-1) end end + + context "when there is a selector with a branch and a max age, and a selector with a max age only" do + before do + Timecop.freeze(Date.today - 20) do + td.publish_pact(consumer_name: "Foo", provider_name: "Bar", consumer_version_number: "1", branch: "main") + td.publish_pact(consumer_name: "Foo", provider_name: "Bar", consumer_version_number: "2", branch: "feat/foo") + end + Timecop.freeze(Date.today - 10) do + td.publish_pact(consumer_name: "Foo", provider_name: "Bar", consumer_version_number: "3", branch: "main") + end + td.publish_pact(consumer_name: "Foo", provider_name: "Bar", consumer_version_number: "4", branch: "feat/foo") + end + + let(:options) { { keep: [ { max_age: 5 }, { max_age: 15, branch: "main" } ] } } + + it "applies the max age correctly by branch" do + expect { subject }.to change { + PactBroker::Domain::Version.join(:branch_versions, { version_id: :id }) + .order(:order) + .select_map([:number, :branch_name]) + } + .from([["1", "main"], ["2", "feat/foo"], ["3", "main"], ["4", "feat/foo"]]) + .to([["3", "main"], ["4", "feat/foo"]]) + end + end end end end