From 02eeb42485f93d5d6ed8796c6460b339585b52a6 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Sun, 15 Aug 2021 13:02:16 +1000 Subject: [PATCH] feat: add index to verifications table to improve performance of 'latest verification' query --- ...10815_add_provider_version_id_index_to_verifications.rb | 7 +++++++ lib/pact_broker/pacts/pact_version.rb | 3 ++- 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 db/migrations/20210815_add_provider_version_id_index_to_verifications.rb 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 } )