Commit 5af3eb5f authored by Grzegorz Bizon's avatar Grzegorz Bizon

Check foreign keys in migration in separate conditional

parent 6a615285
...@@ -8,14 +8,28 @@ class AddStageIdForeignKeyToBuilds < ActiveRecord::Migration ...@@ -8,14 +8,28 @@ class AddStageIdForeignKeyToBuilds < ActiveRecord::Migration
def up def up
unless index_exists?(:ci_builds, :stage_id) unless index_exists?(:ci_builds, :stage_id)
add_concurrent_index(:ci_builds, :stage_id) add_concurrent_index(:ci_builds, :stage_id)
end
unless foreign_key_exists?(:ci_builds, :stage_id)
add_concurrent_foreign_key(:ci_builds, :ci_stages, column: :stage_id, on_delete: :cascade) add_concurrent_foreign_key(:ci_builds, :ci_stages, column: :stage_id, on_delete: :cascade)
end end
end end
def down def down
if index_exists?(:ci_builds, :stage_id) if foreign_key_exists?(:ci_builds, :stage_id)
remove_foreign_key(:ci_builds, column: :stage_id) remove_foreign_key(:ci_builds, column: :stage_id)
end
if index_exists?(:ci_builds, :stage_id)
remove_concurrent_index(:ci_builds, :stage_id) remove_concurrent_index(:ci_builds, :stage_id)
end end
end end
private
def foreign_key_exists?(table, column)
foreign_keys(:ci_builds).any? do |key|
key.options[:column] == column.to_s
end
end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment