Skip to content

Commit

Permalink
feat: add delete integration which deletes all objects pertaining to …
Browse files Browse the repository at this point in the history
…an integration that are not referenced by other objects
  • Loading branch information
bethesque committed Apr 13, 2019
1 parent b5d15d5 commit 718f021
Show file tree
Hide file tree
Showing 25 changed files with 361 additions and 25 deletions.
7 changes: 6 additions & 1 deletion lib/pact_broker/api/resources/integrations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def content_types_provided
end

def allowed_methods
["GET", "OPTIONS"]
["GET", "OPTIONS", "DELETE"]
end

def to_dot
Expand All @@ -20,6 +20,11 @@ def to_dot
def integrations
pact_service.find_latest_pacts
end

def delete_resource
integration_service.delete(consumer_name, provider_name)
true
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/api/resources/pact_versions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def pacts
end

def delete_resource
pact_service.delete_all_pact_versions_between(consumer_name, and: provider_name)
pact_service.delete_all_pact_publications_between(consumer_name, and: provider_name)
true
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/api/resources/tagged_pact_versions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def to_json
end

def delete_resource
pact_service.delete_all_pact_versions_between consumer_name, and: provider_name, tag: identifier_from_path[:tag]
pact_service.delete_all_pact_publications_between consumer_name, and: provider_name, tag: identifier_from_path[:tag]
true
end

Expand Down
5 changes: 5 additions & 0 deletions lib/pact_broker/domain/pacticipant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ def label label_name
end
end

def before_destroy
PactBroker::Domain::Label.where(pacticipant: self).destroy
super
end

def latest_version
versions.last
end
Expand Down
14 changes: 2 additions & 12 deletions lib/pact_broker/domain/verification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class Verification < Sequel::Model
set_primary_key :id
associate(:many_to_one, :pact_version, class: "PactBroker::Pacts::PactVersion", key: :pact_version_id, primary_key: :id)
associate(:many_to_one, :provider_version, class: "PactBroker::Domain::Version", key: :provider_version_id, primary_key: :id)
associate(:many_to_one, :provider, class: "PactBroker::Domain::Pacticipant", key: :provider_id, primary_key: :id)
associate(:many_to_one, :consumer, class: "PactBroker::Domain::Pacticipant", key: :consumer_id, primary_key: :id)
plugin :serialization, :json, :test_results

def before_create
Expand Down Expand Up @@ -74,18 +76,6 @@ def provider_name
provider.name
end

def consumer
Pacticipant.find(id: PactBroker::Pacts::AllPactPublications
.where(pact_version_id: pact_version_id)
.limit(1).select(:consumer_id))
end

def provider
Pacticipant.find(id: PactBroker::Pacts::AllPactPublications
.where(pact_version_id: pact_version_id)
.limit(1).select(:provider_id))
end

def provider_version_number
provider_version.number
end
Expand Down
8 changes: 5 additions & 3 deletions lib/pact_broker/domain/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
require 'pact_broker/repositories/helpers'

module PactBroker

module Domain

class Version < Sequel::Model

set_primary_key :id
one_to_many :pact_publications, order: :revision_number, class: "PactBroker::Pacts::PactPublication", key: :consumer_version_id
associate(:many_to_one, :pacticipant, :class => "PactBroker::Domain::Pacticipant", :key => :pacticipant_id, :primary_key => :id)
Expand All @@ -21,6 +18,11 @@ def after_create
OrderVersions.(self)
end

def before_destroy
PactBroker::Domain::Tag.where(version: self).destroy
super
end

def to_s
"Version: number=#{number}, pacticipant=#{pacticipant_id}"
end
Expand Down
27 changes: 27 additions & 0 deletions lib/pact_broker/integrations/service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'pact_broker/services'
require 'pact_broker/repositories'
require 'pact_broker/logging'

module PactBroker
module Integrations
class Service
extend PactBroker::Repositories
extend PactBroker::Services
include PactBroker::Logging

def self.delete(consumer_name, provider_name)
consumer = pacticipant_service.find_pacticipant_by_name(consumer_name)
provider = pacticipant_service.find_pacticipant_by_name(provider_name)
# this takes care of the triggered webhooks and webhook executions
pact_service.delete_all_pact_publications_between(consumer_name, and: provider_name)
verification_service.delete_all_verifications_between(consumer_name, and: provider_name)
pact_service.delete_all_pact_versions_between(consumer_name, and: provider_name)
webhook_repository.delete_by_consumer_and_provider(consumer, provider)
version_repository.delete_orphan_versions(consumer, provider)

pacticipant_service.delete_if_orphan(consumer)
pacticipant_service.delete_if_orphan(provider)
end
end
end
end
9 changes: 9 additions & 0 deletions lib/pact_broker/pacticipants/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ def create args
def pacticipant_names
PactBroker::Domain::Pacticipant.select(:name).order(:name).collect{ | pacticipant| pacticipant.name }
end

