20181030154446_add_missing_indexes_for_foreign_keys.rb 3.39 KB
Newer Older
1 2
# frozen_string_literal: true

3
class AddMissingIndexesForForeignKeys < ActiveRecord::Migration[4.2]
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
  include Gitlab::Database::MigrationHelpers

  DOWNTIME = false

  disable_ddl_transaction!

  def up
    add_concurrent_index(:application_settings, :usage_stats_set_by_user_id)
    add_concurrent_index(:ci_pipeline_schedules, :owner_id)
    add_concurrent_index(:ci_trigger_requests, :trigger_id)
    add_concurrent_index(:ci_triggers, :owner_id)
    add_concurrent_index(:clusters_applications_helm, :cluster_id, unique: true)
    add_concurrent_index(:clusters_applications_ingress, :cluster_id, unique: true)
    add_concurrent_index(:clusters_applications_jupyter, :cluster_id, unique: true)
    add_concurrent_index(:clusters_applications_jupyter, :oauth_application_id)
19
    add_concurrent_index(:clusters_applications_knative, :cluster_id, unique: true)
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
    add_concurrent_index(:clusters_applications_prometheus, :cluster_id, unique: true)
    add_concurrent_index(:fork_network_members, :forked_from_project_id)
    add_concurrent_index(:internal_ids, :namespace_id)
    add_concurrent_index(:internal_ids, :project_id)
    add_concurrent_index(:issues, :closed_by_id)
    add_concurrent_index(:label_priorities, :label_id)
    add_concurrent_index(:merge_request_metrics, :merged_by_id)
    add_concurrent_index(:merge_request_metrics, :latest_closed_by_id)
    add_concurrent_index(:oauth_openid_requests, :access_grant_id)
    add_concurrent_index(:project_deploy_tokens, :deploy_token_id)
    add_concurrent_index(:protected_tag_create_access_levels, :group_id)
    add_concurrent_index(:subscriptions, :project_id)
    add_concurrent_index(:user_statuses, :user_id)
    add_concurrent_index(:users, :accepted_term_id)
  end

  def down
Kamil Trzciński's avatar
Kamil Trzciński committed
37 38 39 40 41 42 43 44 45 46 47 48
    # MySQL requires index for FK,
    # thus removal of indexes does fail
    return if Gitlab::Database.mysql?

    remove_concurrent_index(:application_settings, :usage_stats_set_by_user_id)
    remove_concurrent_index(:ci_pipeline_schedules, :owner_id)
    remove_concurrent_index(:ci_trigger_requests, :trigger_id)
    remove_concurrent_index(:ci_triggers, :owner_id)
    remove_concurrent_index(:clusters_applications_helm, :cluster_id, unique: true)
    remove_concurrent_index(:clusters_applications_ingress, :cluster_id, unique: true)
    remove_concurrent_index(:clusters_applications_jupyter, :cluster_id, unique: true)
    remove_concurrent_index(:clusters_applications_jupyter, :oauth_application_id)
49
    remove_concurrent_index(:clusters_applications_knative, :cluster_id, unique: true)
Kamil Trzciński's avatar
Kamil Trzciński committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63
    remove_concurrent_index(:clusters_applications_prometheus, :cluster_id, unique: true)
    remove_concurrent_index(:fork_network_members, :forked_from_project_id)
    remove_concurrent_index(:internal_ids, :namespace_id)
    remove_concurrent_index(:internal_ids, :project_id)
    remove_concurrent_index(:issues, :closed_by_id)
    remove_concurrent_index(:label_priorities, :label_id)
    remove_concurrent_index(:merge_request_metrics, :merged_by_id)
    remove_concurrent_index(:merge_request_metrics, :latest_closed_by_id)
    remove_concurrent_index(:oauth_openid_requests, :access_grant_id)
    remove_concurrent_index(:project_deploy_tokens, :deploy_token_id)
    remove_concurrent_index(:protected_tag_create_access_levels, :group_id)
    remove_concurrent_index(:subscriptions, :project_id)
    remove_concurrent_index(:user_statuses, :user_id)
    remove_concurrent_index(:users, :accepted_term_id)
64 65
  end
end