From e644cb209f6d0b8aa23c2ddfd43acf3bacc307c2 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Sun, 29 Oct 2023 18:51:54 +1100 Subject: [PATCH] feat: add method to delete orphan versions associated with deleted branch --- lib/pact_broker/versions/branch_repository.rb | 4 +++ lib/pact_broker/versions/repository.rb | 16 +++++++++++ spec/features/delete_branch_spec.rb | 2 +- .../pact_broker/versions/repository_spec.rb | 27 +++++++++++++++++++ 4 files changed, 48 insertions(+), 1 deletion(-) diff --git a/lib/pact_broker/versions/branch_repository.rb b/lib/pact_broker/versions/branch_repository.rb index 7e41b00f5..1ce658377 100644 --- a/lib/pact_broker/versions/branch_repository.rb +++ b/lib/pact_broker/versions/branch_repository.rb @@ -38,6 +38,10 @@ def find_branch(pacticipant_name:, branch_name:) def delete_branch(branch) branch.delete end + + def delete_branch_and_associated_versions(branch) + branch.delete + end end end end diff --git a/lib/pact_broker/versions/repository.rb b/lib/pact_broker/versions/repository.rb index 28091d084..699559cb0 100644 --- a/lib/pact_broker/versions/repository.rb +++ b/lib/pact_broker/versions/repository.rb @@ -169,6 +169,22 @@ def delete_by_id version_ids nil end + # @param [PactBroker::Versions::Branch] branch + def delete_by_branch(branch) + version_ids_for_branch = PactBroker::Versions::BranchVersion.select(:version_id).distinct.where(branch_id: branch.id) + + # Do not delete versions that have other branches + ids_of_versions_only_on_this_branch = PactBroker::Versions::BranchVersion + .group_and_count(Sequel[:branch_versions][:version_id]) + .join(version_ids_for_branch, { Sequel[:branch_versions][:version_id] => Sequel[:vb][:version_id] }, { table_alias: :vb}) + .group(Sequel[:branch_versions][:version_id]) + .having(count: 1) + .all + .collect(&:version_id) + + Domain::Version.where(id: ids_of_versions_only_on_this_branch).delete + end + def delete_orphan_versions consumer, provider version_ids_with_pact_publications = PactBroker::Pacts::PactPublication.where(consumer_id: [consumer.id, provider.id]).select(:consumer_version_id).collect{|r| r[:consumer_version_id]} version_ids_with_verifications = PactBroker::Domain::Verification.where(provider_id: [provider.id, consumer.id]).select(:provider_version_id).collect{|r| r[:provider_version_id]} diff --git a/spec/features/delete_branch_spec.rb b/spec/features/delete_branch_spec.rb index b67ef29b6..b4f308449 100644 --- a/spec/features/delete_branch_spec.rb +++ b/spec/features/delete_branch_spec.rb @@ -33,7 +33,7 @@ end context "when there is some flag to indicate that the versions should be deleted too" do - subject { delete(path, { deletedAssociatedVersions: true }, headers) } + subject { delete(path, { deleteVersions: true }, headers) } it "deletes the branch" do expect { subject }.to change { PactBroker::Versions::Branch.count }.by(-1) diff --git a/spec/lib/pact_broker/versions/repository_spec.rb b/spec/lib/pact_broker/versions/repository_spec.rb index 356a664d6..b66dba643 100644 --- a/spec/lib/pact_broker/versions/repository_spec.rb +++ b/spec/lib/pact_broker/versions/repository_spec.rb @@ -1,4 +1,5 @@ require "pact_broker/versions/repository" +require "pact_broker/versions/branch_repository" module PactBroker module Versions @@ -182,6 +183,32 @@ module Versions end end + describe "#delete_by_branch" do + before do + td.create_consumer("foo") + .create_consumer_version("1234", branch: "main") + .create_consumer_version("1234", branch: "foo") # 2nd branch + .create_consumer_version("555", branch: "main") + .create_consumer_version("777", branch: "blah") + .create_consumer("bar") + .create_consumer_version("1234", branch: "main") + end + + let(:branch) { PactBroker::Versions::BranchRepository.new.find_branch(pacticipant_name: "foo", branch_name: "main") } + + subject { Repository.new.delete_by_branch(branch) } + + it "deletes versions that belong only to the branch that is being deleted" do + expect(td.find_version("foo", "555")).to_not be_nil + expect { subject }.to change { PactBroker::Domain::Version.count }.by(-1) + expect(td.find_version("foo", "555")).to be_nil + end + + it "only deletes the branch_versions associated with the versions that were deleted" do + expect{ subject }.to change { PactBroker::Versions::BranchVersion.count }.by(-1) + end + end + describe "#find_by_pacticipant_name_and_number" do subject { described_class.new.find_by_pacticipant_name_and_number pacticipant_name, version_number }