def delete_if_orphan(pacticipant)
if PactBroker::Domain::Version.where(pacticipant: pacticipant).empty? &&
PactBroker::Pacts::PactPublication.where(provider: pacticipant).or(consumer: pacticipant).empty? &&
PactBroker::Pacts::PactVersion.where(provider: pacticipant).or(consumer: pacticipant).empty? &&
PactBroker::Webhooks::Webhook.where(provider: pacticipant).or(consumer: pacticipant).empty?
pacticipant.destroy
end
end
end
end
end
4 changes: 4 additions & 0 deletions lib/pact_broker/pacticipants/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ def self.delete name
connection.run("delete from pacticipants where id = #{pacticipant.id}")
end

def self.delete_if_orphan(pacticipant)
pacticipant_repository.delete_if_orphan(pacticipant)
end

def self.pacticipant_names
pacticipant_repository.pacticipant_names
end
Expand Down
3 changes: 2 additions & 1 deletion lib/pact_broker/pacts/pact_publication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ class PactPublication < Sequel::Model(:pact_publications)

extend Forwardable

delegate [:consumer, :consumer_version_number, :name, :provider_name, :consumer_name] => :cached_domain_for_delegation
delegate [:consumer_version_number, :name, :provider_name, :consumer_name] => :cached_domain_for_delegation

set_primary_key :id
associate(:many_to_one, :provider, :class => "PactBroker::Domain::Pacticipant", :key => :provider_id, :primary_key => :id)
associate(:many_to_one, :consumer, :class => "PactBroker::Domain::Pacticipant", :key => :consumer_id, :primary_key => :id)
associate(:many_to_one, :consumer_version, :class => "PactBroker::Domain::Version", :key => :consumer_version_id, :primary_key => :id)
associate(:many_to_one, :pact_version, class: "PactBroker::Pacts::PactVersion", :key => :pact_version_id, :primary_key => :id)

Expand Down
2 changes: 2 additions & 0 deletions lib/pact_broker/pacts/pact_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ module Pacts
class PactVersion < Sequel::Model(:pact_versions)
one_to_many :pact_publications, reciprocal: :pact_version
one_to_many :verifications, reciprocal: :verification, order: :id, :class => "PactBroker::Domain::Verification"
associate(:many_to_one, :provider, class: "PactBroker::Domain::Pacticipant", key: :provider_id, primary_key: :id)
associate(:many_to_one, :consumer, class: "PactBroker::Domain::Pacticipant", key: :consumer_id, primary_key: :id)

def name
"Pact between #{consumer_name} and #{provider_name}"
Expand Down
8 changes: 7 additions & 1 deletion lib/pact_broker/pacts/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,18 @@ def find_all_pact_versions_between consumer_name, options
.collect(&:to_domain)
end

def delete_all_pact_versions_between consumer_name, options
def delete_all_pact_publications_between consumer_name, options
ids = find_all_database_versions_between(consumer_name, options).select_for_subquery(:id)
webhook_repository.delete_triggered_webhooks_by_pact_publication_ids(ids)
PactPublication.where(id: ids).delete
end

def delete_all_pact_versions_between consumer_name, options
consumer = pacticipant_repository.find_by_name(consumer_name)
provider = pacticipant_repository.find_by_name(options.fetch(:and))
PactVersion.where(consumer: consumer, provider: provider).destroy
end

def find_latest_pact_versions_for_provider provider_name, tag = nil
if tag
LatestTaggedPactPublications.provider(provider_name).order_ignore_case(:consumer_name).where(tag_name: tag).collect(&:to_domain)
Expand Down
4 changes: 4 additions & 0 deletions lib/pact_broker/pacts/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ def find_all_pact_versions_between consumer, options
pact_repository.find_all_pact_versions_between consumer, options
end

def delete_all_pact_publications_between consumer, options
pact_repository.delete_all_pact_publications_between consumer, options
end

def delete_all_pact_versions_between consumer, options
pact_repository.delete_all_pact_versions_between consumer, options
end
Expand Down
5 changes: 5 additions & 0 deletions lib/pact_broker/services.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,10 @@ def certificate_service
require 'pact_broker/certificates/service'
Certificates::Service
end

def integration_service
require 'pact_broker/integrations/service'
Integrations::Service
end
end
end
15 changes: 15 additions & 0 deletions lib/pact_broker/verifications/all_verifications.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@ class AllVerifications < PactBroker::Domain::Verification
set_dataset(:all_verifications)
end

