Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve query to check if broker has service instances #2855

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions app/models/services/service_broker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ def space_scoped?
end

def has_service_instances?
services.select do |service|
service.service_plans.select { |plan|
plan.service_instances.any?
}.any?
end.any?
!VCAP::CloudController::ServiceInstance.
join(:service_plans, id: :service_plan_id).
join(:services, id: :service_id).
where(services__service_broker_id: id).
empty?
end

def self.user_visibility_filter(user)
Expand Down
25 changes: 20 additions & 5 deletions spec/unit/models/services/service_broker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,28 @@ module VCAP::CloudController
let(:service) { Service.make(service_broker: service_broker) }
let!(:service_plan) { ServicePlan.make(service: service) }

it 'returns true when there are service instances' do
ManagedServiceInstance.make(service_plan: service_plan)
expect(service_broker.has_service_instances?).to eq(true)
context 'when there are service instances' do
before do
ManagedServiceInstance.make(service_plan: service_plan)
end

it 'returns true' do
expect(service_broker.has_service_instances?).to eq(true)
end

it 'does a single db query' do
expect { service_broker.has_service_instances? }.to have_queried_db_times(/select/i, 1)
end
end

it 'return false when there are no service instances' do
expect(service_broker.has_service_instances?).to eq(false)
context 'when there are no service instances' do
it 'returns false' do
expect(service_broker.has_service_instances?).to eq(false)
end

it 'does a single db query' do
expect { service_broker.has_service_instances? }.to have_queried_db_times(/select/i, 1)
end
end
end
end
Expand Down