Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add method to delete orphan versions associated with deleted branch #711

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/pact_broker/versions/branch_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ def delete_branch(branch)
branch.delete
end

def delete_branch_and_associated_versions(branch)
# TODO
branch.delete
end

# @param [PactBroker::Domain::Pacticipant] pacticipant
# @params [Array<String>] exclude the names of the branches to NOT delete
# @param [Integer] the number of branches that will be deleted
Expand Down
16 changes: 16 additions & 0 deletions lib/pact_broker/versions/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,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 @@ -157,6 +158,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
Loading