Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'spec/services/database/mark_migration_service_spec.rb')
-rw-r--r--spec/services/database/mark_migration_service_spec.rb71
1 files changed, 71 insertions, 0 deletions
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