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

dictionary_file_spec.rb « background_migration « cop « rubocop « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9b4adc87f7828575021a859ed69174d134f6ae18 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# frozen_string_literal: true

require 'rubocop_spec_helper'
require_relative '../../../../rubocop/cop/background_migration/dictionary_file'

RSpec.describe RuboCop::Cop::BackgroundMigration::DictionaryFile, feature_category: :database do
  let(:config) do
    RuboCop::Config.new(
      'BackgroundMigration/DictionaryFile' => {
        'EnforcedSince' => 20231018100907
      }
    )
  end

  shared_examples 'migration with missing dictionary keys offense' do |missing_key|
    it 'registers an offense' do
      expect_offense(<<~RUBY)
        class QueueMyMigration < Gitlab::Database::Migration[2.1]
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{format(described_class::MSG[:missing_key], key: missing_key)}
          MIGRATION = 'MyMigration'

          def up
            queue_batched_background_migration(
              MIGRATION,
              :users,
              :id
            )
          end
        end
      RUBY
    end
  end

  context 'for non post migrations' do
    before do
      allow(cop).to receive(:in_post_deployment_migration?).and_return(false)
    end

    it 'does not throw any offense' do
      expect_no_offenses(<<~RUBY)
        class QueueMyMigration < Gitlab::Database::Migration[2.1]
          MIGRATION = 'MyMigration'

          def up
            queue_batched_background_migration(
              MIGRATION,
              :users,
              :id
            )
          end
        end
      RUBY
    end
  end

  context 'for post migrations' do
    before do
      allow(cop).to receive(:in_post_deployment_migration?).and_return(true)
    end

    context 'without enqueuing batched migrations' do
      it 'does not throw any offense' do
        expect_no_offenses(<<~RUBY)
          class CreateTestTable < Gitlab::Database::Migration[2.1]
            def change
              create_table(:tests)
            end
          end
        RUBY
      end
    end

    context 'with enqueuing batched migration' do
      let(:rails_root) { File.expand_path('../../../..', __dir__) }
      let(:dictionary_file_path) { File.join(rails_root, 'db/docs/batched_background_migrations/my_migration.yml') }

      context 'for migrations before enforced time' do
        before do
          allow(cop).to receive(:version).and_return(20230918100907)
        end

        it 'does not throw any offenses' do
          expect_no_offenses(<<~RUBY)
            class QueueMyMigration < Gitlab::Database::Migration[2.1]
              MIGRATION = 'MyMigration'

              def up
                queue_batched_background_migration(
                  MIGRATION,
                  :users,
                  :id
                )
              end
            end
          RUBY
        end
      end

      context 'for migrations after enforced time' do
        before do
          allow(cop).to receive(:version).and_return(20231118100907)
        end

        it 'throws offense on not having the appropriate dictionary file with migration name as a constant' do
          expect_offense(<<~RUBY)
            class QueueMyMigration < Gitlab::Database::Migration[2.1]
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{format("Missing %{file_name}. Use the generator 'batched_background_migration' to create dictionary files automatically. For more details refer: https://docs.gitlab.com/ee/development/database/batched_background_migrations.html#generator", file_name: dictionary_file_path)}
              MIGRATION = 'MyMigration'

              def up
                queue_batched_background_migration(
                  MIGRATION,
                  :users,
                  :id
                )
              end
            end
          RUBY
        end

        it 'throws offense on not having the appropriate dictionary file with migration name as a variable' do
          expect_offense(<<~RUBY)
            class QueueMyMigration < Gitlab::Database::Migration[2.1]
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{format("Missing %{file_name}. Use the generator 'batched_background_migration' to create dictionary files automatically. For more details refer: https://docs.gitlab.com/ee/development/database/batched_background_migrations.html#generator", file_name: dictionary_file_path)}
              def up
                queue_batched_background_migration(
                  'MyMigration',
                  :users,
                  :id
                )
              end
            end
          RUBY
        end

        context 'with dictionary file' do
          let(:introduced_by_url) { 'https://gitlab.com/gitlab-org/gitlab/-/merge_requests/132639' }
          let(:finalize_after) { '20230507160251' }
          let(:milestone) { '16.1' }

          before do
            allow(File).to receive(:exist?).and_call_original
            allow(File).to receive(:exist?).with(dictionary_file_path).and_return(true)
            allow(::RuboCop::BatchedBackgroundMigrationsDictionary).to receive(:dictionary_data).and_return({
              '20231118100907' => {
                finalize_after: finalize_after,
                introduced_by_url: introduced_by_url,
                milestone: milestone
              }
            })
          end

          context 'without introduced_by_url' do
            it_behaves_like 'migration with missing dictionary keys offense', :introduced_by_url do
              let(:introduced_by_url) { nil }
            end
          end

          context 'without finalize_after' do
            it_behaves_like 'migration with missing dictionary keys offense', :finalize_after do
              let(:finalize_after) { nil }
            end
          end

          context 'when the `introduced_by_url` is not correct' do
            let(:introduced_by_url) { 'https://gitlab.com/gitlab-org/gitlab/-/merge_requests/132639/invalid' }

            it 'throws offense on having a correct url' do
              expect_offense(<<~RUBY)
                class QueueMyMigration < Gitlab::Database::Migration[2.1]
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{format('Invalid `introduced_by_url` url for the dictionary. Please use the following format: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/XXX')}
                  def up
                    queue_batched_background_migration(
                      'MyMigration',
                      :users,
                      :id
                    )
                  end
                end
              RUBY
            end
          end

          context 'without milestone' do
            it_behaves_like 'migration with missing dictionary keys offense', :milestone do
              let(:milestone) { nil }
            end
          end

          context 'when milestone is a number' do
            let(:milestone) { 16.1 }

            it 'throws offense on having an invalid milestone' do
              expect_offense(<<~RUBY)
                class QueueMyMigration < Gitlab::Database::Migration[2.1]
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{format('Invalid `milestone` for the dictionary. It must be a string. Please ensure it is quoted.')}
                  def up
                    queue_batched_background_migration(
                      'MyMigration',
                      :users,
                      :id
                    )
                  end
                end
              RUBY
            end
          end

          context 'with required dictionary keys' do
            it 'does not throw offense with appropriate dictionary file' do
              expect_no_offenses(<<~RUBY)
                class QueueMyMigration < Gitlab::Database::Migration[2.1]
                  MIGRATION = 'MyMigration'

                  def up
                    queue_batched_background_migration(
                      MIGRATION,
                      :users,
                      :id
                    )
                  end
                end
              RUBY
            end
          end
        end
      end
    end
  end
end