gitlab-org--gitlab-foss/lib/gitlab/background_migration/backfill_vulnerability_read...

31 lines
923 B
Ruby

# frozen_string_literal: true
module Gitlab
module BackgroundMigration
# Backfills the `vulnerability_reads.casted_cluster_agent_id` column
class BackfillVulnerabilityReadsClusterAgent < Gitlab::BackgroundMigration::BatchedMigrationJob
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
RELATION = ->(relation) do
relation
.where(report_type: 7)
end
def perform
each_sub_batch(
operation_name: :update_all,
batching_scope: RELATION
) 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