-
Notifications
You must be signed in to change notification settings - Fork 361
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unique index to svc_inst_op on svc_inst_id
* Add unique index on column service_instance_id to prevent double entries for the same service_instance * Remove duplicate service_instance_id entries based on max(updated_at) (=keep the newest entry) to prepare for adding a uniqueness constraint * Drop foreign_key constraint which references service_instance_id (needed for mysql) and old index before creating the unique index 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
Showing
2 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
...ons/20220818142407_add_unique_index_to_service_instance_operations_service_instance_id.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
Sequel.migration do | ||
up do | ||
# Remove duplicate service_instance_operations.service_instance_id to prepare for adding a uniqueness constraint | ||
max_updated_at_group = self[:service_instance_operations]. | ||
select(Sequel.function(:max, :updated_at)). | ||
group_by(:service_instance_id). | ||
having { count.function.* >= 1 } | ||
service_instance_id_group = self[:service_instance_operations]. | ||
select(:service_instance_id). | ||
group_by(:service_instance_id). | ||
having { count.function.* >= 1 } | ||
self[:service_instance_operations].exclude(id: self[:service_instance_operations].select(:id). | ||
where { Sequel.&(Sequel.|({ service_instance_id: service_instance_id_group }, { service_instance_id: nil }), | ||
{ updated_at: max_updated_at_group }) | ||
}).each do |row| | ||
self[:service_instance_operations].where(id: row[:id]).delete | ||
end | ||
# for mysql the foreign_key constraint which references service_instance_id has to be removed before you can | ||
# delete the old index from service_instance_id | ||
alter_table :service_instance_operations do | ||
drop_constraint :fk_svc_inst_op_svc_instance_id, type: :foreign_key | ||
drop_index :service_instance_id, name: :svc_instance_id_index | ||
add_index :service_instance_id, name: :svc_inst_op_svc_instance_id_unique_index, unique: true | ||
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 | ||
drop_index :service_instance_id, name: :svc_inst_op_svc_instance_id_unique_index | ||
add_index :service_instance_id, name: :svc_instance_id_index | ||
add_foreign_key [:service_instance_id], :service_instances, key: :id, name: :fk_svc_inst_op_svc_instance_id | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters