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 +4 -4
- data/.github/workflows/test.yml +13 -1
- data/CHANGELOG.md +7 -0
- data/lib/pact_broker/db/clean_incremental.rb +42 -13
- data/lib/pact_broker/tasks/clean_task.rb +11 -3
- data/lib/pact_broker/version.rb +1 -1
- data/spec/lib/pact_broker/db/clean_incremental_spec.rb +20 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a7fe157577be6cdfb603e4607b174224781db0b9b2ee7ccb1325cdda50ce129d
|
4
|
+
data.tar.gz: 9e28ef38f281ac6d6dbf1879d3794a8f915cb78bb8a82c464c7273ac9f8522dc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3e5d4d249f691a3e03725e3b827f4044eb2fde360cd0dd9128fce61a4442288083f825f654473cf1a3220745460675036951242cc801211eda258369e38eeb09
|
7
|
+
data.tar.gz: f8b9f3bdbfe42ca21291b41588ae4b3be15ca53efaec275e5356d8363dfa181e592942cb5e13bbd492fadb61d231cee68aaed009fef559c0645bc4debc4137c1
|
data/.github/workflows/test.yml
CHANGED
@@ -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.
|
51
|
+
ruby_version: ["2.7"]
|
40
52
|
steps:
|
41
53
|
- uses: actions/checkout@v2
|
42
54
|
- uses: actions/setup-ruby@v1
|
data/CHANGELOG.md
CHANGED
@@ -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/
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
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
|
-
|
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(
|
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
|
-
|
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
|
data/lib/pact_broker/version.rb
CHANGED
@@ -15,15 +15,21 @@ module PactBroker
|
|
15
15
|
let(:options) { {} }
|
16
16
|
let(:db) { PactBroker::DB.connection }
|
17
17
|
|
18
|
-
|
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
|