Skip to content

Commit

Permalink
Add delete on cascade to fk_svc_inst_op_svc_instance_id
Browse files Browse the repository at this point in the history
We can see double entries for the same service_instance_id
in the service_instance_operations table. This leads to:
PG::ForeignKeyViolation: ERROR:  update or delete on table
"service_instances" violates foreign key constraint
"fk_svc_inst_op_svc_instance_id" on table "service_instance_operations"

For service bindings, service keys and route bindings the foreign
key constraint from operation to resource contains ON DELETE CASCADE;
for service instances this is missing.

Adding ON DELETE CASCADE to FK fk_svc_inst_op_svc_instance_id in
service_instance_operations
  • Loading branch information
kathap committed Aug 29, 2022
1 parent 513dae1 commit ea7d7e4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Sequel.migration do
# Add delete cascade to fk_svc_inst_op_svc_instance_id, without there won't be cleaned up all ocurrencies in
# the service_instance_operations table

up do
alter_table :service_instance_operations do
drop_constraint :fk_svc_inst_op_svc_instance_id, type: :foreign_key
add_foreign_key [:service_instance_id], :service_instances, key: :id, name: :fk_svc_inst_op_svc_instance_id, on_delete: :cascade
end
end

down do
alter_table :service_instance_operations do
drop_constraint :fk_svc_inst_op_svc_instance_id, type: :foreign_key
add_foreign_key [:service_instance_id], :service_instances, key: :id, name: :fk_svc_inst_op_svc_instance_id
end
end
end
25 changes: 25 additions & 0 deletions spec/unit/models/services/service_instance_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,31 @@ module VCAP::CloudController
expect(ServiceInstanceAnnotationModel.where(id: annotation.id)).to be_empty
end

it 'cascade deletes all ServiceInstanceOperations for this instance' do
last_operation = ServiceInstanceOperation.make
service_instance.service_instance_operation = last_operation

service_instance.destroy

expect(ServiceInstance.find(guid: service_instance.guid)).to be_nil
expect(ServiceInstanceOperation.find(guid: last_operation.guid)).to be_nil
end

it 'cascades deletion of related dependencies' do
binding = ServiceInstance.make
ServiceInstanceLabelModel.make(key_name: 'foo', value: 'bar', service_instance: binding)
ServiceInstanceAnnotationModel.make(key_name: 'baz', value: 'wow', service_instance: binding)
last_operation = ServiceInstanceOperation.make
binding.service_instance_operation = last_operation

binding.destroy

expect(ServiceInstance.find(guid: binding.guid)).to be_nil
expect(ServiceInstanceOperation.find(id: last_operation.id)).to be_nil
expect(ServiceInstanceLabelModel.find(resource_guid: binding.guid)).to be_nil
expect(ServiceInstanceAnnotationModel.find(resource_guid: binding.guid)).to be_nil
end

it 'creates a DELETED service usage event' do
expect {
service_instance.destroy
Expand Down

0 comments on commit ea7d7e4

Please sign in to comment.