summaryrefslogtreecommitdiff
path: root/db/post_migrate/20181014121030_enqueue_redact_links.rb
blob: 1ee4703c88a30ec317cfa9bfd51525c8a1c515d3 (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
57
58
59
60
61
62
63
64
65
# frozen_string_literal: true

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

  DOWNTIME = false
  BATCH_SIZE = 1000
  DELAY_INTERVAL = 5.minutes.to_i
  MIGRATION = 'RedactLinks'

  disable_ddl_transaction!

  class Note < ActiveRecord::Base
    include EachBatch

    self.table_name = 'notes'
    self.inheritance_column = :_type_disabled
  end

  class Issue < ActiveRecord::Base
    include EachBatch

    self.table_name = 'issues'
    self.inheritance_column = :_type_disabled
  end

  class MergeRequest < ActiveRecord::Base
    include EachBatch

    self.table_name = 'merge_requests'
    self.inheritance_column = :_type_disabled
  end

  class Snippet < ActiveRecord::Base
    include EachBatch

    self.table_name = 'snippets'
    self.inheritance_column = :_type_disabled
  end

  def up
    disable_statement_timeout do
      schedule_migration(Note, 'note')
      schedule_migration(Issue, 'description')
      schedule_migration(MergeRequest, 'description')
      schedule_migration(Snippet, 'description')
    end
  end

  def down
    # nothing to do
  end

  private

  def schedule_migration(model, field)
    link_pattern = "%/sent_notifications/" + ("_" * 32) + "/unsubscribe%"

    model.where("#{field} like ?", link_pattern).each_batch(of: BATCH_SIZE) do |batch, index|
      start_id, stop_id = batch.pluck('MIN(id)', 'MAX(id)').first

      BackgroundMigrationWorker.perform_in(index * DELAY_INTERVAL, MIGRATION, [model.name.demodulize, field, start_id, stop_id])
    end
  end
end