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,
lib/pact_broker/db/clean/branch_selector.rb

Defined Under Namespace

Classes: BranchSelector, Selector, Unionable

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

included, #log_error, #log_with_tag, #measure_debug, #measure_info

Constructor Details

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

Returns a new instance of Clean.



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

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

Class Method Details

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



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

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

Instance Method Details

#callObject



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
135
136
137
138
# File 'lib/pact_broker/db/clean.rb', line 108

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

  deleted_counts[:stale_branches] = delete_stale_branches
  kept_counts[:stale_branches] = db[:branches].count

  { kept: kept_counts, deleted: deleted_counts }
end

#keepObject



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

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



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

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



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

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



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

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



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

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



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

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



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

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

#verification_ids_to_deleteObject



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

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



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

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



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

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



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

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



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

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



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

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