Commit b2531659 authored by Sean Arnold's avatar Sean Arnold

Merge branch '356215-sy-fix-missing-incident-status' into 'master'

Remove backfilled escalation statuses

See merge request gitlab-org/gitlab!83159
parents 5c3f5646 f91a6a0f
# frozen_string_literal: true
class RemoveAllIssuableEscalationStatuses < Gitlab::Database::Migration[1.0]
BATCH_SIZE = 5_000
disable_ddl_transaction!
# Removes records from previous backfill. Records for
# existing incidents will be created entirely as-needed.
#
# See db/post_migrate/20211214012507_backfill_incident_issue_escalation_statuses.rb,
# & IncidentManagement::IssuableEscalationStatuses::[BuildService,PrepareUpdateService]
def up
each_batch_range('incident_management_issuable_escalation_statuses', of: BATCH_SIZE) do |min, max|
execute <<~SQL
DELETE FROM incident_management_issuable_escalation_statuses
WHERE id BETWEEN #{min} AND #{max}
SQL
end
end
def down
# no-op
#
# Potential rollback/re-run should not have impact, as these
# records are not required to be present in the application.
# The corresponding feature flag is also disabled,
# preventing any user-facing access to the records.
end
end
# frozen_string_literal: true # frozen_string_literal: true
class BackfillIncidentIssueEscalationStatuses < Gitlab::Database::Migration[1.0] class BackfillIncidentIssueEscalationStatuses < Gitlab::Database::Migration[1.0]
MIGRATION = 'BackfillIncidentIssueEscalationStatuses' # Removed in favor of creating records for existing incidents
DELAY_INTERVAL = 2.minutes # as-needed. See db/migrate/20220321234317_remove_all_issuable_escalation_statuses.rb.
BATCH_SIZE = 20_000 def change
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 # no-op
end end
end end
ba5c1738b7c368ee8e10e390c959538c4d74055b8bc57f652b06ffe3a1c3becf
\ 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
...@@ -10,27 +10,10 @@ RSpec.describe BackfillIncidentIssueEscalationStatuses do ...@@ -10,27 +10,10 @@ RSpec.describe BackfillIncidentIssueEscalationStatuses do
let(:namespace) { namespaces.create!(name: 'foo', path: 'foo') } let(:namespace) { namespaces.create!(name: 'foo', path: 'foo') }
let(:project) { projects.create!(namespace_id: namespace.id) } let(:project) { projects.create!(namespace_id: namespace.id) }
before do # Backfill removed - see db/migrate/20220321234317_remove_all_issuable_escalation_statuses.rb.
stub_const("#{described_class.name}::BATCH_SIZE", 1) it 'does nothing' do
end issues.create!(project_id: project.id, issue_type: 1)
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( expect { migrate! }.not_to change { BackgroundMigrationWorker.jobs.size }
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
end end
# frozen_string_literal: true
require 'spec_helper'
require_migration!
RSpec.describe RemoveAllIssuableEscalationStatuses do
let(:namespaces) { table(:namespaces) }
let(:projects) { table(:projects) }
let(:issues) { table(:issues) }
let(:statuses) { table(:incident_management_issuable_escalation_statuses) }
let(:namespace) { namespaces.create!(name: 'foo', path: 'foo') }
let(:project) { projects.create!(namespace_id: namespace.id) }
it 'removes all escalation status records' do
issue = issues.create!(project_id: project.id, issue_type: 1)
statuses.create!(issue_id: issue.id)
expect { migrate! }.to change(statuses, :count).from(1).to(0)
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