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
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/pact_broker/db/clean.rb', line 16

def call
  deleted_counts = {}
  kept_counts = {}
  date = options[:date]

  version_ids_to_keep = nil

  if options[:keep]
    version_ids_to_keep = options[:keep].collect do | selector |
      PactBroker::Domain::Version.select(:id).for_selector(selector)
    end.reduce(&:union)
  end

  pact_publication_ids_to_delete = if date
    db[:pact_publications].select(:id).where(Sequel.lit('created_at < ?', date))
  elsif version_ids_to_keep
    db[:pact_publications].select(:id).where(consumer_version_id: version_ids_to_keep).invert
  else
    keep = db[:latest_tagged_pact_publications].select(:id).union(db[:latest_pact_publications].select(:id))
    db[:pact_publications].select(:id).where(id: keep).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