|
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) |
| 5 | + if database_type == :mysql && server_version < 80_000 # MySQL versions < 8 cannot drop unique constraints directly |
| 6 | + alter_table(:service_bindings) do |
| 7 | + # rubocop:disable Sequel/ConcurrentIndex |
| 8 | + if @db.indexes(:service_bindings).key?(:unique_service_binding_service_instance_guid_app_guid) |
| 9 | + drop_index %i[service_instance_guid app_guid], |
| 10 | + name: :unique_service_binding_service_instance_guid_app_guid |
| 11 | + end |
| 12 | + drop_index %i[app_guid name], name: :unique_service_binding_app_guid_name if @db.indexes(:service_bindings).key?(:unique_service_binding_app_guid_name) |
| 13 | + # rubocop:enable Sequel/ConcurrentIndex |
| 14 | + end |
| 15 | + else |
| 16 | + alter_table(:service_bindings) do |
| 17 | + drop_constraint(:unique_service_binding_service_instance_guid_app_guid) if @db.indexes(:service_bindings).key?(:unique_service_binding_service_instance_guid_app_guid) |
| 18 | + drop_constraint(:unique_service_binding_app_guid_name) if @db.indexes(:service_bindings).key?(:unique_service_binding_app_guid_name) |
10 | 19 | end |
11 | 20 | end |
12 | 21 |
|
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 |
| 22 | + if database_type == :postgres |
| 23 | + VCAP::Migration.with_concurrent_timeout(self) do |
| 24 | + 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 |
| 25 | + add_index :service_bindings, %i[app_guid name], name: :service_bindings_app_guid_name_index, concurrently: true, if_not_exists: true |
| 26 | + end |
| 27 | + elsif database_type == :mysql |
| 28 | + alter_table(:service_bindings) do |
| 29 | + # rubocop:disable Sequel/ConcurrentIndex |
| 30 | + unless @db.indexes(:service_bindings).key?(:service_bindings_app_guid_service_instance_guid_index) |
| 31 | + add_index %i[app_guid service_instance_guid], |
| 32 | + name: :service_bindings_app_guid_service_instance_guid_index |
| 33 | + end |
| 34 | + 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) |
| 35 | + # rubocop:enable Sequel/ConcurrentIndex |
| 36 | + end |
15 | 37 | end |
16 | 38 | end |
17 | 39 |
|
18 | 40 | 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) |
| 41 | + alter_table(:service_bindings) do |
| 42 | + if @db.indexes(:service_bindings)[:unique_service_binding_service_instance_guid_app_guid].blank? |
| 43 | + add_unique_constraint %i[service_instance_guid app_guid], |
| 44 | + name: :unique_service_binding_service_instance_guid_app_guid |
26 | 45 | end |
27 | 46 | end |
| 47 | + alter_table(:service_bindings) do |
| 48 | + add_unique_constraint %i[app_guid name], name: :unique_service_binding_app_guid_name if @db.indexes(:service_bindings)[:unique_service_binding_app_guid_name].blank? |
| 49 | + end |
28 | 50 |
|
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 |
| 51 | + if database_type == :postgres |
| 52 | + VCAP::Migration.with_concurrent_timeout(self) do |
| 53 | + drop_index :service_bindings, %i[app_guid service_instance_guid], name: :service_bindings_app_guid_service_instance_guid_index, concurrently: true, if_exists: true |
| 54 | + drop_index :service_bindings, %i[app_guid name], name: :service_bindings_app_guid_name_index, concurrently: true, if_exists: true |
| 55 | + end |
| 56 | + elsif database_type == :mysql |
| 57 | + alter_table(:service_bindings) do |
| 58 | + # rubocop:disable Sequel/ConcurrentIndex |
| 59 | + if @db.indexes(:service_bindings).key?(:service_bindings_app_guid_service_instance_guid_index) |
| 60 | + drop_index %i[app_guid service_instance_guid], name: :service_bindings_app_guid_service_instance_guid_index |
| 61 | + end |
| 62 | + 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) |
| 63 | + # rubocop:enable Sequel/ConcurrentIndex |
| 64 | + end |
31 | 65 | end |
32 | 66 | end |
33 | 67 | end |
0 commit comments