diff --git a/keeps/overdue_finalize_background_migration.rb b/keeps/overdue_finalize_background_migration.rb index ce8c0d121cc701a0b15499fc04236d263176517c..47e525531249e0db024fb5a91c801a06eafa5d85 100644 --- a/keeps/overdue_finalize_background_migration.rb +++ b/keeps/overdue_finalize_background_migration.rb @@ -29,7 +29,7 @@ def each_change next unless before_cuttoff_milestone?(migration['milestone']) job_name = migration['migration_job_name'] - next if migration_finalized?(job_name) + next if migration_finalized?(migration, job_name) migration_record = fetch_migration_status(job_name) next unless migration_record @@ -41,31 +41,26 @@ def each_change queue_method_node = find_queue_method_node(last_migration_file) - migration_name = truncate_migration_name("Finalize#{migration['migration_job_name']}") + migration_name = truncate_migration_name("FinalizeHK#{job_name}") PostDeploymentMigration::PostDeploymentMigrationGenerator .source_root('generator_templates/post_deployment_migration/post_deployment_migration/') + generator = ::PostDeploymentMigration::PostDeploymentMigrationGenerator.new([migration_name]) + migration_file = generator.invoke_all.first + change.changed_files = [migration_file] - begin - generator = ::PostDeploymentMigration::PostDeploymentMigrationGenerator.new([migration_name]) - migration_file = generator.invoke_all.first - change.changed_files = [migration_file] + add_ensure_call_to_migration(migration_file, queue_method_node, job_name, migration_record) + ::Gitlab::Housekeeper::Shell.rubocop_autocorrect(migration_file) - add_ensure_call_to_migration(migration_file, queue_method_node, job_name, migration_record) - ::Gitlab::Housekeeper::Shell.rubocop_autocorrect(migration_file) + digest = Digest::SHA256.hexdigest(generator.migration_number) + digest_file = Pathname.new('db').join('schema_migrations', generator.migration_number.to_s).to_s + File.open(digest_file, 'w') { |f| f.write(digest) } - digest = Digest::SHA256.hexdigest(generator.migration_number) - digest_file = Pathname.new('db').join('schema_migrations', generator.migration_number.to_s).to_s - File.open(digest_file, 'w') { |f| f.write(digest) } + add_finalized_by_to_yaml(migration_yaml_file, generator.migration_number) - add_finalized_by_to_yaml(migration_yaml_file, generator.migration_number) + change.changed_files << digest_file + change.changed_files << migration_yaml_file - change.changed_files << digest_file - change.changed_files << migration_yaml_file - - yield(change) - rescue Rails::Generators::Error - next - end + yield(change) end end @@ -204,7 +199,9 @@ def postgres_ai @postgres_ai ||= Keeps::Helpers::PostgresAi.new end - def migration_finalized?(job_name) + def migration_finalized?(migration, job_name) + return true if migration['finalized_by'].present? + result = `git grep --name-only "#{job_name}"`.chomp result.each_line.select do |file| File.read(file.chomp).include?('ensure_batched_background_migration_is_finished') diff --git a/spec/keeps/overdue_finalize_background_migration_spec.rb b/spec/keeps/overdue_finalize_background_migration_spec.rb index 8cd32d32c48573abc593e622629361da2f5775a1..64ec2e4211b5e9fcc23b9e60aeada9338fa33477 100644 --- a/spec/keeps/overdue_finalize_background_migration_spec.rb +++ b/spec/keeps/overdue_finalize_background_migration_spec.rb @@ -67,51 +67,4 @@ end end end - - describe '#each_change' do - let(:migration) do - { 'milestone' => '15.0', 'migration_job_name' => 'TestMigration', 'feature_category' => 'shared' } - end - - let(:migration_record) { MigrationRecord.new(id: 1, finished_at: "2020-04-01 12:00:01") } - let(:queue_method_node) do - instance_double(RuboCop::AST::SendNode, children: [nil, nil, nil, - instance_double(RuboCop::AST::StrNode, source: 'table'), - instance_double(RuboCop::AST::StrNode, source: 'column')]) - end - - let(:generator) { instance_double(::PostDeploymentMigration::PostDeploymentMigrationGenerator) } - let(:groups_helper) { instance_double(::Keeps::Helpers::Groups) } - - before do - allow(keep).to receive_messages(batched_background_migrations: { 'path/to/migration.yml' => migration }, - before_cuttoff_milestone?: true, migration_finalized?: false, fetch_migration_status: migration_record, - last_migration_for_job: 'path/to/last_migration.rb', find_queue_method_node: queue_method_node, - groups_helper: groups_helper - ) - allow(groups_helper).to receive_messages(labels_for_feature_category: [], - pick_reviewer_for_feature_category: "reviewer") - allow(PostDeploymentMigration::PostDeploymentMigrationGenerator).to receive(:source_root) - - stub_request(:any, /.*/).to_return(status: 200, body: "", headers: {}) - end - - context 'when generator raises Rails::Generators::Error' do - before do - allow(::PostDeploymentMigration::PostDeploymentMigrationGenerator).to receive(:new) - .and_raise(Rails::Generators::Error) - end - - it 'skips to the next iteration' do - changes = [] - keep.each_change { |change| changes << change } - - expect(changes).to be_empty - end - - it 'does not raise the error' do - expect { keep.each_change { |change| change } }.not_to raise_error - end - end - end end