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

complex_indexes_require_name_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: ac814c105506c36833971c3d254db9bcd129cf70 (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
# frozen_string_literal: true
#
require 'fast_spec_helper'
require_relative '../../../../rubocop/cop/migration/complex_indexes_require_name'

RSpec.describe RuboCop::Cop::Migration::ComplexIndexesRequireName do
  subject(:cop) { described_class.new }

  context 'when in migration' do
    let(:msg) { 'indexes added with custom options must be explicitly named' }

    before do
      allow(cop).to receive(:in_migration?).and_return(true)
    end

    context 'when creating complex indexes as part of create_table' do
      context 'when indexes are configured with an options hash, but no name' do
        it 'registers an offense' do
          expect_offense(<<~RUBY)
            class TestComplexIndexes < ActiveRecord::Migration[6.0]
              def up
                create_table :test_table do |t|
                  t.integer :column1, null: false
                  t.integer :column2, null: false
                  t.jsonb :column3

                  t.index :column1, unique: true
                  t.index :column2, where: 'column1 = 0'
                    ^^^^^ #{msg}
                  t.index :column3, using: :gin
                    ^^^^^ #{msg}
                end
              end

              def down
                drop_table :test_table
              end
            end
          RUBY
        end
      end

      context 'when indexes are configured with an options hash and name' do
        it 'registers no offense' do
          expect_no_offenses(<<~RUBY)
            class TestComplexIndexes < ActiveRecord::Migration[6.0]
              def up
                create_table :test_table do |t|
                  t.integer :column1, null: false
                  t.integer :column2, null: false
                  t.jsonb :column3

                  t.index :column1, unique: true
                  t.index :column2, where: 'column1 = 0', name: 'my_index_1'
                  t.index :column3, using: :gin, name: 'my_gin_index'
                end
              end

              def down
                drop_table :test_table
              end
            end
          RUBY
        end
      end
    end

    context 'when indexes are added to an existing table' do
      context 'when indexes are configured with an options hash, but no name' do
        it 'registers an offense' do
          expect_offense(<<~RUBY)
            class TestComplexIndexes < ActiveRecord::Migration[6.0]
              disable_ddl_transaction!

              def up
                add_index :test_indexes, :column1

                add_index :test_indexes, :column2, where: "column2 = 'value'", order: { column4: :desc }
                ^^^^^^^^^ #{msg}
              end

              def down
                add_index :test_indexes, :column4, 'unique' => true, where: 'column4 IS NOT NULL'
                ^^^^^^^^^ #{msg}

                add_concurrent_index :test_indexes, :column6, using: :gin, opclass: :gin_trgm_ops
                ^^^^^^^^^^^^^^^^^^^^ #{msg}
              end
            end
          RUBY
        end
      end

      context 'when indexes are configured with an options hash and a name' do
        it 'registers no offenses' do
          expect_no_offenses(<<~RUBY)
            class TestComplexIndexes < ActiveRecord::Migration[6.0]
              INDEX_NAME = 'my_test_name'

              disable_ddl_transaction!

              def up
                add_index :test_indexes, :column1

                add_index :test_indexes, :column2, where: "column2 = 'value'", order: { column4: :desc }, name: 'my_index_1'

                add_concurrent_index :test_indexes, :column3, where: 'column3 = 10', name: 'idx_equal_to_10'
              end

              def down
                add_index :test_indexes, :column4, 'unique' => true, where: 'column4 IS NOT NULL', name: 'my_index_2'

                add_concurrent_index :test_indexes, :column6, using: :gin, opclass: :gin_trgm_ops, name: INDEX_NAME
              end
            end
          RUBY
        end
      end
    end
  end

  context 'when outside migration' do
    before do
      allow(cop).to receive(:in_migration?).and_return(false)
    end

    it 'registers no offenses' do
      expect_no_offenses(<<~RUBY)
        class TestComplexIndexes < ActiveRecord::Migration[6.0]
          disable_ddl_transaction!

          def up
            create_table :test_table do |t|
              t.integer :column1

              t.index :column1, where: 'column2 IS NOT NULL'
            end

            add_index :test_indexes, :column1, where: "some_column = 'value'"
          end

          def down
            add_concurrent_index :test_indexes, :column2, unique: true
          end
        end
      RUBY
    end
  end
end