diff options
author | Andreas Brandl <abrandl@gitlab.com> | 2018-03-21 13:36:28 +0100 |
---|---|---|
committer | Andreas Brandl <abrandl@gitlab.com> | 2018-03-21 13:41:24 +0100 |
commit | 1059b3ab27930c8bb430a2ba858fb33ababa5c9f (patch) | |
tree | ce7dda94498dd5093d64d374dcba5efa981b0fb8 /db/migrate/20180320182229_add_indexes_for_user_activity_queries.rb | |
parent | 5f8abe4783a252c5979d87572ceaeb37b11d59dc (diff) | |
download | gitlab-ce-1059b3ab27930c8bb430a2ba858fb33ababa5c9f.tar.gz |
Guard creation/removal with existence checks.
Diffstat (limited to 'db/migrate/20180320182229_add_indexes_for_user_activity_queries.rb')
-rw-r--r-- | db/migrate/20180320182229_add_indexes_for_user_activity_queries.rb | 22 |
1 files changed, 16 insertions, 6 deletions
diff --git a/db/migrate/20180320182229_add_indexes_for_user_activity_queries.rb b/db/migrate/20180320182229_add_indexes_for_user_activity_queries.rb index ba2dd7c8cf8..824bbb3ac05 100644 --- a/db/migrate/20180320182229_add_indexes_for_user_activity_queries.rb +++ b/db/migrate/20180320182229_add_indexes_for_user_activity_queries.rb @@ -6,25 +6,35 @@ class AddIndexesForUserActivityQueries < ActiveRecord::Migration disable_ddl_transaction! def up - add_concurrent_index :events, [:author_id, :project_id] - add_concurrent_index :user_interacted_projects, :user_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 unless index_exists?(:user_interacted_projects, :user_id) end 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 - 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 private def patch_foreign_keys + return yield if Gitlab::Database.postgresql? + # 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 + # 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 |