summaryrefslogtreecommitdiff
path: root/db/post_migrate/20200518114540_schedule_fix_ruby_object_in_audit_events.rb
blob: e4335089540879acbadce279896e4152edd1b35b (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
# frozen_string_literal: true

class ScheduleFixRubyObjectInAuditEvents < ActiveRecord::Migration[6.0]
  include Gitlab::Database::MigrationHelpers

  DOWNTIME = false
  INDEX_NAME = 'index_audit_events_on_ruby_object_in_details'
  INTERVAL = 2.minutes.to_i
  BATCH_SIZE = 1_000
  MIGRATION = 'FixRubyObjectInAuditEvents'

  disable_ddl_transaction!

  class AuditEvent < ActiveRecord::Base
    self.table_name = 'audit_events'

    include ::EachBatch
  end

  def up
    return unless Gitlab.ee?

    # create temporary index for audit_events with ruby/object in details field, may take well over 1h
    add_concurrent_index(:audit_events, :id, where: "details ~~ '%ruby/object%'", name: INDEX_NAME)

    relation = AuditEvent.where("details ~~ '%ruby/object%'")

    queue_background_migration_jobs_by_range_at_intervals(
      relation,
      MIGRATION,
      INTERVAL,
      batch_size: BATCH_SIZE
    )
  end

  def down
    # temporary index is to be dropped in a different migration in an upcoming release
    # https://gitlab.com/gitlab-org/gitlab/issues/196842
    remove_concurrent_index_by_name(:audit_events, INDEX_NAME)
  end
end