summaryrefslogtreecommitdiff
path: root/db/post_migrate/20180906051323_remove_orphaned_label_links.rb
blob: a474aaf534cf2184a4ee2889b5bf5d23ff5da520 (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
# frozen_string_literal: true

class RemoveOrphanedLabelLinks < ActiveRecord::Migration[4.2]
  include Gitlab::Database::MigrationHelpers

  DOWNTIME = false

  disable_ddl_transaction!

  class LabelLinks < ActiveRecord::Base
    self.table_name = 'label_links'
    include EachBatch

    def self.orphaned
      where('NOT EXISTS ( SELECT 1 FROM labels WHERE labels.id = label_links.label_id )')
    end
  end

  def up
    # Some of these queries can take up to 10 seconds to run on GitLab.com,
    # which is pretty close to our 15 second statement timeout. To ensure a
    # smooth deployment procedure we disable the statement timeouts for this
    # migration, just in case.
    disable_statement_timeout do
      # On GitLab.com there are over 2,000,000 orphaned label links. On
      # staging, removing 100,000 rows generated a max replication lag of 6.7
      # MB. In total, removing all these rows will only generate about 136 MB
      # of data, so it should be safe to do this.
      LabelLinks.orphaned.each_batch(of: 100_000) do |batch|
        batch.delete_all
      end
    end

    add_concurrent_foreign_key(:label_links, :labels, column: :label_id, on_delete: :cascade)
  end

  def down
    # There is no way to restore orphaned label links.
    if foreign_key_exists?(:label_links, column: :label_id)
      remove_foreign_key(:label_links, column: :label_id)
    end
  end
end