summaryrefslogtreecommitdiff
path: root/db/migrate/20171106151218_issues_moved_to_id_foreign_key.rb
blob: 6395462384bbfdc84a68e58f03241f562d22bade (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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# See http://doc.gitlab.com/ce/development/migration_style_guide.html
# for more information on how to write migrations for GitLab.

class IssuesMovedToIdForeignKey < ActiveRecord::Migration
  include Gitlab::Database::MigrationHelpers

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

  disable_ddl_transaction!

  class Issue < ActiveRecord::Base
    include EachBatch

    self.table_name = 'issues'

    def self.with_orphaned_moved_to_issues
      if Gitlab::Database.postgresql?
        # Be careful to use a second table here for comparison otherwise we'll null
        # out all rows that don't have id == moved.to_id!
        where('NOT EXISTS (SELECT true FROM issues B WHERE issues.moved_to_id = B.id)')
          .where('moved_to_id IS NOT NULL')
      else
        # MySQL doesn't allow modification of the same table in a subquery,
        # and using a temporary table isn't automatically guaranteed to work
        # due to the MySQL query optimizer. See
        # https://dev.mysql.com/doc/refman/5.7/en/update.html for more
        # details.
        joins('LEFT JOIN issues AS b ON issues.moved_to_id = b.id')
          .where('issues.moved_to_id IS NOT NULL AND b.id IS NULL')
      end
    end
  end

  def up
    Issue.with_orphaned_moved_to_issues.each_batch(of: 100) do |batch|
      batch.update_all(moved_to_id: nil)
    end

    add_concurrent_foreign_key(
      :issues,
      :issues,
      column: :moved_to_id,
      on_delete: :nullify
    )

    # We're using a partial index here so we only index the data we actually
    # care about.
    add_concurrent_index(:issues, :moved_to_id, where: 'moved_to_id IS NOT NULL')
  end

  def down
    remove_foreign_key_without_error(:issues, column: :moved_to_id)
    remove_concurrent_index(:issues, :moved_to_id)
  end
end