summaryrefslogtreecommitdiff
path: root/lib/gitlab/background_migration/delete_orphaned_operational_vulnerabilities.rb
blob: 6953ae6565166d92d8b986f91e7a42aa04fde01d (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
# frozen_string_literal: true

module Gitlab
  module BackgroundMigration
    # Background migration for deleting orphaned operational vulnerabilities (without findings)
    class DeleteOrphanedOperationalVulnerabilities < ::Gitlab::BackgroundMigration::BatchedMigrationJob
      REPORT_TYPES = {
        cluster_image_scanning: 7,
        custom: 99
      }.freeze

      NOT_EXISTS_SQL = <<-SQL
        NOT EXISTS (
          SELECT FROM vulnerability_occurrences
          WHERE "vulnerability_occurrences"."vulnerability_id" = "vulnerabilities"."id"
        )
      SQL

      operation_name :delete_orphaned_operational_vulnerabilities
      feature_category :database

      scope_to ->(relation) do
        relation
          .where(report_type: [REPORT_TYPES[:cluster_image_scanning], REPORT_TYPES[:custom]])
      end

      def perform
        each_sub_batch do |sub_batch|
          sub_batch
            .where(NOT_EXISTS_SQL)
            .delete_all
        end
      end
    end
  end
end