Class: PactBroker::DB::Clean

Inherits:
Object
  • Object
show all
Defined in:
lib/pact_broker/db/clean.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(database_connection, options = {}) ⇒ Clean

Returns a new instance of Clean.



11
12
13
14
# File 'lib/pact_broker/db/clean.rb', line 11

def initialize database_connection, options = {}
  @db = database_connection
  @options = options
end

Class Method Details

.call(database_connection, options = {}) ⇒ Object



7
8
9
# File 'lib/pact_broker/db/clean.rb', line 7

def self.call database_connection, options = {}
  new(database_connection, options).call
end

Instance Method Details

#callObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/pact_broker/db/clean.rb', line 16

def call
  deleted_counts = {}
  kept_counts = {}
  date = options[:date]
  pact_publication_ids_to_delete = if date
    db[:pact_publications].select(:id).where(Sequel.lit('created_at < ?', date))
  else
    db[:pact_publications].select(:id).where(id: db[:head_matrix].select(:pact_publication_id)).invert
  end

  deleted_counts[:pact_publications] = pact_publication_ids_to_delete.count
  kept_counts[:pact_publications] = db[:pact_publications].where(id: pact_publication_ids_to_delete).invert.count

  # TODO head matrix is the head for the consumer tags, not the provider tags.
  # Work out how to keep the head verifications for the provider tags.
  verification_ids = get_verification_ids(pact_publication_ids_to_delete)
  deleted_counts[:verification_results] = verification_ids.count
  kept_counts[:verification_results] = db[:verifications].where(id:verification_ids ).invert.count

  delete_webhook_data(db[:triggered_webhooks].where(verification_id: verification_ids).select(:id))
  verification_ids.delete

  delete_webhook_data(db[:triggered_webhooks].where(pact_publication_id: pact_publication_ids_to_delete).select(:id))
  delete_deprecated_webhook_executions(pact_publication_ids_to_delete)
  delete_pact_publications(pact_publication_ids_to_delete)

  delete_orphan_pact_versions
  overwritten_delete_counts = delete_overwritten_verifications
  deleted_counts[:verification_results] = deleted_counts[:verification_results] + overwritten_delete_counts[:verification_results]
  kept_counts[:verification_results] = kept_counts[:verification_results] - overwritten_delete_counts[:verification_results]


  referenced_version_ids = db[:pact_publications].select(:consumer_version_id).collect{ | h| h[:consumer_version_id] } +
    db[:verifications].select(:provider_version_id).collect{ | h| h[:provider_version_id] }

  delete_orphan_tags(referenced_version_ids)
  delete_orphan_versions(referenced_version_ids)

  { kept: kept_counts, deleted: deleted_counts }
end