diff --git a/lib/pact_broker/pacts/repository.rb b/lib/pact_broker/pacts/repository.rb index 1bf110f36..f12cdaf1d 100644 --- a/lib/pact_broker/pacts/repository.rb +++ b/lib/pact_broker/pacts/repository.rb @@ -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 @@ -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) diff --git a/spec/lib/pact_broker/pacts/repository_spec.rb b/spec/lib/pact_broker/pacts/repository_spec.rb index 510994cd4..9c243ba7e 100644 --- a/spec/lib/pact_broker/pacts/repository_spec.rb +++ b/spec/lib/pact_broker/pacts/repository_spec.rb @@ -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 @@ -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