Commit 4e9419ee authored by Dylan Griffith's avatar Dylan Griffith

Merge branch '244380_repopulate_historical_vulnerability_statistics' into 'master'

Create migration to schedule re-population of historical stats

See merge request gitlab-org/gitlab!48128
parents d607ea70 93cd7059
---
title: Repopulate historical vulnerability statistics
merge_request: 48128
author:
type: fixed
# 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
9ff8ddefff1df81f1eac2ccfc6f3019bb77a6129280e799c0abe54f51e09277a
\ No newline at end of file
# frozen_string_literal: true
require 'spec_helper'
require_migration!
RSpec.describe ScheduleRepopulateHistoricalVulnerabilityStatistics do
let(:namespaces) { table(:namespaces) }
let(:projects) { table(:projects) }
let(:project_settings) { table(:project_settings) }
let(:namespace) { namespaces.create!(name: 'gitlab', path: 'gitlab-org') }
let!(:project_1) { projects.create!(namespace_id: namespace.id, name: 'foo_1') }
let!(:project_2) { projects.create!(namespace_id: namespace.id, name: 'foo_2') }
let!(:project_3) { projects.create!(namespace_id: namespace.id, name: 'foo_3') }
let!(:project_4) { projects.create!(namespace_id: namespace.id, name: 'foo_4') }
around do |example|
freeze_time { Sidekiq::Testing.fake! { example.run } }
end
before do
stub_const("#{described_class.name}::BATCH_SIZE", 1)
project_settings.create!(project_id: project_1.id, has_vulnerabilities: true)
project_settings.create!(project_id: project_2.id, has_vulnerabilities: false)
project_settings.create!(project_id: project_4.id, has_vulnerabilities: true)
end
it 'schedules the background jobs', :aggregate_failures do
migrate!
expect(BackgroundMigrationWorker.jobs.size).to be(2)
expect(described_class::MIGRATION_CLASS).to be_scheduled_delayed_migration(described_class::DELAY_INTERVAL, [project_1.id], described_class::DAY_COUNT)
expect(described_class::MIGRATION_CLASS).to be_scheduled_delayed_migration(2 * described_class::DELAY_INTERVAL, [project_4.id], described_class::DAY_COUNT)
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment