Concurrent migration helpers: Check for existence of index, foreign keys
In many migrations, we don't use transactions to allow for concurrent index builds and such.
Those migrations can fail right in the middle and leave an inconsistent state. For example, consider this migration:
def up
add_concurrent_index :table1, :a
add_concurrent_index :table2, :b
end
This should be rewritten with existence checks so we can safely recover from the inconsistent state by re-running the migration:
def up
add_concurrent_index :table1, :a unless index_exists?(:table1, :a)
add_concurrent_index :table2, :b unless index_exists?(:table2, :b)
end
Similar for add_concurrent_foreign_key
and maybe others.
The idea in this issue is to automatically do these checks in the existing migration helpers, so we don't have to repeat this in each migration step. If the object we want to create (index, FK) exists already - do nothing.
Ideally, this should not do anything if the index already exists (also not fail):
add_concurrent_index :table1, :a
Edited by Andreas Brandl