Skip to content

Commit

Permalink
fix: lazy load latest verification using select max, and eager load u…
Browse files Browse the repository at this point in the history
…sing the skynet query
  • Loading branch information
bethesque committed Aug 14, 2021
1 parent f6463ec commit e6ee6ab
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 14 deletions.
10 changes: 4 additions & 6 deletions lib/pact_broker/index/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def self.find_all_index_items
find_index_items(page_number: 1, page_size: 100000000000)
end

# rubocop: disable Metrics/CyclomaticComplexity, Metrics/MethodLength
# rubocop: disable Metrics/CyclomaticComplexity
def self.find_index_items options = {}
latest_verifications_for_cv_tags = latest_verifications_for_consumer_version_tags(options)
latest_pp_ids = latest_pact_publication_ids
Expand All @@ -44,10 +44,9 @@ def self.find_index_items options = {}
pact_publications = pact_publication_query
.eager(:consumer)
.eager(:provider)
.eager(:pact_version)
.eager(pact_version: { latest_verification: { provider_version: [{ current_deployed_versions: :environment }, { current_supported_released_versions: :environment }, :latest_version_for_branch, { tags: :head_tag } ] } })
.eager(integration: [{latest_verification: :provider_version}, :latest_triggered_webhooks])
.eager(consumer_version: [{ current_deployed_versions: :environment }, { current_supported_released_versions: :environment }, :latest_version_for_branch, { tags: :head_tag }])
.eager(latest_verification: { provider_version: [{ current_deployed_versions: :environment }, { current_supported_released_versions: :environment }, :latest_version_for_branch, { tags: :head_tag } ] })
.eager(:head_pact_publications_for_tags)

index_items = pact_publications.all.collect do | pact_publication |
Expand All @@ -72,7 +71,7 @@ def self.find_index_items options = {}

Page.new(index_items, pagination_record_count)
end
# rubocop: enable Metrics/CyclomaticComplexity, Metrics/MethodLength
# rubocop: enable Metrics/CyclomaticComplexity

# Worst. Code. Ever.
#
Expand Down Expand Up @@ -107,9 +106,8 @@ def self.find_index_items_for_api(consumer_name: nil, provider_name: nil, page_n
pact_publications = head_pact_publications(consumer_name: consumer_name, provider_name: provider_name, tags: true, page_number: page_number, page_size: page_size)
.eager(:consumer)
.eager(:provider)
.eager(:pact_version)
.eager(pact_version: { latest_verification: { provider_version: [{ current_deployed_versions: :environment }, { current_supported_released_versions: :environment }, :latest_version_for_branch, { tags: :head_tag }]} })
.eager(consumer_version: [{ current_deployed_versions: :environment }, { current_supported_released_versions: :environment }, :latest_version_for_branch, { tags: :head_tag }])
.eager(latest_verification: { provider_version: [{ current_deployed_versions: :environment }, { current_supported_released_versions: :environment }, :latest_version_for_branch, { tags: :head_tag }]})
.eager(:head_pact_publications_for_tags)

pact_publications.all.collect do | pact_publication |
Expand Down
4 changes: 0 additions & 4 deletions lib/pact_broker/pacts/pact_publication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ class PactPublication < Sequel::Model(:pact_publications)
associate(:many_to_one, :pact_version, class: "PactBroker::Pacts::PactVersion", :key => :pact_version_id, :primary_key => :id)
associate(:many_to_one, :integration, class: "PactBroker::Integrations::Integration", key: [:consumer_id, :provider_id], primary_key: [:consumer_id, :provider_id])

one_to_one(:latest_verification, class: "PactBroker::Domain::Verification", key: :pact_version_id, primary_key: :pact_version_id) do | ds |
ds.unlimited.latest_by_pact_version
end

# TODO rename to consumer_version_tags
associate(:one_to_many, :tags, :class => "PactBroker::Domain::Tag", :key => :version_id, :primary_key => :consumer_version_id)

Expand Down
10 changes: 7 additions & 3 deletions lib/pact_broker/pacts/pact_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ class PactVersion < Sequel::Model(:pact_versions)
associate(:many_to_one, :consumer, class: "PactBroker::Domain::Pacticipant", key: :consumer_id, primary_key: :id)
associate(:many_to_many, :consumer_versions, class: "PactBroker::Domain::Version", join_table: :pact_publications, left_key: :pact_version_id, right_key: :consumer_version_id, order: :order)

one_to_one(:latest_verification, class: "PactBroker::Domain::Verification", key: :pact_version_id, primary_key: :id) do | ds |
ds.unlimited.latest_by_pact_version
end
one_to_one(:latest_verification,
class: "PactBroker::Domain::Verification",
read_only: true,
dataset: lambda { PactBroker::Domain::Verification.where(id: PactBroker::Domain::Verification.select(Sequel.function(:max, :id)).where(pact_version_id: id)) },
key: :pact_version_id, primary_key: :id,
eager_block: lambda { | ds | ds.latest_by_pact_version }
)

# do not eager load this - it won't work because of the limit(1)
one_through_one(:latest_consumer_version, class: "PactBroker::Domain::Version", join_table: :pact_publications, left_key: :pact_version_id, right_key: :consumer_version_id) do | ds |
Expand Down
27 changes: 26 additions & 1 deletion spec/lib/pact_broker/pacts/pact_version_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
module PactBroker
module Pacts
describe PactVersion do

describe "pacticipant names" do
subject(:pact_version) do
td.create_consumer("consumer")
Expand Down Expand Up @@ -36,6 +35,32 @@ module Pacts
end
end

describe "#latest_verification" do
before do
td.create_pact_with_verification("Foo", "1", "Bar", "2")
.create_verification(provider_version: "3", number: 2)
.create_pact_with_verification("NotFoo", "1", "Bar", "4")
.create_verification(provider_version: "5", number: 3)
end

context "lazy loading" do
it "lazy loads" do
expect(PactPublication.first.pact_version.latest_verification.provider_version_number).to eq "3"
expect(PactPublication.last.pact_version.latest_verification.provider_version_number).to eq "5"
end
end

context "eager loading" do
let(:pact_version_1) { PactVersion.eager(:latest_verification).all.first }
let(:pact_version_2) { PactVersion.eager(:latest_verification).all.last }

it "eager loads" do
expect(pact_version_1.associations[:latest_verification].provider_version_number).to eq "3"
expect(pact_version_2.associations[:latest_verification].provider_version_number).to eq "5"
end
end
end

describe "latest_consumer_version" do
before do
td.create_consumer("consumer")
Expand Down

0 comments on commit e6ee6ab

Please sign in to comment.