diff options
author | Stan Hu <stanhu@gmail.com> | 2017-08-03 03:33:55 +0300 |
---|---|---|
committer | Stan Hu <stanhu@gmail.com> | 2017-08-03 03:33:55 +0300 |
commit | 43c015472b5f85f7f345c825687a562f4cb3a13d (patch) | |
tree | e72920cdd8d6cc333f1c78ef3d42e4dcff654c8e | |
parent | 88958e5a9cd364ae36f3d2837982cedb9239c3bc (diff) | |
parent | 0693905d2e586071b186be11ce9e31ab13cf3ae7 (diff) |
Merge branch 'project-foreign-keys-without-errors' into 'master'
Change project FK migration to skip existing FKs
Closes #35243
See merge request !13235
-rw-r--r-- | changelogs/unreleased/project-foreign-keys-without-errors.yml | 4 | ||||
-rw-r--r-- | db/migrate/20170530130129_project_foreign_keys_with_cascading_deletes.rb | 34 |
2 files changed, 30 insertions, 8 deletions
diff --git a/changelogs/unreleased/project-foreign-keys-without-errors.yml b/changelogs/unreleased/project-foreign-keys-without-errors.yml new file mode 100644 index 00000000000..63c53c8ad8f --- /dev/null +++ b/changelogs/unreleased/project-foreign-keys-without-errors.yml @@ -0,0 +1,4 @@ +--- +title: Change project FK migration to skip existing FKs +merge_request: +author: diff --git a/db/migrate/20170530130129_project_foreign_keys_with_cascading_deletes.rb b/db/migrate/20170530130129_project_foreign_keys_with_cascading_deletes.rb index 3eaafac321d..af6d10b5158 100644 --- a/db/migrate/20170530130129_project_foreign_keys_with_cascading_deletes.rb +++ b/db/migrate/20170530130129_project_foreign_keys_with_cascading_deletes.rb @@ -62,8 +62,8 @@ class ProjectForeignKeysWithCascadingDeletes < ActiveRecord::Migration # These columns are not indexed yet, meaning a cascading delete would take # forever. - add_concurrent_index(:project_group_links, :project_id) - add_concurrent_index(:pages_domains, :project_id) + add_index_if_not_exists(:project_group_links, :project_id) + add_index_if_not_exists(:pages_domains, :project_id) end def down @@ -71,15 +71,15 @@ class ProjectForeignKeysWithCascadingDeletes < ActiveRecord::Migration remove_foreign_key_without_error(source, column) end - add_concurrent_foreign_key(:boards, :projects, column: :project_id) - add_concurrent_foreign_key(:lists, :labels, column: :label_id) - add_concurrent_foreign_key(:lists, :boards, column: :board_id) + add_foreign_key_if_not_exists(:boards, :projects, column: :project_id) + add_foreign_key_if_not_exists(:lists, :labels, column: :label_id) + add_foreign_key_if_not_exists(:lists, :boards, column: :board_id) - add_concurrent_foreign_key(:protected_branch_merge_access_levels, + add_foreign_key_if_not_exists(:protected_branch_merge_access_levels, :protected_branches, column: :protected_branch_id) - add_concurrent_foreign_key(:protected_branch_push_access_levels, + add_foreign_key_if_not_exists(:protected_branch_push_access_levels, :protected_branches, column: :protected_branch_id) @@ -89,7 +89,7 @@ class ProjectForeignKeysWithCascadingDeletes < ActiveRecord::Migration def add_foreign_keys TABLES.each do |(source, target, column)| - add_concurrent_foreign_key(source, target, column: column) + add_foreign_key_if_not_exists(source, target, column: column) end end @@ -153,6 +153,18 @@ class ProjectForeignKeysWithCascadingDeletes < ActiveRecord::Migration EOF end + def add_foreign_key_if_not_exists(source, target, column:) + return if foreign_key_exists?(source, column) + + add_concurrent_foreign_key(source, target, column: column) + end + + def add_index_if_not_exists(table, column) + return if index_exists?(table, column) + + add_concurrent_index(table, column) + end + def remove_foreign_key_without_error(table, column) remove_foreign_key(table, column: column) rescue ArgumentError @@ -163,6 +175,12 @@ class ProjectForeignKeysWithCascadingDeletes < ActiveRecord::Migration rescue ArgumentError end + def foreign_key_exists?(table, column) + foreign_keys(table).any? do |key| + key.options[:column] == column.to_s + end + end + def connection # Rails memoizes connection objects, but this causes them to be shared # amongst threads; we don't want that. |