Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorYorick Peterse <yorickpeterse@gmail.com>2019-04-11 17:29:44 +0300
committerYorick Peterse <yorickpeterse@gmail.com>2019-06-17 18:06:20 +0300
commit0f777a8d49568d3f5ffdd4b75b594f07e9fbd2f0 (patch)
tree848ee08d3e8c7dde2a5308dfafc30df856196d34 /lib
parentcfcdfdd2de6009e7ce55e6a415825a0eca75f0c9 (diff)
Allow custom names for concurrent foreign keys
This is necessary for backporting the EE schema to ensure backported foreign keys use the same key names.
Diffstat (limited to 'lib')
-rw-r--r--lib/gitlab/database/migration_helpers.rb14
1 files changed, 9 insertions, 5 deletions
diff --git a/lib/gitlab/database/migration_helpers.rb b/lib/gitlab/database/migration_helpers.rb
index cc61bb7fa02..1b5cd0fbb07 100644
--- a/lib/gitlab/database/migration_helpers.rb
+++ b/lib/gitlab/database/migration_helpers.rb
@@ -149,7 +149,7 @@ module Gitlab
# column - The name of the column to create the foreign key on.
# on_delete - The action to perform when associated data is removed,
# defaults to "CASCADE".
- def add_concurrent_foreign_key(source, target, column:, on_delete: :cascade)
+ def add_concurrent_foreign_key(source, target, column:, on_delete: :cascade, name: nil)
# Transactions would result in ALTER TABLE locks being held for the
# duration of the transaction, defeating the purpose of this method.
if transaction_open?
@@ -167,14 +167,18 @@ module Gitlab
return
end
- return add_foreign_key(source, target,
- column: column,
- on_delete: on_delete)
+ key_options = { column: column, on_delete: on_delete }
+
+ # The MySQL adapter tries to create a foreign key without a name when
+ # `:name` is nil, instead of generating a name for us.
+ key_options[:name] = name if name
+
+ return add_foreign_key(source, target, key_options)
else
on_delete = 'SET NULL' if on_delete == :nullify
end
- key_name = concurrent_foreign_key_name(source, column)
+ key_name = name || concurrent_foreign_key_name(source, column)
unless foreign_key_exists?(source, target, column: column)
Rails.logger.warn "Foreign key not created because it exists already " \