Skip to content
代码片段 群组 项目
提交 d6ddf08c 编辑于 作者: Pavel Shutsin's avatar Pavel Shutsin
浏览文件

Add danger check for required migration stop

Add a check to notify developers for possible
implication of finalizing heavy data migration
上级 242ceb6c
No related branches found
No related tags found
无相关合并请求
# frozen_string_literal: true
require_relative '../../tooling/danger/required_stops'
module Danger
class RequiredStops < ::Danger::Plugin
# Put the helper code somewhere it can be tested
include Tooling::Danger::RequiredStops
end
end
# frozen_string_literal: true
required_stops.add_comment_for_finalized_migrations
# frozen_string_literal: true
require 'gitlab-dangerfiles'
require 'danger'
require 'danger/plugins/internal/helper'
require 'gitlab/dangerfiles/spec_helper'
require_relative '../../../tooling/danger/required_stops'
require_relative '../../../tooling/danger/project_helper'
RSpec.describe Tooling::Danger::RequiredStops, feature_category: :tooling do
include_context "with dangerfile"
let(:fake_danger) { DangerSpecHelper.fake_danger.include(described_class) }
let(:fake_project_helper) { instance_double(Tooling::Danger::ProjectHelper) }
let(:warning_comment) { described_class::WARNING_COMMENT.chomp }
subject(:required_stops) { fake_danger.new(helper: fake_helper) }
before do
allow(required_stops).to receive(:project_helper).and_return(fake_project_helper)
end
describe '#add_comment_for_finalized_migrations' do
let(:file_lines) { file_diff.map { |line| line.delete_prefix('+').delete_prefix('-') } }
before do
allow(required_stops.project_helper).to receive(:file_lines).and_return(file_lines)
allow(required_stops.helper).to receive(:all_changed_files).and_return([filename])
allow(required_stops.helper).to receive(:changed_lines).with(filename).and_return(file_diff)
end
shared_examples "adds comment to added migration finalizations" do
context 'when model has a newly added migration finalization' do
let(:file_diff) do
[
"+ def up",
"+ finalize_background_migration(MIGRATION)",
"+ end",
"+ def up",
"+ finalize_background_migration('MyMigration')",
"+ end",
"+ def up",
"+ ensure_batched_background_migration_is_finished(",
"+ end",
"+ def up",
"+ ensure_batched_background_migration_is_finished('MyMigration')",
"+ end",
"+ def up",
"+ finalize_batched_background_migration(",
"+ end",
"+ def up",
"+ finalize_batched_background_migration('MyMigration')",
"+ end"
]
end
it 'adds comment at the correct line' do
matching_line_numbers = [2, 5, 8, 11, 14, 17]
matching_line_numbers.each do |line_number|
expect(required_stops).to receive(:markdown).with("\n#{warning_comment}", file: filename, line: line_number)
end
required_stops.add_comment_for_finalized_migrations
end
end
context 'when model does not have migration finalization statement' do
let(:file_diff) do
[
"+ queue_batched_background_migration(",
"- ensure_batched_background_migration_is_finished("
]
end
it 'does not add comment' do
expect(required_stops).not_to receive(:markdown)
required_stops.add_comment_for_finalized_migrations
end
end
end
context 'when model has a newly added migration finalization' do
context 'with regular migration' do
let(:filename) { 'db/migrate/my_migration.rb' }
include_examples 'adds comment to added migration finalizations'
end
context 'with post migration' do
let(:filename) { 'db/post_migrate/my_migration.rb' }
include_examples 'adds comment to added migration finalizations'
end
end
end
end
# frozen_string_literal: true
require_relative 'suggestor'
module Tooling
module Danger
module RequiredStops
include ::Tooling::Danger::Suggestor
MIGRATION_FILES_REGEX = %r{^db/(post_)?migrate}
MIGRATION_FINALIZE_METHODS = %w[finalize_background_migration ensure_batched_background_migration_is_finished
finalize_batched_background_migration].freeze
MIGRATION_FINALIZE_REGEX = /^\+\s*(.*\.)?(#{MIGRATION_FINALIZE_METHODS.join('|')})[( ]/
DOC_URL = "https://docs.gitlab.com/ee/development/database/required_stops.html"
WARNING_COMMENT = <<~COMMENT.freeze
Finalizing data migration might be time consuming and require a [required stop](#{DOC_URL}).
Check the timings of the underlying data migration.
If possible postpone the finalization to the scheduled required stop.
If postponing is impossible please create new required stop as documentation suggests.
COMMENT
def add_comment_for_finalized_migrations
migration_files.each do |filename|
add_suggestion(
filename: filename,
regex: MIGRATION_FINALIZE_REGEX,
comment_text: WARNING_COMMENT
)
end
end
private
def migration_files
helper.all_changed_files.grep(MIGRATION_FILES_REGEX)
end
end
end
end
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册