summaryrefslogtreecommitdiff
path: root/db/post_migrate/20221128120634_schedule_fixing_security_scan_statuses.rb
blob: 1cf4a33e09fda6702c1babe76a10270e6bad6f4e (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
# frozen_string_literal: true

class ScheduleFixingSecurityScanStatuses < Gitlab::Database::Migration[2.0]
  MIGRATION = 'FixSecurityScanStatuses'
  TABLE_NAME = :security_scans
  BATCH_COLUMN = :id
  DELAY_INTERVAL = 2.minutes
  BATCH_SIZE = 10_000
  MAX_BATCH_SIZE = 50_000
  SUB_BATCH_SIZE = 100

  disable_ddl_transaction!

  restrict_gitlab_migration gitlab_schema: :gitlab_main

  class SecurityScan < MigrationRecord
    def self.start_migration_from
      sort_order = Arel::Nodes::SqlLiteral.new("date(timezone('UTC'::text, created_at)) ASC, id ASC")

      where("date(timezone('UTC'::text, created_at)) > ?", 90.days.ago).order(sort_order).first&.id
    end
  end

  def up
    # Only the SaaS application is affected
    return unless Gitlab.dev_or_test_env? || Gitlab.com?

    batch_min_value = SecurityScan.start_migration_from

    return unless batch_min_value # It is possible that some users don't have corrupted records

    queue_batched_background_migration(
      MIGRATION,
      TABLE_NAME,
      BATCH_COLUMN,
      job_interval: DELAY_INTERVAL,
      batch_size: BATCH_SIZE,
      max_batch_size: MAX_BATCH_SIZE,
      sub_batch_size: SUB_BATCH_SIZE,
      batch_min_value: batch_min_value
    )
  end

  def down
    delete_batched_background_migration(
      MIGRATION,
      TABLE_NAME,
      BATCH_COLUMN,
      []
    )
  end
end