diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-10-23 21:11:07 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-10-23 21:11:07 +0300 |
commit | c5da163db1c10676b1a01a898b7b3a4506e65b89 (patch) | |
tree | 0110bf1518c8049cddab8d62e77d4f7dad2478a7 /spec/tooling | |
parent | 750a63ac4cc7acbecff3b8d22232cb1c15af34fb (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/tooling')
-rw-r--r-- | spec/tooling/danger/change_column_default_spec.rb | 104 | ||||
-rw-r--r-- | spec/tooling/fixtures/change_column_default_migration.txt | 13 |
2 files changed, 117 insertions, 0 deletions
diff --git a/spec/tooling/danger/change_column_default_spec.rb b/spec/tooling/danger/change_column_default_spec.rb new file mode 100644 index 00000000000..8cfcbfa1dc0 --- /dev/null +++ b/spec/tooling/danger/change_column_default_spec.rb @@ -0,0 +1,104 @@ +# frozen_string_literal: true + +require 'danger' +require 'gitlab/dangerfiles/spec_helper' + +require_relative '../../../tooling/danger/change_column_default' +require_relative '../../../tooling/danger/project_helper' + +RSpec.describe Tooling::Danger::ChangeColumnDefault, feature_category: :tooling do + subject(:change_column_default) { fake_danger.new(helper: fake_helper) } + + let(:fake_danger) { DangerSpecHelper.fake_danger.include(described_class) } + let(:fake_project_helper) { instance_double(Tooling::Danger::ProjectHelper) } + let(:comment) { described_class::COMMENT.chomp } + let(:file_diff) do + File.read(File.expand_path("../fixtures/change_column_default_migration.txt", __dir__)).split("\n") + end + + include_context "with dangerfile" + + describe '#add_comment_for_change_column_default' do + let(:file_lines) { file_diff.map { |line| line.delete_prefix('+').delete_prefix('-') } } + let(:matching_lines) { [7, 9, 11] } + + before do + allow(change_column_default).to receive(:project_helper).and_return(fake_project_helper) + allow(change_column_default.project_helper).to receive(:file_lines).and_return(file_lines) + allow(change_column_default.helper).to receive(:all_changed_files).and_return([filename]) + allow(change_column_default.helper).to receive(:changed_lines).with(filename).and_return(file_diff) + end + + context 'when column default is changed in a regular migration' do + let(:filename) { 'db/migrate/change_column_default_migration.rb' } + + it 'adds comment at the correct line' do + matching_lines.each do |line_number| + expect(change_column_default).to receive(:markdown).with("\n#{comment}", file: filename, line: line_number) + end + + change_column_default.add_comment_for_change_column_default + end + end + + context 'when column default is changed in a post-deployment migration' do + let(:filename) { 'db/post_migrate/change_column_default_migration.rb' } + + it 'adds comment at the correct line' do + matching_lines.each do |line_number| + expect(change_column_default).to receive(:markdown).with("\n#{comment}", file: filename, line: line_number) + end + + change_column_default.add_comment_for_change_column_default + end + end + + context 'when a regular migration does not change column default' do + let(:filename) { 'db/migrate/my_migration.rb' } + let(:file_diff) do + [ + "+ undo_cleanup_concurrent_column_rename(:my_table, :old_column, :new_column)", + "- cleanup_concurrent_column_rename(:my_table, :new_column, :old_column)" + ] + end + + let(:file_lines) do + [ + ' def up', + ' undo_cleanup_concurrent_column_rename(:my_table, :old_column, :new_column)', + ' end' + ] + end + + it 'does not add comment' do + expect(change_column_default).not_to receive(:markdown) + + change_column_default.add_comment_for_change_column_default + end + end + + context 'when a post-deployment migration does not change column default' do + let(:filename) { 'db/post_migrate/my_migration.rb' } + let(:file_diff) do + [ + "+ restore_conversion_of_integer_to_bigint(TABLE, COLUMNS)", + "- cleanup_conversion_of_integer_to_bigint(TABLE, COLUMNS)" + ] + end + + let(:file_lines) do + [ + ' def up', + ' cleanup_conversion_of_integer_to_bigint(TABLE, COLUMNS)', + ' end' + ] + end + + it 'does not add comment' do + expect(change_column_default).not_to receive(:markdown) + + change_column_default.add_comment_for_change_column_default + end + end + end +end diff --git a/spec/tooling/fixtures/change_column_default_migration.txt b/spec/tooling/fixtures/change_column_default_migration.txt new file mode 100644 index 00000000000..a74c31464ee --- /dev/null +++ b/spec/tooling/fixtures/change_column_default_migration.txt @@ -0,0 +1,13 @@ ++# frozen_string_literal: true ++ ++class TestMigration < Gitlab::Database::Migration[2.1] ++ enable_lock_retries! ++ ++ def change ++ change_column_default('ci_builds', 'partition_id', from: 100, to: 101) ++ ++ change_column_default('ci_builds', 'partition_id', from: 100, to: 101) ++ ++ remove_column_default('ci_builds', 'partition_id') ++ end ++end |