Class: PactBroker::DB::DeleteOverwrittenData

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of DeleteOverwrittenData.



11
12
13
14
15
16
# File 'lib/pact_broker/db/delete_overwritten_data.rb', line 11

def initialize database_connection, options = {}
  @db = database_connection
  @options = options
  @cut_off_date = options[:max_age] ? (DateTime.now - options[:max_age]) : DateTime.now
  @limit = options[:limit] || 1000
end

Class Method Details

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



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

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

Instance Method Details

#callObject



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
# File 'lib/pact_broker/db/delete_overwritten_data.rb', line 18

def call
  require "pact_broker/pacts/pact_publication"
  require "pact_broker/domain/verification"

  deleted_counts = {}
  kept_counts = {}

  deleted_counts.merge!(delete_overwritten_pact_publications)
  deleted_counts.merge!(delete_overwritten_verifications)
  deleted_counts.merge!(delete_orphan_pact_versions)
  deleted_counts.merge!(delete_webhook_data)

  kept_counts[:pact_publications] = db[:pact_publications].count
  kept_counts[:verification_results] = db[:verifications].count
  kept_counts[:pact_versions] = db[:pact_versions].count
  kept_counts[:triggered_webhooks] = db[:triggered_webhooks].count

  if dry_run?
    to_keep = deleted_counts.keys.each_with_object({}) do | table_name, new_counts |
      new_counts[table_name] = kept_counts[table_name] - deleted_counts[table_name]
    end
    deleted_counts.each_with_object({}) do | (key, value), new_hash |
      new_hash[key] = { toDelete: value, toKeep: to_keep[key] }
    end
  else
    deleted_counts.each_with_object({}) do | (key, value), new_hash |
      new_hash[key] = { deleted: value, kept: kept_counts[key] }
    end
  end
end