2020-04-22 19:07:51 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
require 'fast_spec_helper'
|
2020-04-22 19:07:51 +05:30
|
|
|
require_relative '../../../../rubocop/cop/migration/add_columns_to_wide_tables'
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
RSpec.describe RuboCop::Cop::Migration::AddColumnsToWideTables do
|
2020-04-22 19:07:51 +05:30
|
|
|
let(:cop) { described_class.new }
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
context 'when outside of a migration' do
|
2020-04-22 19:07:51 +05:30
|
|
|
it 'does not register any offenses' do
|
|
|
|
expect_no_offenses(<<~RUBY)
|
|
|
|
def up
|
|
|
|
add_column(:users, :another_column, :string)
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
context 'when in a migration' do
|
2020-04-22 19:07:51 +05:30
|
|
|
before do
|
|
|
|
allow(cop).to receive(:in_migration?).and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with wide tables' do
|
|
|
|
it 'registers an offense when adding a column to a wide table' do
|
2021-04-17 20:07:23 +05:30
|
|
|
offense = '`projects` is a wide table with several columns, [...]'
|
2020-04-22 19:07:51 +05:30
|
|
|
|
|
|
|
expect_offense(<<~RUBY)
|
|
|
|
def up
|
|
|
|
add_column(:projects, :another_column, :integer)
|
|
|
|
^^^^^^^^^^ #{offense}
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'registers an offense when adding a column with default to a wide table' do
|
2021-04-17 20:07:23 +05:30
|
|
|
offense = '`users` is a wide table with several columns, [...]'
|
2020-04-22 19:07:51 +05:30
|
|
|
|
|
|
|
expect_offense(<<~RUBY)
|
|
|
|
def up
|
2020-05-24 23:13:21 +05:30
|
|
|
add_column(:users, :another_column, :boolean, default: false)
|
|
|
|
^^^^^^^^^^ #{offense}
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'registers an offense when adding a reference' do
|
2021-04-17 20:07:23 +05:30
|
|
|
offense = '`ci_builds` is a wide table with several columns, [...]'
|
2020-04-22 19:07:51 +05:30
|
|
|
|
|
|
|
expect_offense(<<~RUBY)
|
|
|
|
def up
|
|
|
|
add_reference(:ci_builds, :issue, :boolean, index: true)
|
|
|
|
^^^^^^^^^^^^^ #{offense}
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'registers an offense when adding timestamps' do
|
2021-04-17 20:07:23 +05:30
|
|
|
offense = '`projects` is a wide table with several columns, [...]'
|
2020-04-22 19:07:51 +05:30
|
|
|
|
|
|
|
expect_offense(<<~RUBY)
|
|
|
|
def up
|
|
|
|
add_timestamps_with_timezone(:projects, null: false)
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{offense}
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'register no offense when using other method' do
|
|
|
|
expect_no_offenses(<<~RUBY)
|
|
|
|
def up
|
|
|
|
add_concurrent_index(:projects, :new_index)
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a regular table' do
|
|
|
|
it 'registers no offense for notes' do
|
|
|
|
expect_no_offenses(<<~RUBY)
|
|
|
|
def up
|
|
|
|
add_column(:notes, :another_column, :boolean)
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|