Guard creation/removal with existence checks.
This commit is contained in:
parent
5f8abe4783
commit
1059b3ab27
1 changed files with 16 additions and 6 deletions
|
@ -6,25 +6,35 @@ class AddIndexesForUserActivityQueries < ActiveRecord::Migration
|
||||||
disable_ddl_transaction!
|
disable_ddl_transaction!
|
||||||
|
|
||||||
def up
|
def up
|
||||||
add_concurrent_index :events, [:author_id, :project_id]
|
add_concurrent_index :events, [:author_id, :project_id] unless index_exists?(:events, [:author_id, :project_id])
|
||||||
add_concurrent_index :user_interacted_projects, :user_id
|
add_concurrent_index :user_interacted_projects, :user_id unless index_exists?(:user_interacted_projects, :user_id)
|
||||||
end
|
end
|
||||||
|
|
||||||
def down
|
def down
|
||||||
remove_concurrent_index :events, [:author_id, :project_id]
|
remove_concurrent_index :events, [:author_id, :project_id] if index_exists?(:events, [:author_id, :project_id])
|
||||||
|
|
||||||
patch_foreign_keys do
|
patch_foreign_keys do
|
||||||
remove_concurrent_index :user_interacted_projects, :user_id
|
remove_concurrent_index :user_interacted_projects, :user_id if index_exists?(:user_interacted_projects, :user_id)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def patch_foreign_keys
|
def patch_foreign_keys
|
||||||
|
return yield if Gitlab::Database.postgresql?
|
||||||
|
|
||||||
# MySQL doesn't like to remove the index with a foreign key using it.
|
# MySQL doesn't like to remove the index with a foreign key using it.
|
||||||
remove_foreign_key :user_interacted_projects, :users unless Gitlab::Database.postgresql?
|
remove_foreign_key :user_interacted_projects, :users if fk_exists?(:user_interacted_projects, :user_id)
|
||||||
|
|
||||||
yield
|
yield
|
||||||
|
|
||||||
# Let's re-add the foreign key using the existing index on (user_id, project_id)
|
# Let's re-add the foreign key using the existing index on (user_id, project_id)
|
||||||
add_concurrent_foreign_key :user_interacted_projects, :users, column: :user_id unless Gitlab::Database.postgresql?
|
add_concurrent_foreign_key :user_interacted_projects, :users, column: :user_id unless fk_exists?(:user_interacted_projects, :user_id)
|
||||||
|
end
|
||||||
|
|
||||||
|
def fk_exists?(table, column)
|
||||||
|
foreign_keys(table).any? do |key|
|
||||||
|
key.options[:column] == column.to_s
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue