summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorRémy Coutable <remy@rymai.me>2017-02-21 17:00:46 +0000
committerRémy Coutable <remy@rymai.me>2017-02-21 17:00:46 +0000
commitc9fcae8e855139362532a9c1f63b688a6b3b3107 (patch)
treed3071bdbede4ce05f614ab905440ee4e1795a37f /lib
parent2d2ed828880f620238dc7ef5c9f75fb6e7fef59a (diff)
parent79696f5b7aaf260176355026e91af05d40d92d0c (diff)
downloadgitlab-ce-c9fcae8e855139362532a9c1f63b688a6b3b3107.tar.gz
Merge branch 'hash-concurrent-foreign-key-names' into 'master'
Hash concurrent foreign key names similar to Rails See merge request !9415
Diffstat (limited to 'lib')
-rw-r--r--lib/gitlab/database/migration_helpers.rb11
1 files changed, 10 insertions, 1 deletions
diff --git a/lib/gitlab/database/migration_helpers.rb b/lib/gitlab/database/migration_helpers.rb
index 4800a509b37..fc445ab9483 100644
--- a/lib/gitlab/database/migration_helpers.rb
+++ b/lib/gitlab/database/migration_helpers.rb
@@ -54,7 +54,7 @@ module Gitlab
disable_statement_timeout
- key_name = "fk_#{source}_#{target}_#{column}"
+ key_name = concurrent_foreign_key_name(source, column)
# Using NOT VALID allows us to create a key without immediately
# validating it. This means we keep the ALTER TABLE lock only for a
@@ -74,6 +74,15 @@ module Gitlab
execute("ALTER TABLE #{source} VALIDATE CONSTRAINT #{key_name};")
end
+ # Returns the name for a concurrent foreign key.
+ #
+ # PostgreSQL constraint names have a limit of 63 bytes. The logic used
+ # here is based on Rails' foreign_key_name() method, which unfortunately
+ # is private so we can't rely on it directly.
+ def concurrent_foreign_key_name(table, column)
+ "fk_#{Digest::SHA256.hexdigest("#{table}_#{column}_fk").first(10)}"
+ end
+
# Long-running migrations may take more than the timeout allowed by
# the database. Disable the session's statement timeout to ensure
# migrations don't get killed prematurely. (PostgreSQL only)