Skip to content

Commit

Permalink
fix: delete overwritten pact publications when deleting all pact publ…
Browse files Browse the repository at this point in the history
…ications between a consumer and provider
  • Loading branch information
bethesque committed Apr 13, 2019
1 parent 718f021 commit 5456eda
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
11 changes: 8 additions & 3 deletions lib/pact_broker/pacts/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,12 @@ def find_all_pact_versions_between consumer_name, options
end

def delete_all_pact_publications_between consumer_name, options
ids = find_all_database_versions_between(consumer_name, options).select_for_subquery(:id)
consumer = pacticipant_repository.find_by_name(consumer_name)
provider = pacticipant_repository.find_by_name(options.fetch(:and))
query = PactPublication.where(consumer: consumer, provider: provider)
query = query.tag(options[:tag]) if options[:tag]

ids = query.select_for_subquery(:id)
webhook_repository.delete_triggered_webhooks_by_pact_publication_ids(ids)
PactPublication.where(id: ids).delete
end
Expand Down Expand Up @@ -283,10 +288,10 @@ def create_pact_version consumer_id, provider_id, sha, json_content
pact_version.save
end

def find_all_database_versions_between(consumer_name, options)
def find_all_database_versions_between(consumer_name, options, base_class = LatestPactPublicationsByConsumerVersion)
provider_name = options.fetch(:and)

query = LatestPactPublicationsByConsumerVersion
query = base_class
.consumer(consumer_name)
.provider(provider_name)

Expand Down
5 changes: 4 additions & 1 deletion spec/lib/pact_broker/pacts/repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ module Pacts
.create_consumer_version("1.2.3")
.create_provider(provider_name)
.create_pact
.revise_pact
.comment("The overwritten pact needs to be deleted too")
.create_webhook
.create_triggered_webhook
.create_webhook_execution
Expand All @@ -330,12 +332,13 @@ module Pacts
.create_pact
.create_provider("Another Provider")
.create_pact
.comment("This one won't be deleted")
end

subject { Repository.new.delete_all_pact_publications_between(consumer_name, :and => provider_name) }

it "deletes the pacts between the specified consumer and provider" do
expect { subject }.to change { PactPublication.count }.by(-2)
expect { subject }.to change { PactPublication.count }.by(-3)
end

context "with a tag" do
Expand Down

0 comments on commit 5456eda

Please sign in to comment.