pact_broker 2.70.0 → 2.71.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b57a2f04694ce119675cd5b4fb647e1c2faff71b890958391ca30846ef0d7fab
4
- data.tar.gz: 8b6484329b0821600417fc17ad564a14de06edf2edc21939423c34ea2795cee1
3
+ metadata.gz: a7fe157577be6cdfb603e4607b174224781db0b9b2ee7ccb1325cdda50ce129d
4
+ data.tar.gz: 9e28ef38f281ac6d6dbf1879d3794a8f915cb78bb8a82c464c7273ac9f8522dc
5
5
  SHA512:
6
- metadata.gz: e4d1c82adbbb041f38d64200ccb0f1fc8d4672f3a9e445db0dcf428b25c8353c5922a3fb579b74560114df476886fb8aee93d2b793643ae9bbbbd8f786fec443
7
- data.tar.gz: dc130a9cea4a62c741c401290b385f807c4597073a1ae467d3b334bd058267eca2179ebb6be60d53c2f6cf7046c7b567edf473738a2642e122d1c6e40938301a
6
+ metadata.gz: 3e5d4d249f691a3e03725e3b827f4044eb2fde360cd0dd9128fce61a4442288083f825f654473cf1a3220745460675036951242cc801211eda258369e38eeb09
7
+ data.tar.gz: f8b9f3bdbfe42ca21291b41588ae4b3be15ca53efaec275e5356d8363dfa181e592942cb5e13bbd492fadb61d231cee68aaed009fef559c0645bc4debc4137c1
@@ -3,6 +3,18 @@ name: Test
3
3
  on: push
4
4
 
5
5
  jobs:
6
+ sqlite:
7
+ runs-on: "ubuntu-latest"
8
+ strategy:
9
+ matrix:
10
+ ruby_version: ["2.7"]
11
+ steps:
12
+ - uses: actions/checkout@v2
13
+ - uses: actions/setup-ruby@v1
14
+ with:
15
+ ruby-version: ${{ matrix.ruby_version }}
16
+ - run: "bundle install"
17
+ - run: "bundle exec rake"
6
18
  postgres:
7
19
  runs-on: "ubuntu-latest"
8
20
  strategy:
@@ -36,7 +48,7 @@ jobs:
36
48
  runs-on: "ubuntu-latest"
37
49
  strategy:
38
50
  matrix:
39
- ruby_version: ["2.5", "2.7"]
51
+ ruby_version: ["2.7"]
40
52
  steps:
41
53
  - uses: actions/checkout@v2
42
54
  - uses: actions/setup-ruby@v1
@@ -1,3 +1,10 @@
1
+ <a name="v2.71.0"></a>
2
+ ### v2.71.0 (2020-11-28)
3
+
4
+ #### Features
5
+
6
+ * allow clean to be performed in dry run mode ([a7a18fde](/../../commit/a7a18fde))
7
+
1
8
  <a name="v2.70.0"></a>
2
9
  ### v2.70.0 (2020-11-28)
3
10
 
@@ -1,11 +1,11 @@
1
1
  require 'pact_broker/logging'
2
2
  require 'pact_broker/matrix/unresolved_selector'
3
+ require 'pact_broker/date_helper'
4
+
3
5
 
4
6
  module PactBroker
5
7
  module DB
6
8
  class CleanIncremental
