|
| 1 | +def constraint_exists?(db, table, name) |
| 2 | + if db.database_type == :mysql # MySQL 5.7 workaround |
| 3 | + db.from(:information_schema).where( |
| 4 | + table_schema: Sequel.function(:database), |
| 5 | + table_name: table.to_s, |
| 6 | + constraint_name: name |
| 7 | + ).count > 0 |
| 8 | + else |
| 9 | + db.indexes(table).key?(name.to_sym) |
| 10 | + end |
| 11 | +end |
| 12 | + |
1 | 13 | Sequel.migration do |
2 | 14 | no_transaction # adding an index concurrently cannot be done within a transaction |
3 | 15 |
|
4 | 16 | 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 |
| 17 | + db = self |
| 18 | + alter_table(:service_bindings) do |
| 19 | + drop_constraint(:unique_service_binding_service_instance_guid_app_guid) if constraint_exists?(db, :service_bindings, 'unique_service_binding_service_instance_guid_app_guid') |
| 20 | + drop_constraint(:unique_service_binding_app_guid_name) if constraint_exists?(db, :service_bindings, 'unique_service_binding_app_guid_name') |
11 | 21 | end |
12 | 22 |
|
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 |
| 23 | + if database_type == :postgres |
| 24 | + VCAP::Migration.with_concurrent_timeout(self) do |
| 25 | + 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 |
| 26 | + add_index :service_bindings, %i[app_guid name], name: :service_bindings_app_guid_name_index, concurrently: true, if_not_exists: true |
| 27 | + end |
| 28 | + else |
| 29 | + alter_table(:service_bindings) do |
| 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, concurrently: true |
| 33 | + end |
| 34 | + unless @db.indexes(:service_bindings).key?(:service_bindings_app_guid_name_index) |
| 35 | + add_index %i[app_guid name], name: :service_bindings_app_guid_name_index, |
| 36 | + concurrently: true |
| 37 | + end |
| 38 | + end |
15 | 39 | end |
16 | 40 | end |
17 | 41 |
|
18 | 42 | 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) |
| 43 | + db = self |
| 44 | + alter_table(:service_bindings) do |
| 45 | + unless constraint_exists?(db, :service_bindings, 'unique_service_binding_service_instance_guid_app_guid') |
| 46 | + add_unique_constraint %i[service_instance_guid app_guid], |
| 47 | + name: :unique_service_binding_service_instance_guid_app_guid |
26 | 48 | end |
| 49 | + add_unique_constraint %i[app_guid name], name: :unique_service_binding_app_guid_name unless constraint_exists?(db, :service_bindings, 'unique_service_binding_app_guid_name') |
27 | 50 | end |
28 | 51 |
|
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 |
| 52 | + if database_type == :postgres |
| 53 | + VCAP::Migration.with_concurrent_timeout(self) do |
| 54 | + drop_index :service_bindings, %i[app_guid service_instance_guid], name: :service_bindings_app_guid_service_instance_guid_index, concurrently: true, if_exists: true |
| 55 | + drop_index :service_bindings, %i[app_guid name], name: :service_bindings_app_guid_name_index, concurrently: true, if_exists: true |
| 56 | + end |
| 57 | + else |
| 58 | + alter_table(:service_bindings) do |
| 59 | + if @db.indexes(:service_bindings).key?(:service_bindings_app_guid_service_instance_guid_index) |
| 60 | + drop_index %i[app_guid service_instance_guid], |
| 61 | + name: :service_bindings_app_guid_service_instance_guid_index, concurrently: true |
| 62 | + end |
| 63 | + 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) |
| 64 | + end |
31 | 65 | end |
32 | 66 | end |
33 | 67 | end |
0 commit comments