# frozen_string_literal: true

class MigrateVulnerabilityDismissalFeedback < ActiveRecord::Migration[6.0]
  include Gitlab::Database::MigrationHelpers

  DOWNTIME = false
  disable_ddl_transaction!

  MIGRATION = 'UpdateVulnerabilitiesFromDismissalFeedback'
  BATCH_SIZE = 500
  DELAY_INTERVAL = 2.minutes.to_i

  class Vulnerability < ActiveRecord::Base
    self.table_name = 'vulnerabilities'
    self.inheritance_column = :_type_disabled

    include ::EachBatch
  end

  def up
    return unless Gitlab.ee?

    Vulnerability.select('project_id').group(:project_id).each_batch(of: BATCH_SIZE, column: "project_id") do |project_batch, index|
      batch_delay = (index - 1) * BATCH_SIZE * DELAY_INTERVAL

      project_batch.each_with_index do |project, project_batch_index|
        project_delay = project_batch_index * DELAY_INTERVAL
        migrate_in(batch_delay + project_delay, MIGRATION, project[:project_id])
      end
    end
  end

  def down
    # nothing to do
  end
end
