summaryrefslogtreecommitdiff
path: root/db/post_migrate/20190313092516_clean_up_noteable_id_for_notes_on_commits.rb
blob: fcd63f42b0ef489bd9fdab99c8e9360420979bc8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# frozen_string_literal: true

# See http://doc.gitlab.com/ce/development/migration_style_guide.html
# for more information on how to write migrations for GitLab.

class CleanUpNoteableIdForNotesOnCommits < ActiveRecord::Migration[5.0]
  include Gitlab::Database::MigrationHelpers

  # Set this constant to true if this migration requires downtime.
  DOWNTIME = false

  TEMP_INDEX_NAME = 'index_notes_on_commit_with_null_noteable_id'

  disable_ddl_transaction!

  def up
    remove_concurrent_index_by_name(:notes, TEMP_INDEX_NAME)

    add_concurrent_index(:notes, :id, where: "noteable_type = 'Commit' AND noteable_id IS NOT NULL", name: TEMP_INDEX_NAME)

    # rubocop:disable Migration/UpdateLargeTable
    update_column_in_batches(:notes, :noteable_id, nil, batch_size: 300) do |table, query|
      query.where(
        table[:noteable_type].eq('Commit').and(table[:noteable_id].not_eq(nil))
      )
    end

    remove_concurrent_index_by_name(:notes, TEMP_INDEX_NAME)
  end

  def down
  end
end