diff options
author | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2017-07-13 13:25:11 +0200 |
---|---|---|
committer | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2017-07-18 10:51:19 +0200 |
commit | 5cd6f8c76ec74c85e6ff6dae9736b27baeb55988 (patch) | |
tree | 4606388025752bdeb9312293749124a185ef3719 /db/migrate | |
parent | 9e5c8e5d7f81b88998c8def2f4a20d54eeee49d0 (diff) | |
download | gitlab-ce-5cd6f8c76ec74c85e6ff6dae9736b27baeb55988.tar.gz |
Add a foreign key to `merge_requests.head_pipeline_id`
Diffstat (limited to 'db/migrate')
-rw-r--r-- | db/migrate/20170713104829_add_foreign_key_to_merge_requests.rb | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/db/migrate/20170713104829_add_foreign_key_to_merge_requests.rb b/db/migrate/20170713104829_add_foreign_key_to_merge_requests.rb new file mode 100644 index 00000000000..78ff7527967 --- /dev/null +++ b/db/migrate/20170713104829_add_foreign_key_to_merge_requests.rb @@ -0,0 +1,28 @@ +class AddForeignKeyToMergeRequests < ActiveRecord::Migration + include Gitlab::Database::MigrationHelpers + + DOWNTIME = false + + disable_ddl_transaction! + + def up + unless foreign_key_exists?(:merge_requests, :head_pipeline_id) + add_concurrent_foreign_key(:merge_requests, :ci_pipelines, + column: :head_pipeline_id, on_delete: :nullify) + end + end + + def down + if foreign_key_exists?(:merge_requests, :head_pipeline_id) + remove_foreign_key(:merge_requests, column: :head_pipeline_id) + end + end + + private + + def foreign_key_exists?(table, column) + foreign_keys(table).any? do |key| + key.options[:column] == column.to_s + end + end +end |