From 5afcbe03ead9ada87621888a31a62652b10a7e4f Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Wed, 20 Sep 2023 11:18:08 +0000 Subject: Add latest changes from gitlab-org/gitlab@16-4-stable-ee --- spec/tooling/danger/clickhouse_spec.rb | 70 ++++++++++ spec/tooling/danger/ignored_model_columns_spec.rb | 145 +++++++++++++++++++++ .../fixtures/cleanup_conversion_migration.txt | 44 +++++++ spec/tooling/fixtures/remove_column_migration.txt | 84 ++++++++++++ spec/tooling/fixtures/rename_column_migration.txt | 45 +++++++ 5 files changed, 388 insertions(+) create mode 100644 spec/tooling/danger/clickhouse_spec.rb create mode 100644 spec/tooling/danger/ignored_model_columns_spec.rb create mode 100644 spec/tooling/fixtures/cleanup_conversion_migration.txt create mode 100644 spec/tooling/fixtures/remove_column_migration.txt create mode 100644 spec/tooling/fixtures/rename_column_migration.txt (limited to 'spec/tooling') diff --git a/spec/tooling/danger/clickhouse_spec.rb b/spec/tooling/danger/clickhouse_spec.rb new file mode 100644 index 00000000000..ad2f0b4a827 --- /dev/null +++ b/spec/tooling/danger/clickhouse_spec.rb @@ -0,0 +1,70 @@ +# frozen_string_literal: true + +require 'rspec-parameterized' +require 'gitlab-dangerfiles' +require 'danger' +require 'danger/plugins/internal/helper' +require 'gitlab/dangerfiles/spec_helper' + +require_relative '../../../tooling/danger/clickhouse' + +RSpec.describe Tooling::Danger::Clickhouse, feature_category: :tooling do + include_context "with dangerfile" + + let(:fake_danger) { DangerSpecHelper.fake_danger.include(described_class) } + let(:migration_files) do + %w[ + db/click_house/20220901010203_add_widgets_table.rb + db/click_house/20220909010203_add_properties_column.rb + db/click_house/20220910010203_drop_tools_table.rb + db/click_house/20220912010203_add_index_to_widgets_table.rb + ] + end + + subject(:clickhouse) { fake_danger.new(helper: fake_helper) } + + describe '#changes' do + using RSpec::Parameterized::TableSyntax + + where do + { + 'with click_house gem changes' => { + modified_files: %w[gems/click_house-client/lib/click_house/client.rb], + changes_by_category: { + database: [], + clickhouse: %w[gems/click_house-client/lib/click_house/client.rb] + }, + impacted_files: %w[gems/click_house-client/lib/click_house/client.rb] + }, + 'with clickhouse data changes' => { + modified_files: %w[db/clickhouse/20230720114001_add_magic_table_migration.rb], + changes_by_category: { + database: [], + clickhouse: %w[db/clickhouse/20230720114001_add_magic_table_migration.rb] + }, + impacted_files: %w[db/clickhouse/20230720114001_add_magic_table_migration.rb] + }, + 'with clickhouse app changes' => { + modified_files: %w[lib/click_house/query_builder.rb], + changes_by_category: { + database: [], + clickhouse: %w[lib/click_house/query_builder.rb] + }, + impacted_files: %w[lib/click_house/query_builder.rb] + } + } + end + + with_them do + before do + allow(fake_helper).to receive(:modified_files).and_return(modified_files) + allow(fake_helper).to receive(:all_changed_files).and_return(modified_files) + allow(fake_helper).to receive(:changes_by_category).and_return(changes_by_category) + end + + it 'returns only clickhouse changes' do + expect(clickhouse.changes).to match impacted_files + end + end + end +end diff --git a/spec/tooling/danger/ignored_model_columns_spec.rb b/spec/tooling/danger/ignored_model_columns_spec.rb new file mode 100644 index 00000000000..737b6cce077 --- /dev/null +++ b/spec/tooling/danger/ignored_model_columns_spec.rb @@ -0,0 +1,145 @@ +# frozen_string_literal: true + +require 'danger' +require 'gitlab/dangerfiles/spec_helper' + +require_relative '../../../tooling/danger/ignored_model_columns' +require_relative '../../../tooling/danger/project_helper' + +RSpec.describe Tooling::Danger::IgnoredModelColumns, feature_category: :tooling do + subject(:ignored_model_columns) { 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/#{fixture}", __dir__)).split("\n") + end + + include_context "with dangerfile" + + describe '#add_comment_for_ignored_model_columns' do + let(:file_lines) { file_diff.map { |line| line.delete_prefix('+').delete_prefix('-') } } + + before do + allow(ignored_model_columns).to receive(:project_helper).and_return(fake_project_helper) + allow(ignored_model_columns.project_helper).to receive(:file_lines).and_return(file_lines) + allow(ignored_model_columns.helper).to receive(:all_changed_files).and_return([filename]) + allow(ignored_model_columns.helper).to receive(:changed_lines).with(filename).and_return(file_diff) + end + + context 'when table column is renamed in a regular migration' do + let(:filename) { 'db/migrate/rename_my_column_migration.rb' } + let(:fixture) { 'rename_column_migration.txt' } + let(:matching_lines) { [7, 11, 15, 19, 23, 27, 31, 35, 39] } + + it 'adds comment at the correct line' do + matching_lines.each do |line_number| + expect(ignored_model_columns).to receive(:markdown).with("\n#{comment}", file: filename, line: line_number) + end + + ignored_model_columns.add_comment_for_ignored_model_columns + end + end + + context 'when table column is renamed in a post migration' do + let(:filename) { 'db/post_migrate/remove_column_migration.rb' } + let(:fixture) { 'remove_column_migration.txt' } + let(:matching_lines) { [7, 8, 16, 24, 32, 40, 48, 56, 64, 72] } + + it 'adds comment at the correct line' do + matching_lines.each do |line_number| + expect(ignored_model_columns).to receive(:markdown).with("\n#{comment}", file: filename, line: line_number) + end + + ignored_model_columns.add_comment_for_ignored_model_columns + end + end + + context 'when table cleanup is performed in a post migration' do + let(:filename) { 'db/post_migrate/cleanup_conversion_big_int_migration.rb' } + let(:fixture) { 'cleanup_conversion_migration.txt' } + let(:matching_lines) { [7, 11, 15, 19, 23, 27, 31, 35, 39] } + + it 'adds comment at the correct line' do + matching_lines.each do |line_number| + expect(ignored_model_columns).to receive(:markdown).with("\n#{comment}", file: filename, line: line_number) + end + + ignored_model_columns.add_comment_for_ignored_model_columns + end + end + + context 'when a regular migration does not rename table column' 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(ignored_model_columns).not_to receive(:markdown) + + ignored_model_columns.add_comment_for_ignored_model_columns + end + end + + context 'when a post migration does not remove table column' do + let(:filename) { 'db/migrate/my_migration.rb' } + let(:file_diff) do + [ + "+ add_column(:my_table, :my_column, :type)", + "- remove_column(:my_table, :my_column)" + ] + end + + let(:file_lines) do + [ + ' def up', + ' add_column(:my_table, :my_column, :type)', + ' end' + ] + end + + it 'does not add comment' do + expect(ignored_model_columns).not_to receive(:markdown) + + ignored_model_columns.add_comment_for_ignored_model_columns + end + end + + context 'when a post migration does not convert table column' do + let(:filename) { 'db/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(ignored_model_columns).not_to receive(:markdown) + + ignored_model_columns.add_comment_for_ignored_model_columns + end + end + end +end diff --git a/spec/tooling/fixtures/cleanup_conversion_migration.txt b/spec/tooling/fixtures/cleanup_conversion_migration.txt new file mode 100644 index 00000000000..14a7937b469 --- /dev/null +++ b/spec/tooling/fixtures/cleanup_conversion_migration.txt @@ -0,0 +1,44 @@ ++# frozen_string_literal: true ++ ++class TestMigration < Gitlab::Database::Migration[2.1] ++ disable_ddl_transaction! ++ ++ def up ++ cleanup_conversion_of_integer_to_bigint :my_table, :my_column ++ end ++ ++ def up ++ cleanup_conversion_of_integer_to_bigint 'my_table', 'my_column' ++ end ++ ++ def up ++ cleanup_conversion_of_integer_to_bigint "my_table", "my_column", "new_column" ++ end ++ ++ def up ++ cleanup_conversion_of_integer_to_bigint TABLE_NAME, MY_COLUMN ++ end ++ ++ def up ++ cleanup_conversion_of_integer_to_bigint(:my_table, :my_column) ++ end ++ ++ def up ++ cleanup_conversion_of_integer_to_bigint('my_table', 'my_column') ++ end ++ ++ def up ++ cleanup_conversion_of_integer_to_bigint("my_table", "my_column") ++ end ++ ++ def up ++ cleanup_conversion_of_integer_to_bigint(TABLE_NAME, MY_COLUMN) ++ end ++ ++ def up ++ cleanup_conversion_of_integer_to_bigint( ++ :my_table, ++ :my_column ++ ) ++ end ++end diff --git a/spec/tooling/fixtures/remove_column_migration.txt b/spec/tooling/fixtures/remove_column_migration.txt new file mode 100644 index 00000000000..885f0060d92 --- /dev/null +++ b/spec/tooling/fixtures/remove_column_migration.txt @@ -0,0 +1,84 @@ ++# frozen_string_literal: true ++ ++class TestMigration < Gitlab::Database::Migration[2.1] ++ disable_ddl_transaction! ++ ++ def up ++ remove_column :my_table, :my_column ++ remove_column :my_other_table, :my_column ++ end ++ ++ def down ++ remove_column :my_table, :my_column ++ end ++ ++ def up ++ remove_column 'my_table', 'my_column' ++ end ++ ++ def down ++ remove_column 'my_table', 'my_column' ++ end ++ ++ def up ++ remove_column "my_table", "my_column", "new_column" ++ end ++ ++ def down ++ remove_column "my_table", "my_column", "new_column" ++ end ++ ++ def up ++ remove_column TABLE_NAME, MY_COLUMN ++ end ++ ++ def down ++ remove_column TABLE_NAME, MY_COLUMN ++ end ++ ++ def up ++ remove_column(:my_table, :my_column) ++ end ++ ++ def down ++ remove_column(:my_table, :my_column) ++ end ++ ++ def up ++ remove_column('my_table', 'my_column') ++ end ++ ++ def down ++ remove_column('my_table', 'my_column') ++ end ++ ++ def up ++ remove_column("my_table", "my_column") ++ end ++ ++ def down ++ remove_column("my_table", "my_column") ++ end ++ ++ def up ++ remove_column(TABLE_NAME, MY_COLUMN) ++ end ++ ++ def down ++ remove_column(TABLE_NAME, MY_COLUMN) ++ end ++ ++ def up ++ remove_column( ++ :my_table, ++ :my_column ++ ) ++ end ++ ++ def down ++ remove_column( ++ :my_table, ++ :my_column ++ ) ++ end ++end diff --git a/spec/tooling/fixtures/rename_column_migration.txt b/spec/tooling/fixtures/rename_column_migration.txt new file mode 100644 index 00000000000..e79029219a5 --- /dev/null +++ b/spec/tooling/fixtures/rename_column_migration.txt @@ -0,0 +1,45 @@ ++# frozen_string_literal: true ++ ++class TestMigration < Gitlab::Database::Migration[2.1] ++ disable_ddl_transaction! ++ ++ def up ++ cleanup_concurrent_column_rename :my_table, :old_column, :new_column ++ end ++ ++ def up ++ cleanup_concurrent_column_rename 'my_table', 'old_column', 'new_column' ++ end ++ ++ def up ++ cleanup_concurrent_column_rename "my_table", "old_column", "new_column" ++ end ++ ++ def up ++ cleanup_concurrent_column_rename TABLE_NAME, OLD_COLUMN_NAME, NEW_COLUMN_NAME ++ end ++ ++ def up ++ cleanup_concurrent_column_rename(:my_table, :old_column, :new_column) ++ end ++ ++ def up ++ cleanup_concurrent_column_rename('my_table', 'old_column', 'new_column') ++ end ++ ++ def up ++ cleanup_concurrent_column_rename("my_table", "old_column", "new_column") ++ end ++ ++ def up ++ cleanup_concurrent_column_rename(TABLE_NAME, OLD_COLUMN_NAME, NEW_COLUMN_NAME) ++ end ++ ++ def up ++ cleanup_concurrent_column_rename( ++ :my_table, ++ :old_column, ++ :new_column ++ ) ++ end ++end -- cgit v1.2.3