File: mark_migration_service_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (79 lines) | stat: -rw-r--r-- 2,013 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# 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 }

    def versions
      if ::Gitlab.next_rails?
        connection.schema_migration.versions.count { |v| v == version.to_s }
      else
        ActiveRecord::SchemaMigration.where(version: version).count
      end
    end

    it 'marks the migration as successful' do
      expect { execute }
        .to change { versions }
        .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 { versions }
      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 { versions }
      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