7
- include PactBroker::Logging
8
-
9
9
  DEFAULT_KEEP_SELECTORS = [
10
10
  PactBroker::Matrix::UnresolvedSelector.new(tag: true, latest: true),
11
11
  PactBroker::Matrix::UnresolvedSelector.new(latest: true),
@@ -22,6 +22,10 @@ module PactBroker
22
22
  @options = options
23
23
  end
24
24
 
25
+ def logger
26
+ options[:logger] || PactBroker.logger
27
+ end
28
+
25
29
  def keep
26
30
  options[:keep] || DEFAULT_KEEP_SELECTORS
27
31
  end
@@ -45,16 +49,38 @@ module PactBroker
45
49
  end
46
50
 
47
51
  def call
48
- require 'pact_broker/domain/version'
49
- before_counts = current_counts
50
-
51
- result = PactBroker::Domain::Version.where(id: resolve_ids(version_ids_to_delete)).delete
52
- delete_orphan_pact_versions
53
-
54
- after_counts = current_counts
55
-
56
- TABLES.each_with_object({}) do | table_name, comparison_counts |
57
- comparison_counts[table_name.to_s] = { "deleted" => before_counts[table_name] - after_counts[table_name], "kept" => after_counts[table_name] }
52
+ require 'pact_broker/db/models'
53
+
54
+ if dry_run?
55
+ to_delete = PactBroker::Domain::Version
56
+ .where(id: version_ids_to_delete.select(:id))
57
+ .all
58
+ .group_by{ | v | v.pacticipant_id }
59
+ .each_with_object({}) do | (pacticipant_id, versions), thing |
60
+ thing[versions.first.pacticipant.name] = {
61
+ count: versions.count,
62
+ from_version: {
63
+ number: versions.first.number,
64
+ created: DateHelper.distance_of_time_in_words(versions.first.created_at, DateTime.now) + " ago",
65
+ tags: versions.first.tags.collect(&:name)
66
+ },
67
+ to_version: {
68
+ number: versions.last.number,
69
+ created: DateHelper.distance_of_time_in_words(versions.last.created_at, DateTime.now) + " ago",
70
+ tags: versions.last.tags.collect(&:name)
71
+ }
72
+ }
73
+ end
74
+ { "to_delete" => to_delete }
75
+ else
76
+ before_counts = current_counts
77
+ result = PactBroker::Domain::Version.where(id: resolve_ids(version_ids_to_delete)).delete
78
+ delete_orphan_pact_versions
79
+ after_counts = current_counts
80
+
81
+ TABLES.each_with_object({}) do | table_name, comparison_counts |
82
+ comparison_counts[table_name.to_s] = { "deleted" => before_counts[table_name] - after_counts[table_name], "kept" => after_counts[table_name] }
83
+ end
58
84
  end
59
85
  end
60
86
 
@@ -68,8 +94,11 @@ module PactBroker
68
94
  end
69
95
  end
70
96
 
97
+ def dry_run?
98
+ options[:dry_run]
99
+ end
100
+
71
101
  def delete_orphan_pact_versions
72
- logger.info("Deleting orphan pact versions")
73
102
  referenced_pact_version_ids = db[:pact_publications].select(:pact_version_id).union(db[:verifications].select(:pact_version_id))
74
103
  db[:pact_versions].where(id: referenced_pact_version_ids).invert.delete
75
104
  end
@@ -5,10 +5,13 @@ module PactBroker
5
5
  attr_accessor :database_connection
6
6
  attr_reader :keep_version_selectors
7
7
  attr_accessor :version_deletion_limit
8
+ attr_accessor :logger
9
+ attr_accessor :dry_run
8
10
 
9
11
  def initialize &block
10
12
  require 'pact_broker/db/clean_incremental'
11
13
  @version_deletion_limit = 1000
14
+ @dry_run = false
12
15
  @keep_version_selectors = PactBroker::DB::CleanIncremental::DEFAULT_KEEP_SELECTORS
13
16
  rake_task &block
14
17
  end
@@ -38,14 +41,19 @@ module PactBroker
38
41
  if keep_version_selectors.nil? || keep_version_selectors.empty?
39
42
  raise PactBroker::Error.new("You must specify which versions to keep")
40
43
  else
41
- puts "Deleting oldest #{version_deletion_limit} versions, keeping versions that match the following selectors: #{keep_version_selectors}..."
44
+ output "Deleting oldest #{version_deletion_limit} versions, keeping versions that match the configured selectors", keep_version_selectors
42
45
  end
43
46
 
44
47
  start_time = Time.now
45
- results = PactBroker::DB::CleanIncremental.call(database_connection, keep: keep_version_selectors, limit: version_deletion_limit)
48
+ results = PactBroker::DB::CleanIncremental.call(
49
+ database_connection, keep: keep_version_selectors, limit: version_deletion_limit, logger: logger, dry_run: dry_run)
46
50
  end_time = Time.now
47
51
  elapsed_seconds = (end_time - start_time).to_i
48
- puts results.to_yaml.gsub("---", "\nResults (#{elapsed_seconds} seconds)\n-------")
52
+ output "Results (#{elapsed_seconds} seconds)", results
53
+ end
54
+
55
+ def output string, payload = {}
56
+ logger ? logger.info(string, payload: payload) : puts("#{string} #{payload.to_json}")
49
57
  end
50
58
  end
51
59
  end
@@ -1,3 +1,3 @@
1
1
  module PactBroker
2
- VERSION = '2.70.0'
2
+ VERSION = '2.71.0'
3
3
  end
@@ -15,15 +15,21 @@ module PactBroker
15
15
  let(:options) { {} }
16
16
  let(:db) { PactBroker::DB.connection }
17
17
 
18
- subject { CleanIncremental.call(PactBroker::DB.connection, options) }
18
+
19
19
  let(:latest_dev_selector) { PactBroker::Matrix::UnresolvedSelector.new(tag: "dev", latest: true) }
20
20
  let(:all_prod_selector) { PactBroker::Matrix::UnresolvedSelector.new(tag: "prod") }
21
21
  let(:limit) { 3 }
22
+ let(:dry_run) { false }
23
+
24
+ subject { CleanIncremental.call(PactBroker::DB.connection, options) }
22
25
 
23
26
  describe ".call"do
24
27
  context "when there are specified versions to keep" do
25
28
  before do
26
29
  td.create_pact_with_hierarchy("Foo", "1", "Bar")
30
+ .create_webhook
31
+ .create_triggered_webhook
32
+ .create_webhook_execution
27
33
  .create_consumer_version_tag("prod").comment("keep as one of prod")
28
34
  .create_consumer_version_tag("dev")
29
35
  .add_day
@@ -45,7 +51,7 @@ module PactBroker
45
51
  .create_pact
46
52
  end
47
53
 
48
- let(:options) { { keep: [all_prod_selector, latest_dev_selector], limit: limit } }
54
+ let(:options) { { keep: [all_prod_selector, latest_dev_selector], limit: limit, dry_run: dry_run } }
49
55
 
50
56
  it "does not delete the consumer versions specified" do
51
57
  expect(PactBroker::Domain::Version.where(number: "1").count).to be 1
@@ -64,8 +70,20 @@ module PactBroker
64
70
  expect(PactBroker::Domain::Version.where(number: "6").count).to be 0
65
71
  expect(PactBroker::Domain::Version.where(number: "7").count).to be 1
66
72
  end
73
+
74
+ context "when dry_run: true" do
75
+ before do
76
+ td.create_pact_with_hierarchy("Meep", "2", "Moop")
77
+ end
78
+ let(:dry_run) { true }
79
+
80
+ it "doesn't delete anything" do
81
+ expect { subject }.to_not change { PactBroker::Domain::Version.count }
82
+ end
83
+ end
67
84
  end
68
85
 
86
+
69
87
  context "with orphan pact versions" do
70
88
  before do
71
89
  # Create a pact that will not be deleted
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pact_broker
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.70.0
4
+ version: 2.71.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bethany Skurrie