summaryrefslogtreecommitdiff
path: root/lib/gitlab/background_migration/backfill_cluster_agents_has_vulnerabilities.rb
blob: 2ee0594d0a68bebdc89d2c46fe6f83264a7fdb02 (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

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