summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/database/schema_migrations/context_spec.rb
blob: 1f1943d00a3925b4b7928eec0fc9ff8cf7cef19f (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# 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, 'db/schema_migrations'))
    end

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

      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, 'db/schema_migrations'))
      end
    end

    context 'multiple databases' do
      let(:connection_class) do
        Class.new(::ApplicationRecord) do
          self.abstract_class = true

          def self.name
            'Gitlab::Database::SchemaMigrations::Context::TestConnection'
          end
        end
      end

      let(:configuration_overrides) { {} }

      before do
        connection_class.establish_connection(
          ActiveRecord::Base
            .connection_pool
            .db_config
            .configuration_hash
            .merge(configuration_overrides)
        )
      end

      after do
        connection_class.remove_connection
      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

  def skip_if_multiple_databases_not_setup
    skip 'Skipping because multiple databases not set up' unless Gitlab::Database.has_config?(:ci)
  end
end