2019-10-29 08:06:40 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-08-18 14:10:07 -04:00
|
|
|
require 'rubocop_spec_helper'
|
2018-08-01 11:52:54 -04:00
|
|
|
require_relative '../../../../rubocop/cop/migration/add_reference'
|
|
|
|
|
2021-01-07 13:10:38 -05:00
|
|
|
RSpec.describe RuboCop::Cop::Migration::AddReference do
|
2018-08-01 11:52:54 -04:00
|
|
|
let(:cop) { described_class.new }
|
|
|
|
|
2021-02-17 16:09:06 -05:00
|
|
|
context 'when outside of a migration' do
|
2018-08-01 11:52:54 -04:00
|
|
|
it 'does not register any offenses' do
|
|
|
|
expect_no_offenses(<<~RUBY)
|
|
|
|
def up
|
|
|
|
add_reference(:projects, :users)
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-02-17 16:09:06 -05:00
|
|
|
context 'when in a migration' do
|
2018-08-01 11:52:54 -04:00
|
|
|
before do
|
|
|
|
allow(cop).to receive(:in_migration?).and_return(true)
|
|
|
|
end
|
|
|
|
|
2021-02-17 16:09:06 -05:00
|
|
|
let(:offense) { '`add_reference` requires downtime for existing tables, use `add_concurrent_foreign_key`[...]' }
|
2019-09-13 14:06:03 -04:00
|
|
|
|
|
|
|
context 'when the table existed before' do
|
|
|
|
it 'registers an offense when using add_reference' do
|
|
|
|
expect_offense(<<~RUBY)
|
|
|
|
def up
|
|
|
|
add_reference(:projects, :users)
|
|
|
|
^^^^^^^^^^^^^ #{offense}
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'registers an offense when using add_reference with index enabled' do
|
|
|
|
expect_offense(<<~RUBY)
|
|
|
|
def up
|
|
|
|
add_reference(:projects, :users, index: true)
|
|
|
|
^^^^^^^^^^^^^ #{offense}
|
2018-08-01 11:52:54 -04:00
|
|
|
end
|
2019-09-13 14:06:03 -04:00
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'registers an offense if only a different table was created' do
|
|
|
|
expect_offense(<<~RUBY)
|
|
|
|
def up
|
|
|
|
create_table(:foo) do |t|
|
|
|
|
t.string :name
|
|
|
|
end
|
|
|
|
add_reference(:projects, :users, index: true)
|
|
|
|
^^^^^^^^^^^^^ #{offense}
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
2018-08-01 11:52:54 -04:00
|
|
|
end
|
|
|
|
|
2019-09-13 14:06:03 -04:00
|
|
|
context 'when creating the table at the same time' do
|
|
|
|
let(:create_table_statement) do
|
|
|
|
<<~RUBY
|
|
|
|
create_table(:projects) do |t|
|
|
|
|
t.string :name
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'registers an offense when using add_reference without index' do
|
|
|
|
expect_offense(<<~RUBY)
|
2018-08-01 11:52:54 -04:00
|
|
|
def up
|
2019-09-13 14:06:03 -04:00
|
|
|
#{create_table_statement}
|
|
|
|
add_reference(:projects, :users)
|
|
|
|
^^^^^^^^^^^^^ #{offense}
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'registers an offense when using add_reference index disabled' do
|
|
|
|
expect_offense(<<~RUBY)
|
|
|
|
def up
|
|
|
|
#{create_table_statement}
|
2018-08-01 11:52:54 -04:00
|
|
|
add_reference(:projects, :users, index: false)
|
2019-09-13 14:06:03 -04:00
|
|
|
^^^^^^^^^^^^^ #{offense}
|
2018-08-01 11:52:54 -04:00
|
|
|
end
|
2019-09-13 14:06:03 -04:00
|
|
|
RUBY
|
|
|
|
end
|
2018-08-01 11:52:54 -04:00
|
|
|
|
2019-09-13 14:06:03 -04:00
|
|
|
it 'does not register an offense when using add_reference with index enabled' do
|
|
|
|
expect_no_offenses(<<~RUBY)
|
2018-08-01 11:52:54 -04:00
|
|
|
def up
|
2019-09-13 14:06:03 -04:00
|
|
|
#{create_table_statement}
|
2018-08-01 11:52:54 -04:00
|
|
|
add_reference(:projects, :users, index: true)
|
|
|
|
end
|
2019-09-13 14:06:03 -04:00
|
|
|
RUBY
|
|
|
|
end
|
2018-11-22 15:13:36 -05:00
|
|
|
|
2019-09-13 14:06:03 -04:00
|
|
|
it 'does not register an offense when the index is unique' do
|
|
|
|
expect_no_offenses(<<~RUBY)
|
2018-11-22 15:13:36 -05:00
|
|
|
def up
|
2019-09-13 14:06:03 -04:00
|
|
|
#{create_table_statement}
|
2018-11-22 15:13:36 -05:00
|
|
|
add_reference(:projects, :users, index: { unique: true } )
|
|
|
|
end
|
2019-09-13 14:06:03 -04:00
|
|
|
RUBY
|
|
|
|
end
|
2018-11-22 15:13:36 -05:00
|
|
|
end
|
2018-08-01 11:52:54 -04:00
|
|
|
end
|
|
|
|
end
|