From 9ac2ba9b4a200a5e57d112e22e7ba3f221a3e208 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Tue, 28 Aug 2018 08:21:34 +1000 Subject: [PATCH] feat: eager load latest version for /pacticipants resource --- .../20180828_create_latest_versions.rb | 25 ++++++++++++++++++ db/pact_broker_database.sqlite3 | Bin 319488 -> 319488 bytes lib/pact_broker/domain/pacticipant.rb | 5 ++-- lib/pact_broker/pacticipants/repository.rb | 2 +- lib/pact_broker/versions/latest_version.rb | 11 ++++++++ 5 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 db/migrations/20180828_create_latest_versions.rb create mode 100644 lib/pact_broker/versions/latest_version.rb diff --git a/db/migrations/20180828_create_latest_versions.rb b/db/migrations/20180828_create_latest_versions.rb new file mode 100644 index 000000000..4747160d4 --- /dev/null +++ b/db/migrations/20180828_create_latest_versions.rb @@ -0,0 +1,25 @@ +Sequel.migration do + up do + latest_version_orders = from(:versions) + .select_group(:pacticipant_id) + .select_append{ max(order).as(latest_version_order) } + + create_or_replace_view(:latest_version_orders, latest_version_orders) + + join = { + Sequel[:versions][:pacticipant_id] => Sequel[:latest_version_orders][:pacticipant_id], + Sequel[:versions][:order] => Sequel[:latest_version_orders][:latest_version_order] + } + + latest_versions = from(:versions) + .select(Sequel[:versions].*) + .join(:latest_version_orders, join) + + create_or_replace_view(:latest_versions, latest_versions) + end + + down do + drop_view(:latest_version_orders) + drop_view(:latest_versions) + end +end diff --git a/db/pact_broker_database.sqlite3 b/db/pact_broker_database.sqlite3 index 0bf7cb5874207492dce1b90d0f1f83bfdaf89133..20ac2199646619dac191e2f08f1b4dbf9643c20a 100644 GIT binary patch delta 8889 zcmeHM4RBOdmhSuR?SB1}?w7w#=ZBaV1B5_GI$#J8{sIXQ5)wiPKZ0~ONkbZwbdyd3 z>L7IBHHr*_JmClmqN^oSvqJdMp4DLnf0*T;RoC@zMs#YYtlHUHwWge{U0iq1echdO z0Cg<4YHO?Pa3AlUd+xpG-h1vj-#KsV*|e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m2<+ zyz*j16-IShiFkA|bd8K1*qV&fY4t)?4u2lufiYv&_-QTx%Z_os&;?99;q}ROpYrke z;m>eIzkwFTX?}E?YpUveL!#C;$)Q`@#DyQT!0HUo4Nv}-dzB-c6rQBGdwQ(*bdGh= zr*-~_N7F*G$E9f68r&w^d%FA9c18C2yz!c?Y5iW< za*n6L0Ux(oHLTtnrmyqDo)MZ2(;visFwM`CA$lp+vFa*rhc-X=MQhsXp3ZKqKe)0_ z>&GWSPZdvxvwj{kW!!}ORv#W6`Y@=8H{qTRq=>s`Yn39_&K2X9mAyS{L)!9a(bKqmD#vh^mNkI^J>YcLW`kju`u=_9yHs z?S9*rwpVRKwmG&q>-*Lt)_&_!>vZ)^byQuY7UN9*T;ccvugEe1ridgHpitzP0Dh5T v0+eHh1q3J(8Kw)=xHq;={4Em+Ry=0I1k*>KiY=8zZoMY*yZ}~x$RGL_3-Wtq diff --git a/lib/pact_broker/domain/pacticipant.rb b/lib/pact_broker/domain/pacticipant.rb index f228bb844..7e9ca4bbe 100644 --- a/lib/pact_broker/domain/pacticipant.rb +++ b/lib/pact_broker/domain/pacticipant.rb @@ -1,11 +1,11 @@ require 'pact_broker/db' require 'pact_broker/messages' require 'pact_broker/repositories/helpers' +require 'pact_broker/versions/latest_version' +require 'pact_broker/domain/label' module PactBroker - module Domain - class Pacticipant < Sequel::Model include Messages @@ -15,6 +15,7 @@ class Pacticipant < Sequel::Model one_to_many :versions, :order => :order, :reciprocal => :pacticipant one_to_many :labels, :order => :name, :reciprocal => :pacticipant one_to_many :pacts + one_to_one :latest_version, :class => "PactBroker::Versions::LatestVersion", primary_key: :id, key: :pacticipant_id dataset_module do include PactBroker::Repositories::Helpers diff --git a/lib/pact_broker/pacticipants/repository.rb b/lib/pact_broker/pacticipants/repository.rb index 101132edb..1b363cf10 100644 --- a/lib/pact_broker/pacticipants/repository.rb +++ b/lib/pact_broker/pacticipants/repository.rb @@ -23,7 +23,7 @@ def find_all def find options = {} query = PactBroker::Domain::Pacticipant.select_all_qualified query = query.label(options[:label_name]) if options[:label_name] - query.order_ignore_case(Sequel[:pacticipants][:name]).eager(:labels).all + query.order_ignore_case(Sequel[:pacticipants][:name]).eager(:labels).eager(:latest_version).all end def find_all_pacticipant_versions_in_reverse_order name diff --git a/lib/pact_broker/versions/latest_version.rb b/lib/pact_broker/versions/latest_version.rb new file mode 100644 index 000000000..00c4a0d38 --- /dev/null +++ b/lib/pact_broker/versions/latest_version.rb @@ -0,0 +1,11 @@ +require 'pact_broker/domain/version' + +module PactBroker + module Versions + include PactBroker::Repositories::Helpers + + class LatestVersion < PactBroker::Domain::Version + set_dataset(:latest_versions) + end + end +end