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