|
2 | 2 | no_transaction # adding an index concurrently cannot be done within a transaction |
3 | 3 |
|
4 | 4 | up do |
5 | | - transaction do |
6 | | - alter_table :service_bindings do |
7 | | - drop_constraint :unique_service_binding_service_instance_guid_app_guid if @db.indexes(:service_bindings).include?(:unique_service_binding_service_instance_guid_app_guid) |
8 | | - |
9 | | - drop_constraint :unique_service_binding_app_guid_name if @db.indexes(:service_bindings).include?(:unique_service_binding_app_guid_name) |
10 | | - end |
| 5 | + if indexes(:service_bindings)[:unique_service_binding_service_instance_guid_app_guid].present? |
| 6 | + alter_table(:service_bindings) { drop_constraint(:unique_service_binding_service_instance_guid_app_guid) } |
11 | 7 | end |
| 8 | + alter_table(:service_bindings) { drop_constraint(:unique_service_binding_app_guid_name) } if indexes(:service_bindings)[:unique_service_binding_app_guid_name].present? |
12 | 9 |
|
13 | | - VCAP::Migration.with_concurrent_timeout(self) do |
14 | | - add_index :service_bindings, %i[app_guid service_instance_guid], name: :service_bindings_app_guid_service_instance_guid_index, if_not_exists: true, concurrently: true |
| 10 | + if database_type == :postgres |
| 11 | + VCAP::Migration.with_concurrent_timeout(self) do |
| 12 | + add_index :service_bindings, %i[app_guid service_instance_guid], name: :service_bindings_app_guid_service_instance_guid_index, concurrently: true, if_not_exists: true |
| 13 | + add_index :service_bindings, %i[app_guid name], name: :service_bindings_app_guid_name_index, concurrently: true, if_not_exists: true |
| 14 | + end |
| 15 | + elsif database_type == :mysql |
| 16 | + alter_table(:service_bindings) do |
| 17 | + # rubocop:disable Sequel/ConcurrentIndex |
| 18 | + unless @db.indexes(:service_bindings).key?(:service_bindings_app_guid_service_instance_guid_index) |
| 19 | + add_index %i[app_guid service_instance_guid], |
| 20 | + name: :service_bindings_app_guid_service_instance_guid_index |
| 21 | + end |
| 22 | + add_index %i[app_guid name], name: :service_bindings_app_guid_name_index unless @db.indexes(:service_bindings).key?(:service_bindings_app_guid_name_index) |
| 23 | + # rubocop:enable Sequel/ConcurrentIndex |
| 24 | + end |
15 | 25 | end |
16 | 26 | end |
17 | 27 |
|
18 | 28 | down do |
19 | | - transaction do |
20 | | - alter_table :service_bindings do |
21 | | - unless @db.indexes(:service_bindings).include?(:unique_service_binding_service_instance_guid_app_guid) |
22 | | - add_unique_constraint %i[service_instance_guid app_guid], |
23 | | - name: :unique_service_binding_service_instance_guid_app_guid |
24 | | - end |
25 | | - add_unique_constraint %i[app_guid name], name: :unique_service_binding_app_guid_name unless @db.indexes(:service_bindings).include?(:unique_service_binding_app_guid_name) |
| 29 | + if indexes(:service_bindings)[:unique_service_binding_service_instance_guid_app_guid].blank? |
| 30 | + alter_table(:service_bindings) do |
| 31 | + add_unique_constraint %i[service_instance_guid app_guid], |
| 32 | + name: :unique_service_binding_service_instance_guid_app_guid |
| 33 | + end |
| 34 | + end |
| 35 | + if indexes(:service_bindings)[:unique_service_binding_app_guid_name].blank? |
| 36 | + alter_table(:service_bindings) do |
| 37 | + add_unique_constraint %i[app_guid name], name: :unique_service_binding_app_guid_name |
26 | 38 | end |
27 | 39 | end |
28 | 40 |
|
29 | | - VCAP::Migration.with_concurrent_timeout(self) do |
30 | | - drop_index :service_bindings, %i[app_guid service_instance_guid], name: :service_bindings_app_guid_service_instance_guid_index, if_exists: true, concurrently: true |
| 41 | + if database_type == :postgres |
| 42 | + VCAP::Migration.with_concurrent_timeout(self) do |
| 43 | + drop_index :service_bindings, %i[app_guid service_instance_guid], name: :service_bindings_app_guid_service_instance_guid_index, concurrently: true, if_exists: true |
| 44 | + drop_index :service_bindings, %i[app_guid name], name: :service_bindings_app_guid_name_index, concurrently: true, if_exists: true |
| 45 | + end |
| 46 | + elsif database_type == :mysql |
| 47 | + alter_table(:service_bindings) do |
| 48 | + # rubocop:disable Sequel/ConcurrentIndex |
| 49 | + if @db.indexes(:service_bindings).key?(:service_bindings_app_guid_service_instance_guid_index) |
| 50 | + drop_index %i[app_guid service_instance_guid], name: :service_bindings_app_guid_service_instance_guid_index |
| 51 | + end |
| 52 | + drop_index %i[app_guid name], name: :service_bindings_app_guid_name_index if @db.indexes(:service_bindings).key?(:service_bindings_app_guid_name_index) |
| 53 | + # rubocop:enable Sequel/ConcurrentIndex |
| 54 | + end |
31 | 55 | end |
32 | 56 | end |
33 | 57 | end |
0 commit comments