From 1fc72cb8765dab466da8555b70eb744a53a74a80 Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Wed, 14 Jun 2023 09:09:21 +0000 Subject: Add latest changes from gitlab-org/gitlab@master --- .../database/mark_migration_service_spec.rb | 71 ++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 spec/services/database/mark_migration_service_spec.rb (limited to 'spec/services/database/mark_migration_service_spec.rb') diff --git a/spec/services/database/mark_migration_service_spec.rb b/spec/services/database/mark_migration_service_spec.rb new file mode 100644 index 00000000000..5fd2268484e --- /dev/null +++ b/spec/services/database/mark_migration_service_spec.rb @@ -0,0 +1,71 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Database::MarkMigrationService, feature_category: :database do + let(:service) { described_class.new(connection: connection, version: version) } + let(:version) { 1 } + let(:connection) { ApplicationRecord.connection } + + let(:migrations) do + [ + instance_double( + ActiveRecord::MigrationProxy, + version: 1, + name: 'migration_pending', + filename: 'db/migrate/1_migration_pending.rb' + ) + ] + end + + before do + ctx = instance_double(ActiveRecord::MigrationContext, migrations: migrations) + allow(connection).to receive(:migration_context).and_return(ctx) + end + + describe '#execute' do + subject(:execute) { service.execute } + + it 'marks the migration as successful' do + expect { execute } + .to change { ActiveRecord::SchemaMigration.where(version: version).count } + .by(1) + + is_expected.to be_success + end + + context 'when the migration does not exist' do + let(:version) { 123 } + + it { is_expected.to be_error } + it { expect(execute.reason).to eq(:not_found) } + + it 'does not insert records' do + expect { execute } + .not_to change { ActiveRecord::SchemaMigration.where(version: version).count } + end + end + + context 'when the migration was already executed' do + before do + allow(service).to receive(:all_versions).and_return([version]) + end + + it { is_expected.to be_error } + it { expect(execute.reason).to eq(:invalid) } + + it 'does not insert records' do + expect { execute } + .not_to change { ActiveRecord::SchemaMigration.where(version: version).count } + end + end + + context 'when the insert fails' do + it 'returns an error response' do + expect(service).to receive(:create_version).with(version).and_return(false) + + is_expected.to be_error + end + end + end +end -- cgit v1.2.3