From 02a2200f9c80c1f10ce69155b5b3bd009a92c4f9 Mon Sep 17 00:00:00 2001 From: Sam Gunaratne Date: Wed, 20 Nov 2024 11:23:56 -0700 Subject: [PATCH] Use destroy over delete for deleting unmapped routes This triggers the deletion of associated resources, like labels/annotations. Otherwise this operation gives a FK error. --- app/actions/space_delete_unmapped_routes.rb | 2 +- .../actions/space_delete_unmapped_routes_spec.rb | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/app/actions/space_delete_unmapped_routes.rb b/app/actions/space_delete_unmapped_routes.rb index b43913c1c0d..ae6d3506a76 100644 --- a/app/actions/space_delete_unmapped_routes.rb +++ b/app/actions/space_delete_unmapped_routes.rb @@ -7,7 +7,7 @@ def delete(space) space.routes_dataset. exclude(guid: RouteMappingModel.select(:route_guid)). exclude(id: RouteBinding.select(:route_id)). - delete + destroy end end end diff --git a/spec/unit/actions/space_delete_unmapped_routes_spec.rb b/spec/unit/actions/space_delete_unmapped_routes_spec.rb index 12922efe0b0..504baebc8bb 100644 --- a/spec/unit/actions/space_delete_unmapped_routes_spec.rb +++ b/spec/unit/actions/space_delete_unmapped_routes_spec.rb @@ -71,6 +71,22 @@ module VCAP::CloudController expect { unbound_and_unmapped_route.refresh }.to raise_error Sequel::Error, 'Record not found' end end + + context 'when the unmapped routes have labels and annotations' do + let!(:unmapped_route_1) { Route.make(domain: domain, space: space, host: 'unmapped1') } + let!(:label_1) { RouteLabelModel.make(key_name: 'k1', value: 'v1', resource_guid: unmapped_route_1.guid) } + let!(:annot_1) { RouteAnnotationModel.make(key_name: 'k1', value: 'v1', resource_guid: unmapped_route_1.guid) } + + it 'deletes the labels, annotations and routes' do + expect do + subject.delete(space) + end.to change(VCAP::CloudController::Route, :count).by(-1) + + expect { unmapped_route_1.refresh }.to raise_error Sequel::Error, 'Record not found' + expect { label_1.refresh }.to raise_error Sequel::Error, 'Record not found' + expect { annot_1.refresh }.to raise_error Sequel::Error, 'Record not found' + end + end end end end