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

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-08-28 12:10:32 +0300
committerGitLab Bot <gitlab-bot@gitlab.com>2020-08-28 12:10:32 +0300
commitc4b69460e8dd4921eb20fa78b47ceff294d725dc (patch)
tree9be3f5c3913ead9eeb5f183ac7a163b193412967 /spec/rubocop
parent540c69c58c9e60f2d9b47987afe5e020593d39ac (diff)
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/rubocop')
-rw-r--r--spec/rubocop/cop/migration/refer_to_index_by_name_spec.rb90
1 files changed, 90 insertions, 0 deletions
diff --git a/spec/rubocop/cop/migration/refer_to_index_by_name_spec.rb b/spec/rubocop/cop/migration/refer_to_index_by_name_spec.rb
new file mode 100644
index 00000000000..76554d7446c
--- /dev/null
+++ b/spec/rubocop/cop/migration/refer_to_index_by_name_spec.rb
@@ -0,0 +1,90 @@
+# frozen_string_literal: true
+#
+require 'fast_spec_helper'
+require 'rubocop'
+require_relative '../../../../rubocop/cop/migration/refer_to_index_by_name'
+
+RSpec.describe RuboCop::Cop::Migration::ReferToIndexByName, type: :rubocop do
+ include CopHelper
+
+ subject(:cop) { described_class.new }
+
+ context 'in migration' do
+ before do
+ allow(cop).to receive(:in_migration?).and_return(true)
+ end
+
+ context 'when existing indexes are referred to without an explicit name' do
+ it 'registers an offense' do
+ expect_offense(<<~RUBY)
+ class TestReferToIndexByName < ActiveRecord::Migration[6.0]
+ DOWNTIME = false
+
+ INDEX_NAME = 'my_test_name'
+
+ disable_ddl_transaction!
+
+ def up
+ if index_exists? :test_indexes, :column1, name: 'index_name_1'
+ remove_index :test_indexes, column: :column1, name: 'index_name_1'
+ end
+
+ if index_exists? :test_indexes, :column2
+ ^^^^^^^^^^^^^ #{described_class::MSG}
+ remove_index :test_indexes, :column2
+ ^^^^^^^^^^^^ #{described_class::MSG}
+ end
+
+ remove_index :test_indexes, column: column3
+ ^^^^^^^^^^^^ #{described_class::MSG}
+
+ remove_index :test_indexes, name: 'index_name_4'
+ end
+
+ def down
+ if index_exists? :test_indexes, :column4, using: :gin, opclass: :gin_trgm_ops
+ ^^^^^^^^^^^^^ #{described_class::MSG}
+ remove_concurrent_index :test_indexes, :column4, using: :gin, opclass: :gin_trgm_ops
+ ^^^^^^^^^^^^^^^^^^^^^^^ #{described_class::MSG}
+ end
+
+ if index_exists? :test_indexes, :column3, unique: true, name: 'index_name_3', where: 'column3 = 10'
+ remove_concurrent_index :test_indexes, :column3, unique: true, name: 'index_name_3', where: 'column3 = 10'
+ end
+ end
+ end
+ RUBY
+
+ expect(cop.offenses.map(&:cop_name)).to all(eq("Migration/#{described_class.name.demodulize}"))
+ end
+ end
+ end
+
+ context '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 TestReferToIndexByName < ActiveRecord::Migration[6.0]
+ DOWNTIME = false
+
+ disable_ddl_transaction!
+
+ def up
+ if index_exists? :test_indexes, :column1
+ remove_index :test_indexes, :column1
+ end
+ end
+
+ def down
+ if index_exists? :test_indexes, :column1
+ remove_concurrent_index :test_indexes, :column1
+ end
+ end
+ end
+ RUBY
+ end
+ end
+end