summaryrefslogtreecommitdiff
path: root/spec/rubocop/cop/background_migration/missing_dictionary_file_spec.rb
blob: 32b958426b9be481fe56276e9ae800ff330a24f2 (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
# frozen_string_literal: true

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

RSpec.describe RuboCop::Cop::BackgroundMigration::MissingDictionaryFile, feature_category: :database do
  let(:config) do
    RuboCop::Config.new(
      'BackgroundMigration/MissingDictionaryFile' => {
        'EnforcedSince' => 20230307160251
      }
    )
  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(20230307160250)
        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(20230307160252)
        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

        it 'does not throw offense with appropriate dictionary file' do
          expect(File).to receive(:exist?).with(dictionary_file_path).and_return(true)

          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