Skip to content

Commit f5a3fc3

Browse files
committed
feat: concurrent migration for postgres db
1 parent 41cac0c commit f5a3fc3

1 file changed

Lines changed: 30 additions & 4 deletions

File tree

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
Sequel.migration do
2+
no_transaction # required for concurrently option on postgres
3+
24
up do
35
transaction do
46
duplicates = self[:sidecars].
57
select(:app_guid, :name).
68
group(:app_guid, :name).
79
having { count(1) > 1 }
10+
811
duplicates.each do |dup|
912
ids_to_remove = self[:sidecars].
1013
where(app_guid: dup[:app_guid], name: dup[:name]).
@@ -15,19 +18,42 @@
1518

1619
self[:sidecars].where(id: ids_to_remove).delete
1720
end
21+
end
1822

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
1932
alter_table(:sidecars) do
33+
# rubocop:disable Sequel/ConcurrentIndex -- MySQL does not support concurrent index operations
2034
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
2337
end
38+
# rubocop:enable Sequel/ConcurrentIndex
2439
end
2540
end
2641
end
2742

2843
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
3157
end
3258
end
3359
end

0 commit comments

Comments
 (0)