# this view doesn't have a consumer_id
# TODO add it
def consumer
PactBroker::Domain::Pacticipant.find(id: PactBroker::Pacts::AllPactPublications
.where(pact_version_id: pact_version_id)
.limit(1).select(:consumer_id))
end

# this view doesn't have a provider_id
# TODO add it
def provider
PactBroker::Domain::Pacticipant.find(id: PactBroker::Pacts::AllPactPublications
.where(pact_version_id: pact_version_id)
.limit(1).select(:provider_id))
end
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@ module PactBroker
module Verifications
class LatestVerificationForPactVersion < PactBroker::Domain::Verification
set_dataset(:latest_verifications_for_pact_versions)

# this view doesn't have a consumer_id
# TODO add it
def consumer
PactBroker::Domain::Pacticipant.find(id: PactBroker::Pacts::AllPactPublications
.where(pact_version_id: pact_version_id)
.limit(1).select(:consumer_id))
end

# this view doesn't have a provider_id
# TODO add it
def provider
PactBroker::Domain::Pacticipant.find(id: PactBroker::Pacts::AllPactPublications
.where(pact_version_id: pact_version_id)
.limit(1).select(:provider_id))
end
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions lib/pact_broker/verifications/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ def delete_by_provider_version_id version_id
PactBroker::Domain::Verification.where(provider_version_id: version_id).delete
end

def delete_all_verifications_between(consumer_name, options)
consumer = pacticipant_repository.find_by_name(consumer_name)
provider = pacticipant_repository.find_by_name(options.fetch(:and))
PactBroker::Domain::Verification.where(provider: provider, consumer: consumer).destroy
end

def pact_version_id_for pact
PactBroker::Pacts::PactPublication.select(:pact_version_id).where(id: pact.id)
end
Expand Down
4 changes: 4 additions & 0 deletions lib/pact_broker/verifications/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ def verification_summary_for_consumer_version params
pacts = pact_service.find_by_consumer_version(params)
SummaryForConsumerVersion.new(verifications, pacts)
end

def delete_all_verifications_between(consumer_name, options)
verification_repository.delete_all_verifications_between(consumer_name, options)
end
end
end
end
12 changes: 12 additions & 0 deletions lib/pact_broker/versions/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,18 @@ def find_by_pacticipant_id_and_number_or_create pacticipant_id, number
def delete_by_id version_ids
Domain::Version.where(id: version_ids).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]}
# Hope we don't hit max parameter constraints here...
version_ids_to_keep = (version_ids_with_pact_publications + version_ids_with_verifications).uniq

PactBroker::Domain::Version
.where(pacticipant_id: [consumer.id, provider.id])
.exclude(id: (version_ids_with_pact_publications + version_ids_with_verifications).uniq)
.destroy
end
end
end
end
5 changes: 5 additions & 0 deletions lib/pact_broker/webhooks/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ def find_by_consumer_and_provider consumer, provider
Webhook.where(criteria).collect(&:to_domain)
end

def delete_by_consumer_and_provider consumer, provider
Webhook.where(consumer: consumer, provider: provider).destroy
end

def find_for_pact_and_event_name pact, event_name
find_by_consumer_and_or_provider_and_event_name(pact.consumer, pact.provider, event_name)
end
Expand All @@ -102,6 +106,7 @@ def find_by_consumer_and_provider_and_event_name consumer, provider, event_name
.collect(&:to_domain)
end

# TODO delete
def find_by_consumer_and_provider_existing_at consumer, provider, date_time
Webhook.where(consumer_id: consumer.id, provider_id: provider.id)
.where(Sequel.lit("created_at < ?", date_time))
Expand Down
3 changes: 3 additions & 0 deletions lib/pact_broker/webhooks/triggered_webhook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
require 'pact_broker/repositories/helpers'
require 'pact_broker/webhooks/execution'

# Represents the relationship between a webhook and the event and object
# that caused it to be triggered. eg a pact publication

module PactBroker
module Webhooks
class TriggeredWebhook < Sequel::Model(:triggered_webhooks)
Expand Down
1 change: 1 addition & 0 deletions lib/pact_broker/webhooks/webhook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Webhook < Sequel::Model

def before_destroy
WebhookHeader.where(webhook_id: id).destroy
super
end

def update_from_domain webhook
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ module Resources

context "DELETE" do
before do
allow(pact_service).to receive(:delete_all_pact_versions_between)
allow(pact_service).to receive(:delete_all_pact_publications_between)
end

subject { delete(path) }

it "deletes all the pacts with the given consumer/provider/tag" do
expect(pact_service).to receive(:delete_all_pact_versions_between).with("Foo", and: "Bar", tag: "prod")
expect(pact_service).to receive(:delete_all_pact_publications_between).with("Foo", and: "Bar", tag: "prod")
subject
end

Expand Down
Loading

0 comments on commit 718f021

Please sign in to comment.