summaryrefslogtreecommitdiff
path: root/lib/gitlab/background_migration/backfill_vulnerability_reads_cluster_agent.rb
blob: 37b1a37569bb7fb90dba6b4ad1431c4820ce4626 (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
# frozen_string_literal: true

module Gitlab
  module BackgroundMigration
    # Backfills the `vulnerability_reads.casted_cluster_agent_id` column
    class BackfillVulnerabilityReadsClusterAgent < Gitlab::BackgroundMigration::BatchedMigrationJob
      operation_name :update_all

      CLUSTER_AGENTS_JOIN = <<~SQL
        INNER JOIN cluster_agents
        ON CAST(vulnerability_reads.cluster_agent_id AS bigint) = cluster_agents.id AND
        vulnerability_reads.project_id = cluster_agents.project_id
      SQL

      CLUSTER_IMAGE_SCANNING_REPORT_TYPE = 7

      scope_to ->(relation) { relation.where(report_type: CLUSTER_IMAGE_SCANNING_REPORT_TYPE) }

      def perform
        each_sub_batch do |sub_batch|
          sub_batch
            .joins(CLUSTER_AGENTS_JOIN)
            .update_all('casted_cluster_agent_id = CAST(vulnerability_reads.cluster_agent_id AS bigint)')
        end
      end
    end
  end
end