From d7e2ef27a6e7e3e1272293b592cbf18282eb03e5 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Mon, 6 Jan 2020 08:29:21 +1100 Subject: [PATCH] feat: support DELETE /integrations for deleting all integration related data at once (pacticipants, pacts, verifications and webhooks) --- lib/pact_broker/api/resources/integrations.rb | 2 +- lib/pact_broker/db/models.rb | 37 +++++++++++++++++++ lib/pact_broker/integrations/service.rb | 9 +++++ lib/pact_broker/test/test_data_builder.rb | 10 +++++ .../pact_broker/integrations/service_spec.rb | 10 +++++ 5 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 lib/pact_broker/db/models.rb diff --git a/lib/pact_broker/api/resources/integrations.rb b/lib/pact_broker/api/resources/integrations.rb index b492a5985..23245af1a 100644 --- a/lib/pact_broker/api/resources/integrations.rb +++ b/lib/pact_broker/api/resources/integrations.rb @@ -30,7 +30,7 @@ def integrations end def delete_resource - integration_service.delete(consumer_name, provider_name) + integration_service.delete_all true end end diff --git a/lib/pact_broker/db/models.rb b/lib/pact_broker/db/models.rb new file mode 100644 index 000000000..dad0a3130 --- /dev/null +++ b/lib/pact_broker/db/models.rb @@ -0,0 +1,37 @@ +require 'pact_broker/webhooks/execution' +require 'pact_broker/webhooks/triggered_webhook' +require 'pact_broker/webhooks/webhook' +require 'pact_broker/pacts/latest_pact_publication_id_by_consumer_version' +require 'pact_broker/verifications/latest_verification_id_for_pact_version_and_provider_version' +require 'pact_broker/pacts/pact_publication' +require 'pact_broker/pacts/pact_version' +require 'pact_broker/domain/verification' +require 'pact_broker/domain/tag' +require 'pact_broker/domain/version' +require 'pact_broker/domain/label' +require 'pact_broker/domain/pacticipant' + +module PactBroker + INTEGRATIONS_TABLES = [ + PactBroker::Webhooks::Execution, + PactBroker::Webhooks::TriggeredWebhook, + PactBroker::Webhooks::Webhook, + PactBroker::Pacts::LatestPactPublicationIdForConsumerVersion, + PactBroker::Verifications::LatestVerificationIdForPactVersionAndProviderVersion, + PactBroker::Domain::Verification, + PactBroker::Pacts::PactPublication, + PactBroker::Pacts::PactVersion, + PactBroker::Domain::Tag, + PactBroker::Domain::Version, + PactBroker::Domain::Label, + PactBroker::Domain::Pacticipant + ] + + module DB + def self.each_integration_model + INTEGRATIONS_TABLES.each do | model | + yield model + end + end + end +end diff --git a/lib/pact_broker/integrations/service.rb b/lib/pact_broker/integrations/service.rb index cc2a92d5f..1a36f7879 100644 --- a/lib/pact_broker/integrations/service.rb +++ b/lib/pact_broker/integrations/service.rb @@ -2,6 +2,7 @@ require 'pact_broker/repositories' require 'pact_broker/logging' require 'pact_broker/integrations/integration' +require 'pact_broker/db/models' module PactBroker module Integrations @@ -41,6 +42,14 @@ def self.delete(consumer_name, provider_name) pacticipant_service.delete_if_orphan(consumer) pacticipant_service.delete_if_orphan(provider) unless consumer == provider end + + def self.delete_all + # TODO move all these into their own repositories + PactBroker::DB.each_integration_model do | model | + logger.info("Truncating ", model.table_name) + model.truncate + end + end end end end diff --git a/lib/pact_broker/test/test_data_builder.rb b/lib/pact_broker/test/test_data_builder.rb index 17dc403dc..d618f7fe0 100644 --- a/lib/pact_broker/test/test_data_builder.rb +++ b/lib/pact_broker/test/test_data_builder.rb @@ -332,6 +332,16 @@ def create_certificate options = {path: 'spec/fixtures/single-certificate.pem'} self end + def create_everything_for_an_integration + create_pact_with_verification("Foo", "1", "Bar", "2") + .create_label("label") + .create_consumer_version_tag("master") + .create_provider_version_tag("master") + .create_webhook + .create_triggered_webhook + .create_webhook_execution + end + def model_counter @@model_counter ||= 0 @@model_counter += 1 diff --git a/spec/lib/pact_broker/integrations/service_spec.rb b/spec/lib/pact_broker/integrations/service_spec.rb index a82ee0c63..a1d2c8f0a 100644 --- a/spec/lib/pact_broker/integrations/service_spec.rb +++ b/spec/lib/pact_broker/integrations/service_spec.rb @@ -249,6 +249,16 @@ module Integrations end end end + + describe "delete_all" do + before do + td.create_everything_for_an_integration + end + + it "doesn't blow up" do + Service.delete_all + end + end end end end