diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-02-23 18:08:04 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-02-23 18:08:04 +0300 |
commit | b465b2440bbf8ac8c4ef21728ab14fb48589057c (patch) | |
tree | 6e6d76871cc1ecbbf0ad7162605ad65972bf94b6 /spec/lib/generators | |
parent | 4a7ca716f68682c41889dc480c029d71dd121f4e (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/lib/generators')
5 files changed, 159 insertions, 0 deletions
diff --git a/spec/lib/generators/batched_background_migration/batched_background_migration_generator_spec.rb b/spec/lib/generators/batched_background_migration/batched_background_migration_generator_spec.rb new file mode 100644 index 00000000000..957be21f5eb --- /dev/null +++ b/spec/lib/generators/batched_background_migration/batched_background_migration_generator_spec.rb @@ -0,0 +1,76 @@ +# frozen_string_literal: true + +require 'spec_helper' +require 'rails/generators/testing/behaviour' +require 'rails/generators/testing/assertions' + +RSpec.describe BatchedBackgroundMigration::BatchedBackgroundMigrationGenerator, feature_category: :database do + include Rails::Generators::Testing::Behaviour + include Rails::Generators::Testing::Assertions + include FileUtils + + tests described_class + destination File.expand_path('tmp', __dir__) + + before do + prepare_destination + end + + after do + rm_rf(destination_root) + end + + context 'with valid arguments' do + let(:expected_migration_file) { load_expected_file('queue_my_batched_migration.txt') } + let(:expected_migration_spec_file) { load_expected_file('queue_my_batched_migration_spec.txt') } + let(:expected_migration_job_file) { load_expected_file('my_batched_migration.txt') } + let(:expected_migration_job_spec_file) { load_expected_file('my_batched_migration_spec_matcher.txt') } + + it 'generates expected files' do + run_generator %w[my_batched_migration --table_name=projects --column_name=id --feature_category=database] + + assert_migration('db/post_migrate/queue_my_batched_migration.rb') do |migration_file| + expect(migration_file).to eq(expected_migration_file) + end + + assert_migration('spec/migrations/queue_my_batched_migration_spec.rb') do |migration_spec_file| + expect(migration_spec_file).to eq(expected_migration_spec_file) + end + + assert_file('lib/gitlab/background_migration/my_batched_migration.rb') do |migration_job_file| + expect(migration_job_file).to eq(expected_migration_job_file) + end + + assert_file('spec/lib/gitlab/background_migration/my_batched_migration_spec.rb') do |migration_job_spec_file| + # Regex is used to match the dynamic schema: <version> in the specs + expect(migration_job_spec_file).to match(/#{expected_migration_job_spec_file}/) + end + end + end + + context 'without required arguments' do + it 'throws table_name is required error' do + expect do + run_generator %w[my_batched_migration] + end.to raise_error(ArgumentError, 'table_name is required') + end + + it 'throws column_name is required error' do + expect do + run_generator %w[my_batched_migration --table_name=projects] + end.to raise_error(ArgumentError, 'column_name is required') + end + + it 'throws feature_category is required error' do + expect do + run_generator %w[my_batched_migration --table_name=projects --column_name=id] + end.to raise_error(ArgumentError, 'feature_category is required') + end + end + + private + + def load_expected_file(file_name) + File.read(File.expand_path("expected_files/#{file_name}", __dir__)) + end +end diff --git a/spec/lib/generators/batched_background_migration/expected_files/my_batched_migration.txt b/spec/lib/generators/batched_background_migration/expected_files/my_batched_migration.txt new file mode 100644 index 00000000000..b2378b414b1 --- /dev/null +++ b/spec/lib/generators/batched_background_migration/expected_files/my_batched_migration.txt @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +# See https://docs.gitlab.com/ee/development/database/batched_background_migrations.html +# for more information on how to use batched background migrations + +# Update below commented lines with appropriate values. + +module Gitlab + module BackgroundMigration + class MyBatchedMigration < BatchedMigrationJob + # operation_name :my_operation + # scope_to ->(relation) { relation.where(column: "value") } + feature_category :database + + def perform + each_sub_batch do |sub_batch| + # Your action on each sub_batch + end + end + end + end +end diff --git a/spec/lib/generators/batched_background_migration/expected_files/my_batched_migration_spec_matcher.txt b/spec/lib/generators/batched_background_migration/expected_files/my_batched_migration_spec_matcher.txt new file mode 100644 index 00000000000..2728d65d54b --- /dev/null +++ b/spec/lib/generators/batched_background_migration/expected_files/my_batched_migration_spec_matcher.txt @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::BackgroundMigration::MyBatchedMigration, schema: [0-9]+, feature_category: :database do # rubocop:disable Layout/LineLength + # Tests go here +end diff --git a/spec/lib/generators/batched_background_migration/expected_files/queue_my_batched_migration.txt b/spec/lib/generators/batched_background_migration/expected_files/queue_my_batched_migration.txt new file mode 100644 index 00000000000..536e07d56aa --- /dev/null +++ b/spec/lib/generators/batched_background_migration/expected_files/queue_my_batched_migration.txt @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +# See https://docs.gitlab.com/ee/development/database/batched_background_migrations.html +# for more information on when/how to queue batched background migrations + +# Update below commented lines with appropriate values. + +class QueueMyBatchedMigration < Gitlab::Database::Migration[2.1] + MIGRATION = "MyBatchedMigration" + # DELAY_INTERVAL = 2.minutes + # BATCH_SIZE = 1000 + # SUB_BATCH_SIZE = 100 + + def up + queue_batched_background_migration( + MIGRATION, + :projects, + :id, + job_interval: DELAY_INTERVAL, + batch_size: BATCH_SIZE, + sub_batch_size: SUB_BATCH_SIZE + ) + end + + def down + delete_batched_background_migration(MIGRATION, :projects, :id, []) + end +end diff --git a/spec/lib/generators/batched_background_migration/expected_files/queue_my_batched_migration_spec.txt b/spec/lib/generators/batched_background_migration/expected_files/queue_my_batched_migration_spec.txt new file mode 100644 index 00000000000..6f33de4ae83 --- /dev/null +++ b/spec/lib/generators/batched_background_migration/expected_files/queue_my_batched_migration_spec.txt @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +require 'spec_helper' +require_migration! + +RSpec.describe QueueMyBatchedMigration, feature_category: :database do + # let!(:batched_migration) { described_class::MIGRATION } + + # it 'schedules a new batched migration' do + # reversible_migration do |migration| + # migration.before -> { + # expect(batched_migration).not_to have_scheduled_batched_migration + # } + + # migration.after -> { + # expect(batched_migration).to have_scheduled_batched_migration( + # table_name: :projects, + # column_name: :id, + # interval: described_class::DELAY_INTERVAL, + # batch_size: described_class::BATCH_SIZE, + # sub_batch_size: described_class::SUB_BATCH_SIZE + # ) + # } + # end + # end +end |