# frozen_string_literal: true

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

  DOWNTIME = false
  BATCH_SIZE = 50
  DELAY_INTERVAL = 5.minutes
  MIGRATION_CLASS = 'PopulateVulnerabilityHistoricalStatistics'
  DAY_COUNT = 365

  disable_ddl_transaction!

  class ProjectSetting < ActiveRecord::Base
    include EachBatch

    self.table_name = 'project_settings'

    scope :has_vulnerabilities, -> { where('has_vulnerabilities IS TRUE') }
  end

  def up
    ProjectSetting.has_vulnerabilities.each_batch(of: BATCH_SIZE) do |batch, index|
      migrate_in(index * DELAY_INTERVAL, MIGRATION_CLASS, [batch.pluck(:project_id), DAY_COUNT])
    end
  end

  def down
    # no-op
  end
end
