Class: PactBroker::DB::Clean

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/pact_broker/db/clean.rb,
lib/pact_broker/db/clean/selector.rb

Defined Under Namespace

Classes: Selector, Unionable

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

included, #log_error, #log_with_tag

Constructor Details

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

Returns a new instance of Clean.



21
22
23
24
# File 'lib/pact_broker/db/clean.rb', line 21

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

Class Method Details

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



17
18
19
# File 'lib/pact_broker/db/clean.rb', line 17

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

Instance Method Details

#callObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/pact_broker/db/clean.rb', line 107

def call
  deleted_counts = {}
  kept_counts = {}

  deleted_counts[:pact_publications] = pact_publication_ids_to_delete.count
  kept_counts[:pact_publications] = pact_publication_ids_to_keep.count

  # Work out how to keep the head verifications for the provider tags.

  deleted_counts[:verification_results] = verification_ids_to_delete.count
  kept_counts[:verification_results] = verification_ids_to_keep.count

  delete_webhook_data(verification_triggered_webhook_ids_to_delete)
  delete_verifications

  delete_webhook_data(pact_publication_triggered_webhook_ids_to_delete)
  delete_pact_publications

  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]

  delete_orphan_tags
  delete_orphan_versions

  { kept: kept_counts, deleted: deleted_counts }
end

#keepObject



26
27
28
29
30
31
32
33
# File 'lib/pact_broker/db/clean.rb', line 26

def keep
  @keep ||= if options[:keep]
              # Could be a Matrix::UnresolvedSelector from the docker image, convert it
              options[:keep].collect { | unknown_thing | Selector.from_hash(unknown_thing.to_hash) }
            else
              [Selector.new(tag: true, latest: true), Selector.new(branch: true, latest: true), Selector.new(latest: true), Selector.new(deployed: true), Selector.new(released: true)]
            end
end

#latest_pact_publication_ids_to_keepObject



57
58
59
# File 'lib/pact_broker/db/clean.rb', line 57

def latest_pact_publication_ids_to_keep
  @latest_pact_publication_ids_to_keep ||= resolve_ids(db[:latest_pact_publications].select(:id))
end

#latest_tagged_pact_publications_ids_to_keepObject



50
51
52
53
54
# File 'lib/pact_broker/db/clean.rb', line 50

def latest_tagged_pact_publications_ids_to_keep
  @latest_tagged_pact_publications_ids_to_keep ||= resolve_ids(keep.select(&:tag).select(&:latest).collect do | selector |
    PactBroker::Pacts::PactPublication.select(:id).latest_by_consumer_tag_for_clean_selector(selector)
  end.reduce(&:union) || [])
end

#pact_publication_ids_to_deleteObject



61
62
63
# File 'lib/pact_broker/db/clean.rb', line 61

def pact_publication_ids_to_delete
  @pact_publication_ids_to_delete ||= resolve_ids(db[:pact_publications].select(:id).where(id: pact_publication_ids_to_keep).invert)
end

#pact_publication_ids_to_keepObject



40
41
42
43
44
# File 'lib/pact_broker/db/clean.rb', line 40

def pact_publication_ids_to_keep
  @pact_publication_ids_to_keep ||= pact_publication_ids_to_keep_for_version_ids_to_keep
                                      .union(latest_pact_publication_ids_to_keep)
                                      .union(latest_tagged_pact_publications_ids_to_keep)
end

#pact_publication_ids_to_keep_for_version_ids_to_keepObject



46
47
48
# File 'lib/pact_broker/db/clean.rb', line 46

def pact_publication_ids_to_keep_for_version_ids_to_keep
  @pact_publication_ids_to_keep_for_version_ids_to_keep ||= resolve_ids(db[:pact_publications].select(:id).where(consumer_version_id: version_ids_to_keep))
end

#resolve_ids(query, column_name = :id) ⇒ Object



35
36
37
38
# File 'lib/pact_broker/db/clean.rb', line 35

def resolve_ids(query, column_name = :id)
  # query
  Unionable.new(query.collect { |h| h[column_name] })
end

#verification_ids_to_deleteObject



97
98
99
# File 'lib/pact_broker/db/clean.rb', line 97

def verification_ids_to_delete
  @verification_ids_to_delete ||= db[:verifications].select(:id).where(id: verification_ids_to_keep).invert
end

#verification_ids_to_keepObject



93
94
95
# File 'lib/pact_broker/db/clean.rb', line 93

def verification_ids_to_keep
  @verification_ids_to_keep ||= verification_ids_to_keep_for_version_ids_to_keep.union(verification_ids_to_keep_because_latest_verification_for_latest_pact)
end

#verification_ids_to_keep_because_latest_verification_for_latest_pactObject



70
71
72
73
74
75
76
77
78
79
# File 'lib/pact_broker/db/clean.rb', line 70

def verification_ids_to_keep_because_latest_verification_for_latest_pact
  @verification_ids_to_keep_because_latest_verification ||= resolve_ids(
    db[:latest_verification_ids_for_pact_versions]
      .select(:latest_verification_id)
      .where(pact_version_id:
        db[:latest_pact_publications].select(:pact_version_id)
      ),
    :latest_verification_id
  )
end

#verification_ids_to_keep_for_pact_publication_ids_to_keepObject



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/pact_broker/db/clean.rb', line 81

def verification_ids_to_keep_for_pact_publication_ids_to_keep
  @verification_ids_to_keep_for_pact_publication_ids_to_keep ||= resolve_ids(
    db[:latest_verification_id_for_pact_version_and_provider_version]
      .select(:verification_id)
      .where(pact_version_id:
        db[:pact_publications]
          .select(:pact_version_id)
          .where(id: pact_publication_ids_to_keep_for_version_ids_to_keep)
    )
  )
end

#verification_ids_to_keep_for_version_ids_to_keepObject

because they belong to the versions to keep



66
67
68
# File 'lib/pact_broker/db/clean.rb', line 66

def verification_ids_to_keep_for_version_ids_to_keep
  @verification_ids_to_keep_for_version_ids_to_keep ||= resolve_ids(db[:verifications].select(:id).where(provider_version_id: version_ids_to_keep))
end

#version_ids_to_keepObject



101
102
103
104
105
# File 'lib/pact_broker/db/clean.rb', line 101

def version_ids_to_keep
  @version_ids_to_keep ||= keep.collect do | selector |
    PactBroker::Domain::Version.select(:id).for_selector(selector)
  end.reduce(&:union)
end