Skip to content

Commit

Permalink
Add unique index to svc_inst_op on svc_inst_id
Browse files Browse the repository at this point in the history
* Add unique index on column service_instance_id to prevent double entries for the same
  service_instance
* Remove duplicate service_instance_id to prepare for adding a uniqueness constraint
* Drop old index before creating the unique one

Compared to service bindings, service keys and route bindings there is a UNIQUE index
defined for the "resource_id" field; for service instances the UNIQUE keyword is missing.
  • Loading branch information
kathap committed Aug 25, 2022
1 parent 2ab201a commit 23cd15c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Sequel.migration do
up do
# Remove duplicate service_instance_operations.service_instance_id to prepare for adding a uniqueness constraint
# assumption is that the max id = newest entry for the same svc_inst_id
max_of_dup_groups = self[:service_instance_operations].
select(Sequel.function(:max, :id)).
group_by(:service_instance_id).
having { count.function.* >= 1 }

self[:service_instance_operations].exclude(id: max_of_dup_groups).each do |row|
self[:service_instance_operations].where(id: row[:id]).delete
end
alter_table :service_instance_operations do
drop_index :service_instance_id, name: :svc_instance_id_index, if_exists: true
add_index :service_instance_id, name: :svc_inst_op_svc_instance_id_unique_index, unique: true
end
end
down do
alter_table :service_instance_operations do
drop_index :service_instance_id
add_index :service_instance_id, name: :svc_instance_id_index
end
end
end
32 changes: 32 additions & 0 deletions spec/unit/models/services/service_instance_operation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,37 @@ module VCAP::CloudController
expect(operation.state).to eq 'finished'
end
end

describe 'when two are created with the same id' do
describe 'when a ServiceInstanceOperation exists' do
let(:service_instance_attrs) do
{
name: 'bommel_instance',
space: VCAP::CloudController::Space.make
}
end

let(:service_instance) { ServiceInstance.create(service_instance_attrs) }
let(:operation_attributes2) do
{
service_instance_id: service_instance.id,
state: 'in progress',
description: '50% all the time',
type: 'create',
proposed_changes: {
name: 'pizza',
service_plan_guid: '1800-pizza',
},
}
end
before { ServiceInstanceOperation.create(operation_attributes2) }

it 'raises an exception when creating another ServiceInstanceOperation' do
expect {
ServiceInstanceOperation.create(operation_attributes2)
}.to raise_error(Sequel::UniqueConstraintViolation)
end
end
end
end
end

0 comments on commit 23cd15c

Please sign in to comment.