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

add_column_with_default_spec.rb « migration « cop « rubocop « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a8cf965a3eff8ef026ed464c1c5267865100712f (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
# frozen_string_literal: true

require 'spec_helper'

require 'rubocop'
require 'rubocop/rspec/support'

require_relative '../../../../rubocop/cop/migration/add_column_with_default'

describe RuboCop::Cop::Migration::AddColumnWithDefault do
  include CopHelper

  let(:cop) { described_class.new }

  context 'outside of a migration' do
    it 'does not register any offenses' do
      expect_no_offenses(<<~RUBY)
        def up
          add_column_with_default(:merge_request_diff_files, :artifacts, :boolean, default: true, allow_null: false)
        end
      RUBY
    end
  end

  context 'in a migration' do
    before do
      allow(cop).to receive(:in_migration?).and_return(true)
    end

    let(:offense) { '`add_column_with_default` without `allow_null: true` may cause prolonged lock situations and downtime, see https://gitlab.com/gitlab-org/gitlab/issues/38060' }

    context 'for blacklisted table' do
      it 'registers an offense when specifying allow_null: false' do
        expect_offense(<<~RUBY)
          def up
            add_column_with_default(:merge_request_diff_files, :artifacts, :boolean, default: true, allow_null: false)
            ^^^^^^^^^^^^^^^^^^^^^^^ #{offense}
          end
        RUBY
      end

      it 'registers no offense when specifying allow_null: true' do
        expect_no_offenses(<<~RUBY)
          def up
            add_column_with_default(:merge_request_diff_files, :artifacts, :boolean, default: true, allow_null: true)
          end
        RUBY
      end

      it 'registers an offense when allow_null is not specified' do
        expect_offense(<<~RUBY)
          def up
            add_column_with_default(:merge_request_diff_files, :artifacts, :boolean, default: true)
            ^^^^^^^^^^^^^^^^^^^^^^^ #{offense}
          end
        RUBY
      end
    end

    context 'for tables not on the blacklist' do
      it 'registers no offense for application_settings (not on blacklist)' do
        expect_no_offenses(<<~RUBY)
          def up
            add_column_with_default(:application_settings, :another_column, :boolean, default: true, allow_null: false)
          end
        RUBY
      end
    end
  end
end