Skip to content

Commit

Permalink
feat: add method to delete orphan versions associated with deleted br…
Browse files Browse the repository at this point in the history
…anch
  • Loading branch information
bethesque committed Oct 29, 2023
1 parent 74f049b commit e644cb2
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/pact_broker/versions/branch_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 16 additions & 0 deletions lib/pact_broker/versions/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]}
Expand Down
2 changes: 1 addition & 1 deletion spec/features/delete_branch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
27 changes: 27 additions & 0 deletions spec/lib/pact_broker/versions/repository_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "pact_broker/versions/repository"
require "pact_broker/versions/branch_repository"

module PactBroker
module Versions
Expand Down Expand Up @@ -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 }
Expand Down

0 comments on commit e644cb2

Please sign in to comment.