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 0bf7cb587..20ac21996 100644 Binary files a/db/pact_broker_database.sqlite3 and b/db/pact_broker_database.sqlite3 differ 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