Skip to content

Commit

Permalink
feat(clean): all the latest version for all tags to be kept
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Mar 9, 2020
1 parent f625c9a commit 9ff7bbe
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 11 deletions.
26 changes: 16 additions & 10 deletions lib/pact_broker/domain/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,15 @@ def where_pacticipant_name(pacticipant_name)
end

def where_tag(tag)
join(:tags) do | tags |
Sequel.&(
{ Sequel[first_source_alias][:id] => Sequel[tags][:version_id] },
name_like(Sequel[tags][:name], tag)
)
if tag == true
join(:tags, Sequel[:tags][:version_id] => Sequel[first_source_alias][:id])
else
join(:tags) do | tags |
Sequel.&(
{ Sequel[first_source_alias][:id] => Sequel[tags][:version_id] },
name_like(Sequel[tags][:name], tag)
)
end
end
end

Expand All @@ -57,25 +61,27 @@ def for_selector(selector)
query = query.where_age_less_than(selector.max_age) if selector.max_age

if selector.latest
select_max_order_for_each_pacticipant_and_join_back_to_versions(query)
calculate_max_version_order_and_join_back_to_versions(query, selector)
else
query
end
end

# private

def select_max_order_for_each_pacticipant_and_join_back_to_versions(query)
join = {
def calculate_max_version_order_and_join_back_to_versions(query, selector)
versions_join = {
Sequel[:versions][:pacticipant_id] => Sequel[:latest][:pacticipant_id],
Sequel[:versions][:order] => Sequel[:latest][:latest_version_order]
}

group_by_cols = selector.tag == true ? [:pacticipant_id, Sequel[:tags][:name]] : [:pacticipant_id]

max_order_for_each_pacticipant = query
.select_group(:pacticipant_id)
.select_group(*group_by_cols)
.select_append{ max(order).as(latest_version_order) }

join(max_order_for_each_pacticipant, join, table_alias: :latest)
join(max_order_for_each_pacticipant, versions_join, table_alias: :latest)
end
end

Expand Down
55 changes: 54 additions & 1 deletion spec/lib/pact_broker/domain/version_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
module PactBroker
module Domain
describe Version do

describe "for_selector" do
subject { Version.for_selector(selector).all }

Expand Down Expand Up @@ -68,6 +67,60 @@ def version_numbers
end
end

context "when selecting the latest version for each tag (head versions)" do
before do
td.create_consumer("Foo")
.create_consumer_version("1", tag_names: %w{prod})
.create_consumer_version("2", tag_names: %w{prod}).comment("yes")
.create_consumer_version("3", tag_names: %w{master})
.create_consumer_version("4", tag_names: %w{master}).comment("yes")
.create_consumer("Bar")
.create_consumer_version("10", tag_names: %w{prod})
.create_consumer_version("11", tag_names: %w{prod}).comment("yes")
end

let(:selector) { PactBroker::Matrix::UnresolvedSelector.new(tag: true, latest: true) }

it "selects the head versions for each tag" do
expect(version_numbers.sort).to eq %w{11 2 4}
end

context "when also specifying pacticipant name" do
let(:selector) { PactBroker::Matrix::UnresolvedSelector.new(tag: true, latest: true, pacticipant_name: "Foo") }

it "selects the head versions for each tag for the given pacticipant" do
expect(version_numbers.sort).to eq %w{2 4}
end
end
end

context "when selecting all tagged versions" 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_version("4", tag_names: %w{master})
.create_consumer_version("5")
.create_consumer("Bar")
.create_consumer_version("10", tag_names: %w{prod})
.create_consumer_version("11")
end

let(:selector) { PactBroker::Matrix::UnresolvedSelector.new(tag: true) }

it "selects every version with a tag" do
expect(version_numbers.sort).to eq %w{1 10 2 4}
end

context "when also specifying pacticipant name" do
let(:selector) { PactBroker::Matrix::UnresolvedSelector.new(tag: true, pacticipant_name: "Foo") }

it "selects every version with a tag for the given pacticipant" do
expect(version_numbers.sort).to eq %w{1 2 4}
end
end
end

context "when selecting all versions for a tag with max age" do
before do
td.set_now(four_days_ago)
Expand Down

0 comments on commit 9ff7bbe

Please sign in to comment.