From bc4574922bbe4d8396cabf3d8d7cc528bcb37e28 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Sun, 1 Mar 2020 17:54:46 +1100 Subject: [PATCH] feat(clean): allow a maximum age to be specified for the versions to keep --- lib/pact_broker/domain/version.rb | 32 ++++++++--- lib/pact_broker/matrix/unresolved_selector.rb | 8 +++ lib/pact_broker/test/test_data_builder.rb | 18 +++++- spec/lib/pact_broker/domain/version_spec.rb | 55 +++++++++++++++++-- 4 files changed, 97 insertions(+), 16 deletions(-) diff --git a/lib/pact_broker/domain/version.rb b/lib/pact_broker/domain/version.rb index 2b8be39bb..6a5c3fd72 100644 --- a/lib/pact_broker/domain/version.rb +++ b/lib/pact_broker/domain/version.rb @@ -39,6 +39,11 @@ def where_number(number) where(name_like(:number, number)) end + def where_age_less_than(days) + start_date = Date.today - days + where{ versions[:created_at] >= start_date } + end + def delete PactBroker::Domain::Tag.where(version: self).delete super @@ -49,20 +54,29 @@ def for_selector(selector) 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 - if selector.latest - join = { - Sequel[:versions][:pacticipant_id] => Sequel[:latest][:pacticipant_id], - Sequel[:versions][:order] => Sequel[:latest][:latest_version_order] - } + query = query.where_age_less_than(selector.max_age) if selector.max_age - max_order_for_each_pacticipant = query.select_group(:pacticipant_id) - .select_append{ max(order).as(latest_version_order) } - - join(max_order_for_each_pacticipant, join, { table_alias: :latest }) + if selector.latest + select_max_order_for_each_pacticipant_and_join_back_to_versions(query) else query end end + + # private + + def select_max_order_for_each_pacticipant_and_join_back_to_versions(query) + join = { + Sequel[:versions][:pacticipant_id] => Sequel[:latest][:pacticipant_id], + Sequel[:versions][:order] => Sequel[:latest][:latest_version_order] + } + + max_order_for_each_pacticipant = query + .select_group(:pacticipant_id) + .select_append{ max(order).as(latest_version_order) } + + join(max_order_for_each_pacticipant, join, table_alias: :latest) + end end def after_create diff --git a/lib/pact_broker/matrix/unresolved_selector.rb b/lib/pact_broker/matrix/unresolved_selector.rb index fea2dad0d..03fb7ea3a 100644 --- a/lib/pact_broker/matrix/unresolved_selector.rb +++ b/lib/pact_broker/matrix/unresolved_selector.rb @@ -40,6 +40,14 @@ def pacticipant_name= pacticipant_name def pacticipant_version_number= pacticipant_version_number self[:pacticipant_version_number] = pacticipant_version_number end + + def max_age= max_age + self[:max_age] = max_age + end + + def max_age + self[:max_age] + end end end end diff --git a/lib/pact_broker/test/test_data_builder.rb b/lib/pact_broker/test/test_data_builder.rb index 7226edc1c..4dfbcf28a 100644 --- a/lib/pact_broker/test/test_data_builder.rb +++ b/lib/pact_broker/test/test_data_builder.rb @@ -150,6 +150,7 @@ def create_consumer_version version_number = "1.0.#{model_counter}", params = {} params.delete(:comment) tag_names = [params.delete(:tag_names), params.delete(:tag_name)].flatten.compact @consumer_version = PactBroker::Domain::Version.create(:number => version_number, :pacticipant => @consumer) + set_created_at_if_set params[:created_at], :versions, { id: @consumer_version.id } tag_names.each do | tag_name | PactBroker::Domain::Tag.create(name: tag_name, version: @consumer_version) end @@ -354,7 +355,7 @@ def and_return instance_variable_name end def set_now date - @now = date + @now = date.to_date self end @@ -363,6 +364,21 @@ def add_day self end + def add_days(days = 1) + @now = @now + days + self + end + + def subtract_day + @now = @now - 1 + self + end + + def subtract_days(days = 1) + @now = @now - days + self + end + def add_minute @now = @now + (1.0/(24*60)) self diff --git a/spec/lib/pact_broker/domain/version_spec.rb b/spec/lib/pact_broker/domain/version_spec.rb index 8fb30a536..98d7088e1 100644 --- a/spec/lib/pact_broker/domain/version_spec.rb +++ b/spec/lib/pact_broker/domain/version_spec.rb @@ -7,6 +7,14 @@ module Domain describe "for_selector" do subject { Version.for_selector(selector).all } + def find_version(number) + subject.find{ |v| v.number == number } + end + + def version_numbers + subject.collect(&:number) + end + context "when selecting the latest prod versions without a pacticipant name" do before do td.create_consumer("Foo") @@ -20,13 +28,11 @@ module Domain let(:selector) { PactBroker::Matrix::UnresolvedSelector.new(tag: 'prod', latest: true) } it "returns the latest prod version for each pacticipant" do - expect(subject.size).to eq 2 - expect(subject.find{ |v| v.number == '2'}).to_not be nil - expect(subject.find{ |v| v.number == '11'}).to_not be nil + expect(version_numbers).to eq %w{2 11} end end - context "when selecting the latest prod versions without a pacticipant name" do + context "when selecting the latest prod versions with a pacticipant name" do before do td.create_consumer("Foo") .create_consumer_version("1", tag_names: %w{prod}) @@ -40,8 +46,45 @@ module Domain it "returns the latest prod version for Foo" do - expect(subject.size).to eq 1 - expect(subject.find{ |v| v.number == '2'}).to_not be nil + expect(version_numbers).to eq %w{2} + end + end + + context "when selecting all prod versions without a pacticipant name" do + before do + td.create_consumer("Foo") + .create_consumer_version("1", tag_names: %w{prod}) + .create_consumer_version("2", tag_names: %w{prod}) + .create_consumer("Bar") + .create_consumer_version("10", tag_names: %w{prod}) + .create_consumer_version("11", tag_names: %w{prod master}) + .create_consumer_version("12", tag_names: %w{master}) + end + + let(:selector) { PactBroker::Matrix::UnresolvedSelector.new(tag: 'prod') } + + it "selects all the production versions without a pacticipant name" do + expect(version_numbers).to eq %w{1 2 10 11} + end + end + + context "when selecting all versions for a tag with max age" do + before do + td.set_now(four_days_ago) + .create_consumer("Foo") + .create_consumer_version("1", tag_names: %w{master}) + .add_days(1) + .create_consumer_version("2", tag_names: %w{master}) + .create_consumer_version("3", tag_names: %w{master}) + end + + let(:selector) { PactBroker::Matrix::UnresolvedSelector.new(tag: 'master', max_age: max_age) } + + let(:max_age) { 3 } + let(:four_days_ago) { Date.today - 4 } + + it "selects the consumer versions younger than the max age" do + expect(version_numbers.sort).to eq %w{2 3} end end end