Skip to content

Commit

Permalink
feat(clean): allow a maximum age to be specified for the versions to …
Browse files Browse the repository at this point in the history
…keep
  • Loading branch information
bethesque committed Mar 1, 2020
1 parent 22f12f1 commit bc45749
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 16 deletions.
32 changes: 23 additions & 9 deletions lib/pact_broker/domain/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 8 additions & 0 deletions lib/pact_broker/matrix/unresolved_selector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 17 additions & 1 deletion lib/pact_broker/test/test_data_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -354,7 +355,7 @@ def and_return instance_variable_name
end

def set_now date
@now = date
@now = date.to_date
self
end

Expand All @@ -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
Expand Down
55 changes: 49 additions & 6 deletions spec/lib/pact_broker/domain/version_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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})
Expand All @@ -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
Expand Down

0 comments on commit bc45749

Please sign in to comment.