summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/database/schema_migrations/context_spec.rb
blob: 07c97ea0ec35338a7c983d092469e63edaa43e28 (plain)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Database::SchemaMigrations::Context do
  let(:connection_class) { ActiveRecord::Base }
  let(:connection) { connection_class.connection }

  let(:context) { described_class.new(connection) }

  describe '#schema_directory' do
    it 'returns db/schema_migrations' do
      expect(context.schema_directory).to eq(File.join(Rails.root, described_class.default_schema_migrations_path))
    end

    context 'CI database' do
      let(:connection_class) { Ci::ApplicationRecord }

      it 'returns a directory path that is database specific' do
        skip_if_multiple_databases_not_setup

        expect(context.schema_directory).to eq(File.join(Rails.root, described_class.default_schema_migrations_path))
      end
    end

    context 'multiple databases', :reestablished_active_record_base do
      before do
        connection_class.establish_connection(
          ActiveRecord::Base
            .connection_pool
            .db_config
            .configuration_hash
            .merge(configuration_overrides)
        )
      end

      context 'when `schema_migrations_path` is configured as string' do
        let(:configuration_overrides) do
          { "schema_migrations_path" => "db/ci_schema_migrations" }
        end

        it 'returns a configured directory path that' do
          skip_if_multiple_databases_not_setup

          expect(context.schema_directory).to eq(File.join(Rails.root, 'db/ci_schema_migrations'))
        end
      end

      context 'when `schema_migrations_path` is configured as symbol' do
        let(:configuration_overrides) do
          { schema_migrations_path: "db/ci_schema_migrations" }
        end

        it 'returns a configured directory path that' do
          skip_if_multiple_databases_not_setup

          expect(context.schema_directory).to eq(File.join(Rails.root, 'db/ci_schema_migrations'))
        end
      end
    end
  end

  describe '#versions_to_create' do
    before do
      allow(connection).to receive_message_chain(:schema_migration, :all_versions).and_return(migrated_versions)

      migrations_struct = Struct.new(:version)
      migrations = file_versions.map { |version| migrations_struct.new(version) }
      allow(connection).to receive_message_chain(:migration_context, :migrations).and_return(migrations)
    end

    let(:version1) { '20200123' }
    let(:version2) { '20200410' }
    let(:version3) { '20200602' }
    let(:version4) { '20200809' }

    let(:migrated_versions) { file_versions }
    let(:file_versions) { [version1, version2, version3, version4] }

    context 'migrated versions is the same as migration file versions' do
      it 'returns migrated versions' do
        expect(context.versions_to_create).to eq(migrated_versions)
      end
    end

    context 'migrated versions is subset of migration file versions' do
      let(:migrated_versions) { [version1, version2] }

      it 'returns migrated versions' do
        expect(context.versions_to_create).to eq(migrated_versions)
      end
    end

    context 'migrated versions is superset of migration file versions' do
      let(:migrated_versions) { file_versions + ['20210809'] }

      it 'returns file versions' do
        expect(context.versions_to_create).to eq(file_versions)
      end
    end

    context 'migrated versions has slightly different versions to migration file versions' do
      let(:migrated_versions) { [version1, version2, version3, version4, '20210101'] }
      let(:file_versions) { [version1, version2, version3, version4, '20210102'] }

      it 'returns the common set' do
        expect(context.versions_to_create).to eq([version1, version2, version3, version4])
      end
    end
  end
end