Skip to content

Commit

Permalink
feat: eager load latest version for /pacticipants resource
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Aug 27, 2018
1 parent 01dd866 commit 9ac2ba9
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 3 deletions.
25 changes: 25 additions & 0 deletions db/migrations/20180828_create_latest_versions.rb
Original file line number Diff line number Diff line change
@@ -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
Binary file modified db/pact_broker_database.sqlite3
Binary file not shown.
5 changes: 3 additions & 2 deletions lib/pact_broker/domain/pacticipant.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/pacticipants/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions lib/pact_broker/versions/latest_version.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 9ac2ba9

Please sign in to comment.