|
1 | 1 | Sequel.migration do |
| 2 | + no_transaction # required for concurrently option on postgres |
| 3 | + |
2 | 4 | up do |
3 | 5 | transaction do |
4 | 6 | duplicates = self[:sidecars]. |
5 | 7 | select(:app_guid, :name). |
6 | 8 | group(:app_guid, :name). |
7 | 9 | having { count(1) > 1 } |
| 10 | + |
8 | 11 | duplicates.each do |dup| |
9 | 12 | ids_to_remove = self[:sidecars]. |
10 | 13 | where(app_guid: dup[:app_guid], name: dup[:name]). |
|
15 | 18 |
|
16 | 19 | self[:sidecars].where(id: ids_to_remove).delete |
17 | 20 | end |
| 21 | + end |
18 | 22 |
|
| 23 | + if database_type == :postgres |
| 24 | + VCAP::Migration.with_concurrent_timeout(self) do |
| 25 | + add_index :sidecars, %i[app_guid name], |
| 26 | + name: :sidecars_app_guid_name_index, |
| 27 | + unique: true, |
| 28 | + concurrently: true, |
| 29 | + if_not_exists: true |
| 30 | + end |
| 31 | + else |
19 | 32 | alter_table(:sidecars) do |
| 33 | + # rubocop:disable Sequel/ConcurrentIndex -- MySQL does not support concurrent index operations |
20 | 34 | unless @db.indexes(:sidecars).key?(:sidecars_app_guid_name_index) |
21 | | - add_unique_constraint %i[app_guid name], |
22 | | - name: :sidecars_app_guid_name_index |
| 35 | + add_index %i[app_guid name], unique: true, |
| 36 | + name: :sidecars_app_guid_name_index |
23 | 37 | end |
| 38 | + # rubocop:enable Sequel/ConcurrentIndex |
24 | 39 | end |
25 | 40 | end |
26 | 41 | end |
27 | 42 |
|
28 | 43 | down do |
29 | | - alter_table(:sidecars) do |
30 | | - drop_constraint(:sidecars_app_guid_name_index, type: :unique) if @db.indexes(:sidecars).key?(:sidecars_app_guid_name_index) |
| 44 | + if database_type == :postgres |
| 45 | + VCAP::Migration.with_concurrent_timeout(self) do |
| 46 | + drop_index :sidecars, nil, |
| 47 | + name: :sidecars_app_guid_name_index, |
| 48 | + concurrently: true, |
| 49 | + if_exists: true |
| 50 | + end |
| 51 | + else |
| 52 | + alter_table(:sidecars) do |
| 53 | + # rubocop:disable Sequel/ConcurrentIndex -- MySQL does not support concurrent index operations |
| 54 | + drop_index %i[app_guid name], name: :sidecars_app_guid_name_index if @db.indexes(:sidecars).key?(:sidecars_app_guid_name_index) |
| 55 | + # rubocop:enable Sequel/ConcurrentIndex |
| 56 | + end |
31 | 57 | end |
32 | 58 | end |
33 | 59 | end |
0 commit comments