summaryrefslogtreecommitdiff
path: root/spec/rubocop/cop/migration
diff options
context:
space:
mode:
Diffstat (limited to 'spec/rubocop/cop/migration')
-rw-r--r--spec/rubocop/cop/migration/add_index_spec.rb4
-rw-r--r--spec/rubocop/cop/migration/add_limit_to_text_columns_spec.rb21
-rw-r--r--spec/rubocop/cop/migration/drop_table_spec.rb62
-rw-r--r--spec/rubocop/cop/migration/prevent_strings_spec.rb21
-rw-r--r--spec/rubocop/cop/migration/update_large_table_spec.rb91
5 files changed, 106 insertions, 93 deletions
diff --git a/spec/rubocop/cop/migration/add_index_spec.rb b/spec/rubocop/cop/migration/add_index_spec.rb
index 0c3f87e5bf8..ca1aadb381b 100644
--- a/spec/rubocop/cop/migration/add_index_spec.rb
+++ b/spec/rubocop/cop/migration/add_index_spec.rb
@@ -18,7 +18,7 @@ describe RuboCop::Cop::Migration::AddIndex do
end
it 'registers an offense when add_index is used' do
- expect_offense(<<~PATTERN.strip_indent)
+ expect_offense(<<~PATTERN)
def change
add_index :table, :column
^^^^^^^^^ `add_index` requires downtime, use `add_concurrent_index` instead
@@ -29,7 +29,7 @@ describe RuboCop::Cop::Migration::AddIndex do
context 'outside of migration' do
it 'registers no offense' do
- expect_no_offenses(<<~PATTERN.strip_indent)
+ expect_no_offenses(<<~PATTERN)
def change
add_index :table, :column
end
diff --git a/spec/rubocop/cop/migration/add_limit_to_text_columns_spec.rb b/spec/rubocop/cop/migration/add_limit_to_text_columns_spec.rb
index 514260a4306..39ca9ace73d 100644
--- a/spec/rubocop/cop/migration/add_limit_to_text_columns_spec.rb
+++ b/spec/rubocop/cop/migration/add_limit_to_text_columns_spec.rb
@@ -73,6 +73,27 @@ describe RuboCop::Cop::Migration::AddLimitToTextColumns do
end
end
+ context 'when text array columns are defined without a limit' do
+ it 'registers no offense' do
+ expect_no_offenses(<<~RUBY)
+ class TestTextLimits < ActiveRecord::Migration[6.0]
+ DOWNTIME = false
+
+ def up
+ create_table :test_text_limits, id: false do |t|
+ t.integer :test_id, null: false
+ t.text :name, array: true, default: [], null: false
+ end
+
+ add_column :test_text_limits, :email, :text, array: true
+ add_column_with_default :test_text_limits, :role, :text, default: [], array: true
+ change_column_type_concurrently :test_text_limits, :test_id, :text, array: true
+ end
+ end
+ RUBY
+ end
+ end
+
# Make sure that the cop is properly checking for an `add_text_limit`
# over the same {table, attribute} as the one that triggered the offence
context 'when the limit is defined for a same name attribute but different table' do
diff --git a/spec/rubocop/cop/migration/drop_table_spec.rb b/spec/rubocop/cop/migration/drop_table_spec.rb
new file mode 100644
index 00000000000..4fe7fc8c5a5
--- /dev/null
+++ b/spec/rubocop/cop/migration/drop_table_spec.rb
@@ -0,0 +1,62 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+require 'rubocop'
+require 'rubocop/rspec/support'
+
+require_relative '../../../../rubocop/cop/migration/drop_table'
+
+describe RuboCop::Cop::Migration::DropTable do
+ include CopHelper
+
+ subject(:cop) { described_class.new }
+
+ context 'when in deployment migration' do
+ before do
+ allow(cop).to receive(:in_deployment_migration?).and_return(true)
+ end
+
+ it 'registers an offense' do
+ expect_offense(<<~PATTERN)
+ def change
+ drop_table :table
+ ^^^^^^^^^^ #{described_class::MSG}
+
+ add_column(:users, :username, :text)
+
+ execute "DROP TABLE table"
+ ^^^^^^^ #{described_class::MSG}
+
+ execute "CREATE UNIQUE INDEX email_index ON users (email);"
+ end
+ PATTERN
+ end
+ end
+
+ context 'when in post-deployment migration' do
+ before do
+ allow(cop).to receive(:in_post_deployment_migration?).and_return(true)
+ end
+
+ it 'registers no offense' do
+ expect_no_offenses(<<~PATTERN)
+ def change
+ drop_table :table
+ execute "DROP TABLE table"
+ end
+ PATTERN
+ end
+ end
+
+ context 'when outside of migration' do
+ it 'registers no offense' do
+ expect_no_offenses(<<~PATTERN)
+ def change
+ drop_table :table
+ execute "DROP TABLE table"
+ end
+ PATTERN
+ end
+ end
+end
diff --git a/spec/rubocop/cop/migration/prevent_strings_spec.rb b/spec/rubocop/cop/migration/prevent_strings_spec.rb
index 2702ce1c090..d0e97874aed 100644
--- a/spec/rubocop/cop/migration/prevent_strings_spec.rb
+++ b/spec/rubocop/cop/migration/prevent_strings_spec.rb
@@ -90,6 +90,27 @@ describe RuboCop::Cop::Migration::PreventStrings do
end
end
+ context 'when the string data type is used for arrays' do
+ it 'registers no offense' do
+ expect_no_offenses(<<~RUBY)
+ class TestStringArrays < ActiveRecord::Migration[6.0]
+ DOWNTIME = false
+
+ def up
+ create_table :test_string_arrays, id: false do |t|
+ t.integer :test_id, null: false
+ t.string :name, array: true, default: [], null: false
+ end
+
+ add_column :test_string_arrays, :email, :string, array: true
+ add_column_with_default :test_string_arrays, :role, :string, default: [], array: true
+ change_column_type_concurrently :test_string_arrays, :test_id, :string, array: true
+ end
+ end
+ RUBY
+ end
+ end
+
context 'on down' do
it 'registers no offense' do
expect_no_offenses(<<~RUBY)
diff --git a/spec/rubocop/cop/migration/update_large_table_spec.rb b/spec/rubocop/cop/migration/update_large_table_spec.rb
deleted file mode 100644
index 30cd84108df..00000000000
--- a/spec/rubocop/cop/migration/update_large_table_spec.rb
+++ /dev/null
@@ -1,91 +0,0 @@
-# frozen_string_literal: true
-
-require 'spec_helper'
-
-require 'rubocop'
-require 'rubocop/rspec/support'
-
-require_relative '../../../../rubocop/cop/migration/update_large_table'
-
-describe RuboCop::Cop::Migration::UpdateLargeTable do
- include CopHelper
-
- subject(:cop) { described_class.new }
-
- context 'in migration' do
- before do
- allow(cop).to receive(:in_migration?).and_return(true)
- end
-
- shared_examples 'large tables' do |update_method|
- described_class::BLACKLISTED_TABLES.each do |table|
- it "registers an offense for the #{table} table" do
- inspect_source("#{update_method} :#{table}, :column, default: true")
-
- aggregate_failures do
- expect(cop.offenses.size).to eq(1)
- expect(cop.offenses.map(&:line)).to eq([1])
- end
- end
- end
- end
-
- context 'for the add_column_with_default method' do
- include_examples 'large tables', 'add_column_with_default'
- end
-
- context 'for the change_column_type_concurrently method' do
- include_examples 'large tables', 'change_column_type_concurrently'
- end
-
- context 'for the rename_column_concurrently method' do
- include_examples 'large tables', 'rename_column_concurrently'
- end
-
- context 'for the update_column_in_batches method' do
- include_examples 'large tables', 'update_column_in_batches'
- end
-
- it 'registers no offense for non-blacklisted tables' do
- inspect_source("add_column_with_default :table, :column, default: true")
-
- expect(cop.offenses).to be_empty
- end
-
- it 'registers no offense for non-blacklisted methods' do
- table = described_class::BLACKLISTED_TABLES.sample
-
- inspect_source("some_other_method :#{table}, :column, default: true")
-
- expect(cop.offenses).to be_empty
- end
- end
-
- context 'outside of migration' do
- let(:table) { described_class::BLACKLISTED_TABLES.sample }
-
- it 'registers no offense for add_column_with_default' do
- inspect_source("add_column_with_default :#{table}, :column, default: true")
-
- expect(cop.offenses).to be_empty
- end
-
- it 'registers no offense for change_column_type_concurrently' do
- inspect_source("change_column_type_concurrently :#{table}, :column, default: true")
-
- expect(cop.offenses).to be_empty
- end
-
- it 'registers no offense for rename_column_concurrently' do
- inspect_source("rename_column_concurrently :#{table}, :column, default: true")
-
- expect(cop.offenses).to be_empty
- end
-
- it 'registers no offense for update_column_concurrently' do
- inspect_source("update_column_concurrently :#{table}, :column, default: true")
-
- expect(cop.offenses).to be_empty
- end
- end
-end