2018-11-13 07:27:31 +00:00
|
|
|
class AddIndexesForUserActivityQueries < ActiveRecord::Migration[4.2]
|
2018-03-20 18:24:40 +00:00
|
|
|
include Gitlab::Database::MigrationHelpers
|
|
|
|
|
|
|
|
DOWNTIME = false
|
|
|
|
|
|
|
|
disable_ddl_transaction!
|
|
|
|
|
|
|
|
def up
|
2018-03-21 12:36:28 +00:00
|
|
|
add_concurrent_index :events, [:author_id, :project_id] unless index_exists?(:events, [:author_id, :project_id])
|
|
|
|
add_concurrent_index :user_interacted_projects, :user_id unless index_exists?(:user_interacted_projects, :user_id)
|
2018-03-20 18:24:40 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def down
|
2018-03-21 12:36:28 +00:00
|
|
|
remove_concurrent_index :events, [:author_id, :project_id] if index_exists?(:events, [:author_id, :project_id])
|
2018-03-20 20:02:04 +00:00
|
|
|
|
|
|
|
patch_foreign_keys do
|
2018-03-21 12:36:28 +00:00
|
|
|
remove_concurrent_index :user_interacted_projects, :user_id if index_exists?(:user_interacted_projects, :user_id)
|
2018-03-20 20:02:04 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def patch_foreign_keys
|
2018-03-21 12:36:28 +00:00
|
|
|
return yield if Gitlab::Database.postgresql?
|
|
|
|
|
2018-03-20 20:02:04 +00:00
|
|
|
# MySQL doesn't like to remove the index with a foreign key using it.
|
2018-03-21 12:36:28 +00:00
|
|
|
remove_foreign_key :user_interacted_projects, :users if fk_exists?(:user_interacted_projects, :user_id)
|
|
|
|
|
2018-03-20 20:02:04 +00:00
|
|
|
yield
|
2018-03-21 12:36:28 +00:00
|
|
|
|
2018-03-20 20:02:04 +00:00
|
|
|
# Let's re-add the foreign key using the existing index on (user_id, project_id)
|
2018-03-21 12:36:28 +00:00
|
|
|
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
|
2018-03-20 18:24:40 +00:00
|
|
|
end
|
|
|
|
end
|