2020-11-24 15:15:51 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
#
|
|
|
|
require 'fast_spec_helper'
|
|
|
|
require_relative '../../../../rubocop/cop/migration/refer_to_index_by_name'
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
RSpec.describe RuboCop::Cop::Migration::ReferToIndexByName do
|
2020-11-24 15:15:51 +05:30
|
|
|
subject(:cop) { described_class.new }
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
context 'when in migration' do
|
2020-11-24 15:15:51 +05:30
|
|
|
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
|
2021-04-17 20:07:23 +05:30
|
|
|
expect_offense(<<~RUBY, msg: 'migration methods that refer to existing indexes must do so by name')
|
2020-11-24 15:15:51 +05:30
|
|
|
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
|
2021-04-17 20:07:23 +05:30
|
|
|
^^^^^^^^^^^^^ %{msg}
|
2020-11-24 15:15:51 +05:30
|
|
|
remove_index :test_indexes, :column2
|
2021-04-17 20:07:23 +05:30
|
|
|
^^^^^^^^^^^^ %{msg}
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
remove_index :test_indexes, column: column3
|
2021-04-17 20:07:23 +05:30
|
|
|
^^^^^^^^^^^^ %{msg}
|
2020-11-24 15:15:51 +05:30
|
|
|
|
|
|
|
remove_index :test_indexes, name: 'index_name_4'
|
|
|
|
end
|
|
|
|
|
|
|
|
def down
|
|
|
|
if index_exists? :test_indexes, :column4, using: :gin, opclass: :gin_trgm_ops
|
2021-04-17 20:07:23 +05:30
|
|
|
^^^^^^^^^^^^^ %{msg}
|
2020-11-24 15:15:51 +05:30
|
|
|
remove_concurrent_index :test_indexes, :column4, using: :gin, opclass: :gin_trgm_ops
|
2021-04-17 20:07:23 +05:30
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^ %{msg}
|
2020-11-24 15:15:51 +05:30
|
|
|
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
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
context 'when outside migration' do
|
2020-11-24 15:15:51 +05:30
|
|
|
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
|