Skip to content

Commit

Permalink
feat(clean): support specifying which versions to keep using selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Feb 29, 2020
1 parent 02ba3b7 commit a06899e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 103 deletions.
15 changes: 14 additions & 1 deletion lib/pact_broker/db/clean.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,25 @@ def call
deleted_counts = {}
kept_counts = {}
date = options[:date]

version_ids_to_keep = nil

if options[:keep]
version_ids_to_keep = options[:keep].collect do | selector |
PactBroker::Domain::Version.select(:id).for_selector(selector)
end.reduce(&:union)
end

pact_publication_ids_to_delete = if date
db[:pact_publications].select(:id).where(Sequel.lit('created_at < ?', date))
elsif version_ids_to_keep
db[:pact_publications].select(:id).where(consumer_version_id: version_ids_to_keep).invert
else
db[:pact_publications].select(:id).where(id: db[:head_matrix].select(:pact_publication_id)).invert
keep = db[:latest_tagged_pact_publications].select(:id).union(db[:latest_pact_publications].select(:id))
db[:pact_publications].select(:id).where(id: keep).invert
end


deleted_counts[:pact_publications] = pact_publication_ids_to_delete.count
kept_counts[:pact_publications] = db[:pact_publications].where(id: pact_publication_ids_to_delete).invert.count

Expand Down
6 changes: 3 additions & 3 deletions lib/pact_broker/domain/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ def delete
super
end

def find_for_selector(selector)
query = select_all_qualified
def for_selector(selector)
query = self
query = query.where_pacticipant_name(selector.pacticipant_name) if selector.pacticipant_name
query = query.where_tag(selector.tag) if selector.tag
query = query.where_number(selector.pacticipant_version_number) if selector.pacticipant_version_number
query = query.reverse_order(:order).limit(1) if selector.latest
versions = query.all
query
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/versions/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def delete_orphan_versions consumer, provider
end

def find_versions_for_selector(selector)
PactBroker::Domain::Version.find_for_selector(selector)
PactBroker::Domain::Version.select_all_qualified.for_selector(selector).all
end
end
end
Expand Down
128 changes: 30 additions & 98 deletions spec/lib/pact_broker/db/clean_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'pact_broker/db/clean'
require 'pact_broker/matrix/unresolved_selector'

IS_MYSQL = !!DB.mysql?

Expand All @@ -12,111 +13,42 @@ module DB
subject { Clean.call(PactBroker::DB.connection, options) }

describe ".call"do

before do
td.create_pact_with_hierarchy("Foo", "0", "Bar")
.create_consumer_version_tag("prod")
.create_consumer_version("1")
.create_pact
.create_consumer_version_tag("prod")
.comment("keep")
.create_verification(provider_version: "20")
.create_consumer_version("2")
.create_pact
.comment("don't keep")
.create_webhook
.create_triggered_webhook
.create_webhook_execution
.create_verification(provider_version: "30")
.create_verification_webhook
.create_triggered_webhook
.create_webhook_execution
.create_consumer_version("3")
.create_pact
.comment("keep")
.create_verification(provider_version: "40")
.create_verification(provider_version: "40", number: 2)
.create_verification(provider_version: "50", number: 3)
end


it "does not delete any rows in the head matrix" do
head_matrix_before = db[:head_matrix].select_all
subject
head_matrix_after = db[:head_matrix].select_all
expect(head_matrix_before).to eq head_matrix_after
end

it "deletes rows that aren't the latest or latest tagged" do
subject
expect(db[:matrix].where(consumer_version_number: "2").count).to eq 0
end

it "deletes orphan pact_versions" do
subject
expect(db[:pact_versions].count).to eq 2
end

it "deletes orphan versions" do
subject
expect(db[:versions].where(number: "20").count).to be 1
expect(db[:versions].where(number: "30").count).to be 0
expect(db[:versions].where(number: "40").count).to be 1
expect(db[:versions].where(number: "50").count).to be 1
end

it "deletes overwritten verifications" do
expect(db[:matrix].where(provider_version_number: "40").count).to eq 2
subject
expect(db[:matrix].where(provider_version_number: "40", verification_number: 2).count).to eq 1
end
end

describe ".call with a date" do
before do
td.set_now(DateTime.new(2019, 1, 1))
.create_pact_with_hierarchy
.create_verification
end
let(:options) { { date: date } }

context "when the data is older than the given date" do
let(:date) { DateTime.new(2019, 1, 2) }
let(:expected_report) do
{
kept: { pact_publications: 0, verification_results: 0 },
deleted: { pact_publications: 1, verification_results: 1 }
}
context "when there are specified versions to keep" do
before do
td.create_pact_with_hierarchy("Foo", "1", "Bar")
.create_consumer_version_tag("prod")
.create_consumer_version_tag("master")
.create_consumer_version("3", tag_names: %w{prod})
.create_pact
.create_consumer_version("4", tag_names: %w{master})
.create_pact
.create_consumer_version("5", tag_names: %w{master})
.create_pact
.create_consumer_version("6", tag_names: %w{foo})
.create_pact
end

it "is deleted" do
subject
expect(db[:head_matrix].count).to be 0
end

it "returns a report" do
expect(subject).to include(expected_report)
end
end

context "when the data is not older than the given date" do
let(:date) { DateTime.new(2019, 1, 1) }
let(:expected_report) do
let(:options) do
{
kept: { pact_publications: 1, verification_results: 1 },
deleted: { pact_publications: 0, verification_results: 0 }
keep: [
PactBroker::Matrix::UnresolvedSelector.new(tag: "prod"),
PactBroker::Matrix::UnresolvedSelector.new(tag: "master", latest: true)
]
}
end

it "is not deleted" do
it "does not delete the consumer versions specified" do
expect(PactBroker::Domain::Version.where(number: "1").count).to be 1
expect(PactBroker::Domain::Version.where(number: "3").count).to be 1
expect(PactBroker::Domain::Version.where(number: "4").count).to be 1
expect(PactBroker::Domain::Version.where(number: "5").count).to be 1
expect(PactBroker::Domain::Version.where(number: "6").count).to be 1
subject
expect(db[:head_matrix].count).to be 1
expect(db[:pact_publications].count).to be 1
expect(db[:verifications].count).to be 1
end

it "returns a report" do
expect(subject).to include(expected_report)
expect(PactBroker::Domain::Version.where(number: "1").count).to be 1
expect(PactBroker::Domain::Version.where(number: "3").count).to be 1
expect(PactBroker::Domain::Version.where(number: "4").count).to be 0
expect(PactBroker::Domain::Version.where(number: "5").count).to be 1
expect(PactBroker::Domain::Version.where(number: "6").count).to be 0
end
end
end
Expand Down

0 comments on commit a06899e

Please sign in to comment.