Commit 8c281dcb authored by Douglas Barbosa Alexandre's avatar Douglas Barbosa Alexandre

Merge branch '330281-backfill-incident-escalation-statuses' into 'master'

Backfill Escalation Statuses for Incident issues

See merge request gitlab-org/gitlab!76714
parents cbe97f54 45d86dc1
# frozen_string_literal: true
class BackfillIncidentIssueEscalationStatuses < Gitlab::Database::Migration[1.0]
MIGRATION = 'BackfillIncidentIssueEscalationStatuses'
DELAY_INTERVAL = 2.minutes
BATCH_SIZE = 20_000
disable_ddl_transaction!
class Issue < ActiveRecord::Base
include EachBatch
self.table_name = 'issues'
end
def up
relation = Issue.all
queue_background_migration_jobs_by_range_at_intervals(
relation, MIGRATION, DELAY_INTERVAL, batch_size: BATCH_SIZE, track_jobs: true)
end
def down
# no-op
end
end
a7aa1ffccce785d365720309e3773f167075a9d06805eea941e6cd47bc918471
\ No newline at end of file
# frozen_string_literal: true
module Gitlab
module BackgroundMigration
# BackfillIncidentIssueEscalationStatuses adds
# IncidentManagement::IssuableEscalationStatus records for existing Incident issues.
# They will be added with no policy, and escalations_started_at as nil.
class BackfillIncidentIssueEscalationStatuses
def perform(start_id, stop_id)
ActiveRecord::Base.connection.execute <<~SQL
INSERT INTO incident_management_issuable_escalation_statuses (issue_id, created_at, updated_at)
SELECT issues.id, current_timestamp, current_timestamp
FROM issues
WHERE issues.issue_type = 1
AND issues.id BETWEEN #{start_id} AND #{stop_id}
ON CONFLICT (issue_id) DO NOTHING;
SQL
mark_job_as_succeeded(start_id, stop_id)
end
private
def mark_job_as_succeeded(*arguments)
::Gitlab::Database::BackgroundMigrationJob.mark_all_as_succeeded(
self.class.name.demodulize,
arguments
)
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::BackgroundMigration::BackfillIncidentIssueEscalationStatuses, schema: 20211214012507 do
let(:namespaces) { table(:namespaces) }
let(:projects) { table(:projects) }
let(:issues) { table(:issues) }
let(:issuable_escalation_statuses) { table(:incident_management_issuable_escalation_statuses) }
subject(:migration) { described_class.new }
it 'correctly backfills issuable escalation status records' do
namespace = namespaces.create!(name: 'foo', path: 'foo')
project = projects.create!(namespace_id: namespace.id)
issues.create!(project_id: project.id, title: 'issue 1', issue_type: 0) # non-incident issue
issues.create!(project_id: project.id, title: 'incident 1', issue_type: 1)
issues.create!(project_id: project.id, title: 'incident 2', issue_type: 1)
incident_issue_existing_status = issues.create!(project_id: project.id, title: 'incident 3', issue_type: 1)
issuable_escalation_statuses.create!(issue_id: incident_issue_existing_status.id)
migration.perform(1, incident_issue_existing_status.id)
expect(issuable_escalation_statuses.count).to eq(3)
end
end
# frozen_string_literal: true
require 'spec_helper'
require_migration!
RSpec.describe BackfillIncidentIssueEscalationStatuses do
let(:namespaces) { table(:namespaces) }
let(:projects) { table(:projects) }
let(:issues) { table(:issues) }
let(:namespace) { namespaces.create!(name: 'foo', path: 'foo') }
let(:project) { projects.create!(namespace_id: namespace.id) }
before do
stub_const("#{described_class.name}::BATCH_SIZE", 1)
end
it 'schedules jobs for incident issues' do
issue_1 = issues.create!(project_id: project.id) # non-incident issue
incident_1 = issues.create!(project_id: project.id, issue_type: 1)
incident_2 = issues.create!(project_id: project.id, issue_type: 1)
Sidekiq::Testing.fake! do
freeze_time do
migrate!
expect(described_class::MIGRATION).to be_scheduled_delayed_migration(
2.minutes, issue_1.id, issue_1.id)
expect(described_class::MIGRATION).to be_scheduled_delayed_migration(
4.minutes, incident_1.id, incident_1.id)
expect(described_class::MIGRATION).to be_scheduled_delayed_migration(
6.minutes, incident_2.id, incident_2.id)
expect(BackgroundMigrationWorker.jobs.size).to eq(3)
end
end
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