diff --git a/lib/pact_broker/db/clean.rb b/lib/pact_broker/db/clean.rb index 71f8c5c7c..e67c29759 100644 --- a/lib/pact_broker/db/clean.rb +++ b/lib/pact_broker/db/clean.rb @@ -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 diff --git a/lib/pact_broker/domain/version.rb b/lib/pact_broker/domain/version.rb index 77d06bf3e..0a238777a 100644 --- a/lib/pact_broker/domain/version.rb +++ b/lib/pact_broker/domain/version.rb @@ -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 diff --git a/lib/pact_broker/versions/repository.rb b/lib/pact_broker/versions/repository.rb index 02237e309..f7a15a4d2 100644 --- a/lib/pact_broker/versions/repository.rb +++ b/lib/pact_broker/versions/repository.rb @@ -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 diff --git a/spec/lib/pact_broker/db/clean_spec.rb b/spec/lib/pact_broker/db/clean_spec.rb index 26e67052d..40634370e 100644 --- a/spec/lib/pact_broker/db/clean_spec.rb +++ b/spec/lib/pact_broker/db/clean_spec.rb @@ -1,4 +1,5 @@ require 'pact_broker/db/clean' +require 'pact_broker/matrix/unresolved_selector' IS_MYSQL = !!DB.mysql? @@ -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