Skip to content

Commit 0ea23f1

Browse files
authored
Add second order_by parameter, as created_at might not be unique (#3658)
Resources included by decorators are ordered by 'created_at', which only has a resolution in seconds. This may lead to arbitrary ordering. To ensure a consistent ordering, 'guid', which has already been utilized by the SequelPaginator for secondary ordering, has been added to the decorators as well. Additional database statements aim to return the oldest or newest entries from a selection. For those, 'id' has been added as the second 'order_by' parameter. The specs testing the results of included resources have been improved to ensure a specific ordering by setting a definitive creation date. In some instances, the matcher has been changed from 'match_array' to 'eq', enabling the tests to verify the order of elements.
1 parent 244eb2b commit 0ea23f1

File tree

51 files changed

+205
-126
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+205
-126
lines changed

app/decorators/field_service_instance_broker_decorator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def decorate(hash, service_instances)
2222
join(:service_instances, service_plan_id: :service_plans__id).
2323
where(service_instances__id: managed_service_instances.map(&:id)).
2424
distinct.
25-
order_by(:service_brokers__created_at).
25+
order_by(:service_brokers__created_at, :service_brokers__guid).
2626
select(:service_brokers__name, :service_brokers__guid, :service_brokers__created_at).
2727
all
2828

app/decorators/field_service_instance_offering_decorator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def decorate(hash, service_instances)
2121
join(:service_instances, service_plan_id: :service_plans__id).
2222
where(service_instances__id: managed_service_instances.map(&:id)).
2323
distinct.
24-
order_by(:services__created_at).
24+
order_by(:services__created_at, :services__guid).
2525
select(:services__label, :services__guid, :services__description, :services__tags, :services__extra, :services__service_broker_id, :services__created_at).
2626
all
2727

app/decorators/field_service_instance_organization_decorator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def decorate(hash, resources)
1717
spaces = resources.map { |r| r.try(:space) || r }.uniq
1818
orgs = spaces.map(&:organization).uniq
1919

20-
hash[:included][:organizations] = orgs.sort_by(&:created_at).map do |org|
20+
hash[:included][:organizations] = orgs.sort_by { |o| [o.created_at, o.guid] }.map do |org|
2121
org_view = {}
2222
org_view[:name] = org.name if @fields.include?('name')
2323
org_view[:guid] = org.guid if @fields.include?('guid')

app/decorators/field_service_instance_plan_decorator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def decorate(hash, service_instances)
1919
hash[:included] ||= {}
2020
plans = managed_service_instances.map(&:service_plan).uniq
2121

22-
hash[:included][:service_plans] = plans.sort_by(&:created_at).map do |plan|
22+
hash[:included][:service_plans] = plans.sort_by { |p| [p.created_at, p.guid] }.map do |plan|
2323
plan_view = {}
2424
plan_view[:guid] = plan.guid if @fields.include?('guid')
2525
plan_view[:name] = plan.name if @fields.include?('name')

app/decorators/field_service_instance_space_decorator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def decorate(hash, resources)
1717

1818
spaces = resources.map { |r| r.try(:space) || r }.uniq
1919

20-
hash[:included][:spaces] = spaces.sort_by(&:created_at).map do |space|
20+
hash[:included][:spaces] = spaces.sort_by { |s| [s.created_at, s.guid] }.map do |space|
2121
temp = {}
2222
temp[:guid] = space.guid if @fields.include?('guid')
2323
temp[:name] = space.name if @fields.include?('name')

app/decorators/field_service_offering_service_broker_decorator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def initialize(fields)
1515
def decorate(hash, service_offerings)
1616
hash[:included] ||= {}
1717
service_brokers = service_offerings.map(&:service_broker).uniq
18-
hash[:included][:service_brokers] = service_brokers.sort_by(&:created_at).map do |broker|
18+
hash[:included][:service_brokers] = service_brokers.sort_by { |sb| [sb.created_at, sb.guid] }.map do |broker|
1919
broker_view = {}
2020
broker_view[:name] = broker.name if @fields.include?('name')
2121
broker_view[:guid] = broker.guid if @fields.include?('guid')

app/decorators/field_service_plan_service_broker_decorator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def decorate(hash, service_plans)
1616
hash[:included] ||= {}
1717
service_offerings = service_plans.map(&:service).uniq
1818
service_brokers = service_offerings.map(&:service_broker).uniq
19-
hash[:included][:service_brokers] = service_brokers.sort_by(&:created_at).map do |broker|
19+
hash[:included][:service_brokers] = service_brokers.sort_by { |sb| [sb.created_at, sb.guid] }.map do |broker|
2020
broker_view = {}
2121
broker_view[:name] = broker.name if @fields.include?('name')
2222
broker_view[:guid] = broker.guid if @fields.include?('guid')

app/decorators/include_binding_app_decorator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def apps(bindings)
1919
app_guids = bindings.pluck(:app_guid).compact.uniq
2020
return if app_guids.empty?
2121

22-
AppModel.where(guid: app_guids).order(:created_at).
22+
AppModel.where(guid: app_guids).order(:created_at, :guid).
2323
eager(Presenters::V3::AppPresenter.associated_resources).all
2424
end
2525
end

app/decorators/include_binding_route_decorator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def match?(include_params)
2020

2121
def routes(route_bindings)
2222
route_ids = route_bindings.map(&:route_id)
23-
routes = Route.where(id: route_ids).order_by(:created_at).
23+
routes = Route.where(id: route_ids).order_by(:created_at, :guid).
2424
eager(Presenters::V3::RoutePresenter.associated_resources).all
2525
routes.map { |route| Presenters::V3::RoutePresenter.new(route).to_hash }
2626
end

app/decorators/include_binding_service_instance_decorator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def decorate(hash, bindings)
1818
private
1919

2020
def service_instances(bindings)
21-
ServiceInstance.where(guid: bindings.map(&:service_instance_guid).uniq).order(:created_at).
21+
ServiceInstance.where(guid: bindings.map(&:service_instance_guid).uniq).order(:created_at, :guid).
2222
eager(Presenters::V3::ServiceInstancePresenter.associated_resources).all
2323
end
2424
end

0 commit comments

Comments
 (0)