Commit 484353b6 authored by Sean Arnold's avatar Sean Arnold Committed by Mayra Cabrera

Add post migration to update incident issues

- Spec to test behaviour
parent 464be48e
---
title: Migrate all 'incident' labelled issues to have issue type 'incident'
merge_request: 37668
author:
type: added
# frozen_string_literal: true
class MigrateIncidentIssuesToIncidentType < ActiveRecord::Migration[6.0]
DOWNTIME = false
BATCH_SIZE = 100
disable_ddl_transaction!
LABEL_PROPERTIES = {
title: 'incident'
}.freeze
class Issue < ActiveRecord::Base
include EachBatch
self.table_name = 'issues'
scope :incident_labelled, -> do
joins("INNER JOIN label_links ON label_links.target_type = 'Issue' AND label_links.target_id = issues.id")
.joins("INNER JOIN labels ON labels.id = label_links.label_id")
.where(labels: LABEL_PROPERTIES)
end
enum issue_type: {
issue: 0,
incident: 1
}
scope :incident_typed, -> { where(issue_type: :incident) }
end
def up
incident_issues = Issue.incident_labelled
incident_issues.each_batch(of: BATCH_SIZE) do |batch|
batch.update_all(issue_type: :incident)
end
end
def down
incident_issues = Issue.incident_typed
incident_issues.each_batch(of: BATCH_SIZE) do |batch|
batch.update_all(issue_type: :issue)
end
end
end
085b3ad0f7da78a1e13f5cd3d2e7df297284a682c3bcd3883e487b18497430ff
\ No newline at end of file
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20200723040950_migrate_incident_issues_to_incident_type.rb')
RSpec.describe MigrateIncidentIssuesToIncidentType do
let(:migration) { described_class.new }
let(:projects) { table(:projects) }
let(:namespaces) { table(:namespaces) }
let(:labels) { table(:labels) }
let(:issues) { table(:issues) }
let(:label_links) { table(:label_links) }
let(:label_props) { IncidentManagement::CreateIncidentLabelService::LABEL_PROPERTIES }
let(:namespace) { namespaces.create!(name: 'foo', path: 'foo') }
let!(:project) { projects.create!(namespace_id: namespace.id) }
let(:label) { labels.create!(project_id: project.id, **label_props) }
let!(:incident_issue) { issues.create!(project_id: project.id) }
let!(:other_issue) { issues.create!(project_id: project.id) }
# Issue issue_type enum
let(:issue_type) { 0 }
let(:incident_type) { 1 }
before do
label_links.create!(target_id: incident_issue.id, label_id: label.id, target_type: 'Issue')
end
describe '#up' do
it 'updates the incident issue type' do
expect { migrate! }
.to change { incident_issue.reload.issue_type }
.from(issue_type)
.to(incident_type)
expect(other_issue.reload.issue_type).to eql(issue_type)
end
end
describe '#down' do
let!(:incident_issue) { issues.create!(project_id: project.id, issue_type: issue_type) }
it 'updates the incident issue type' do
migration.up
expect { migration.down }
.to change { incident_issue.reload.issue_type }
.from(incident_type)
.to(issue_type)
expect(other_issue.reload.issue_type).to eql(issue_type)
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