summaryrefslogtreecommitdiff
path: root/lib/gitlab/background_migration/backfill_cluster_agents_has_vulnerabilities.rb
blob: 1dca82486acbcc5e7738866d1002bea05a047763 (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
# frozen_string_literal: true

module Gitlab
  module BackgroundMigration
    # Backfills the `vulnerability_reads.casted_cluster_agent_id` column
    class BackfillClusterAgentsHasVulnerabilities < Gitlab::BackgroundMigration::BatchedMigrationJob
      VULNERABILITY_READS_JOIN = <<~SQL
        INNER JOIN vulnerability_reads
        ON vulnerability_reads.casted_cluster_agent_id = cluster_agents.id AND
        vulnerability_reads.project_id = cluster_agents.project_id AND
        vulnerability_reads.report_type = 7
      SQL

      RELATION = ->(relation) do
        relation
          .where(has_vulnerabilities: false)
      end

      operation_name :update_all
      feature_category :database

      def perform
        each_sub_batch(batching_scope: RELATION) do |sub_batch|
          sub_batch
            .joins(VULNERABILITY_READS_JOIN)
            .update_all(has_vulnerabilities: true)
        end
      end
    end
  end
end