diff --git a/db/migrations/20210815_add_provider_version_id_index_to_verifications.rb b/db/migrations/20210815_add_provider_version_id_index_to_verifications.rb new file mode 100644 index 000000000..6f9aa05be --- /dev/null +++ b/db/migrations/20210815_add_provider_version_id_index_to_verifications.rb @@ -0,0 +1,7 @@ +Sequel.migration do + change do + alter_table(:verifications) do + add_index([:pact_version_id, :id], name: "verifications_pact_version_id_id_index") + end + end +end diff --git a/lib/pact_broker/pacts/pact_version.rb b/lib/pact_broker/pacts/pact_version.rb index 744463104..913eded22 100644 --- a/lib/pact_broker/pacts/pact_version.rb +++ b/lib/pact_broker/pacts/pact_version.rb @@ -1,6 +1,7 @@ require "sequel" require "pact_broker/repositories/helpers" require "pact_broker/verifications/latest_verification_for_pact_version" +require "pact_broker/verifications/latest_verification_id_for_pact_version_and_provider_version" module PactBroker module Pacts @@ -17,7 +18,7 @@ class PactVersion < Sequel::Model(:pact_versions) 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)) }, + dataset: lambda { PactBroker::Domain::Verification.where(id: PactBroker::Verifications::LatestVerificationIdForPactVersionAndProviderVersion.select(Sequel.function(:max, :verification_id)).where(pact_version_id: id)) }, key: :pact_version_id, primary_key: :id, eager_block: lambda { | ds | ds.latest_by_pact_version } )