Skip to content
Snippets Groups Projects
Commit 43c01547 authored by Stan Hu's avatar Stan Hu
Browse files

Merge branch 'project-foreign-keys-without-errors' into 'master'

Change project FK migration to skip existing FKs

Closes #35243

See merge request !13235
parents 88958e5a 0693905d
No related branches found
No related tags found
No related merge requests found
---
title: Change project FK migration to skip existing FKs
merge_request:
author:
Loading
@@ -62,8 +62,8 @@ class ProjectForeignKeysWithCascadingDeletes < ActiveRecord::Migration
Loading
@@ -62,8 +62,8 @@ class ProjectForeignKeysWithCascadingDeletes < ActiveRecord::Migration
   
# These columns are not indexed yet, meaning a cascading delete would take # These columns are not indexed yet, meaning a cascading delete would take
# forever. # forever.
add_concurrent_index(:project_group_links, :project_id) add_index_if_not_exists(:project_group_links, :project_id)
add_concurrent_index(:pages_domains, :project_id) add_index_if_not_exists(:pages_domains, :project_id)
end end
   
def down def down
Loading
@@ -71,15 +71,15 @@ class ProjectForeignKeysWithCascadingDeletes < ActiveRecord::Migration
Loading
@@ -71,15 +71,15 @@ class ProjectForeignKeysWithCascadingDeletes < ActiveRecord::Migration
remove_foreign_key_without_error(source, column) remove_foreign_key_without_error(source, column)
end end
   
add_concurrent_foreign_key(:boards, :projects, column: :project_id) add_foreign_key_if_not_exists(:boards, :projects, column: :project_id)
add_concurrent_foreign_key(:lists, :labels, column: :label_id) add_foreign_key_if_not_exists(:lists, :labels, column: :label_id)
add_concurrent_foreign_key(:lists, :boards, column: :board_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, :protected_branches,
column: :protected_branch_id) 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, :protected_branches,
column: :protected_branch_id) column: :protected_branch_id)
   
Loading
@@ -89,7 +89,7 @@ class ProjectForeignKeysWithCascadingDeletes < ActiveRecord::Migration
Loading
@@ -89,7 +89,7 @@ class ProjectForeignKeysWithCascadingDeletes < ActiveRecord::Migration
   
def add_foreign_keys def add_foreign_keys
TABLES.each do |(source, target, column)| 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
end end
   
Loading
@@ -153,6 +153,18 @@ class ProjectForeignKeysWithCascadingDeletes < ActiveRecord::Migration
Loading
@@ -153,6 +153,18 @@ class ProjectForeignKeysWithCascadingDeletes < ActiveRecord::Migration
EOF EOF
end 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) def remove_foreign_key_without_error(table, column)
remove_foreign_key(table, column: column) remove_foreign_key(table, column: column)
rescue ArgumentError rescue ArgumentError
Loading
@@ -163,6 +175,12 @@ class ProjectForeignKeysWithCascadingDeletes < ActiveRecord::Migration
Loading
@@ -163,6 +175,12 @@ class ProjectForeignKeysWithCascadingDeletes < ActiveRecord::Migration
rescue ArgumentError rescue ArgumentError
end end
   
def foreign_key_exists?(table, column)
foreign_keys(table).any? do |key|
key.options[:column] == column.to_s
end
end
def connection def connection
# Rails memoizes connection objects, but this causes them to be shared # Rails memoizes connection objects, but this causes them to be shared
# amongst threads; we don't want that. # amongst threads; we don't want that.
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment