diff options
author | Douwe Maan <douwe@selenight.nl> | 2017-02-08 16:17:15 -0600 |
---|---|---|
committer | Douwe Maan <douwe@selenight.nl> | 2017-02-08 16:47:48 -0600 |
commit | abc9548f8afae6d666942f37715d2f7f8258d63b (patch) | |
tree | f7854d80a006ba220f744d565490ff2f56b43bb6 | |
parent | b28d66c38d95e779157e9f68f68e7ca3b0ba2521 (diff) | |
download | gitlab-ce-abc9548f8afae6d666942f37715d2f7f8258d63b.tar.gz |
Add cop that checks if add_column_with_default is used with up/down methods
-rw-r--r-- | rubocop/cop/migration/add_column.rb (renamed from rubocop/cop/migration/column_with_default.rb) | 6 | ||||
-rw-r--r-- | rubocop/cop/migration/add_column_with_default.rb | 34 | ||||
-rw-r--r-- | rubocop/cop/migration/add_index.rb | 2 | ||||
-rw-r--r-- | rubocop/rubocop.rb | 3 | ||||
-rw-r--r-- | spec/rubocop/cop/migration/add_column_with_default_spec.rb | 41 |
5 files changed, 81 insertions, 5 deletions
diff --git a/rubocop/cop/migration/column_with_default.rb b/rubocop/cop/migration/add_column.rb index 97ee8b11044..d5c8eeeea1f 100644 --- a/rubocop/cop/migration/column_with_default.rb +++ b/rubocop/cop/migration/add_column.rb @@ -3,13 +3,13 @@ module RuboCop module Migration # Cop that checks if columns are added in a way that doesn't require # downtime. - class ColumnWithDefault < RuboCop::Cop::Cop + class AddColumn < RuboCop::Cop::Cop include MigrationHelpers WHITELISTED_TABLES = [:application_settings] - MSG = 'add_column with a default value requires downtime, ' \ - 'use add_column_with_default instead' + MSG = '`add_column` with a default value requires downtime, ' \ + 'use `add_column_with_default` instead' def on_send(node) return unless in_migration?(node) diff --git a/rubocop/cop/migration/add_column_with_default.rb b/rubocop/cop/migration/add_column_with_default.rb new file mode 100644 index 00000000000..747d7caf1ef --- /dev/null +++ b/rubocop/cop/migration/add_column_with_default.rb @@ -0,0 +1,34 @@ +require_relative '../../migration_helpers' + +module RuboCop + module Cop + module Migration + # Cop that checks if `add_column_with_default` is used with `up`/`down` methods + # and not `change`. + class AddColumnWithDefault < RuboCop::Cop::Cop + include MigrationHelpers + + MSG = '`add_column_with_default` is not reversible so you must manually define ' \ + 'the `up` and `down` methods in your migration class, using `remove_column` in `down`' + + def on_send(node) + return unless in_migration?(node) + + name = node.children[1] + + return unless name == :add_column_with_default + + node.each_ancestor(:def) do |def_node| + next unless method_name(def_node) == :change + + add_offense(def_node, :name) + end + end + + def method_name(node) + node.children.first + end + end + end + end +end diff --git a/rubocop/cop/migration/add_index.rb b/rubocop/cop/migration/add_index.rb index d9247a1f7ea..506af97866f 100644 --- a/rubocop/cop/migration/add_index.rb +++ b/rubocop/cop/migration/add_index.rb @@ -5,7 +5,7 @@ module RuboCop class AddIndex < RuboCop::Cop::Cop include MigrationHelpers - MSG = 'add_index requires downtime, use add_concurrent_index instead' + MSG = '`add_index` requires downtime, use `add_concurrent_index` instead' def on_def(node) return unless in_migration?(node) diff --git a/rubocop/rubocop.rb b/rubocop/rubocop.rb index 7f20754ee51..3c596bff7ea 100644 --- a/rubocop/rubocop.rb +++ b/rubocop/rubocop.rb @@ -1,4 +1,5 @@ require_relative 'migration_helpers' require_relative 'cop/migration/add_index' -require_relative 'cop/migration/column_with_default' +require_relative 'cop/migration/add_column' +require_relative 'cop/migration/add_column_with_default' require_relative 'cop/gem_fetcher' diff --git a/spec/rubocop/cop/migration/add_column_with_default_spec.rb b/spec/rubocop/cop/migration/add_column_with_default_spec.rb new file mode 100644 index 00000000000..6b9b6b19650 --- /dev/null +++ b/spec/rubocop/cop/migration/add_column_with_default_spec.rb @@ -0,0 +1,41 @@ +require 'spec_helper' + +require 'rubocop' +require 'rubocop/rspec/support' + +require_relative '../../../../rubocop/cop/migration/add_column_with_default' + +describe RuboCop::Cop::Migration::AddColumnWithDefault do + include CopHelper + + subject(:cop) { described_class.new } + + context 'in migration' do + before do + allow(cop).to receive(:in_migration?).and_return(true) + end + + it 'registers an offense when add_column_with_default is used inside a change method' do + inspect_source(cop, 'def change; add_column_with_default :table, :column, default: false; end') + + aggregate_failures do + expect(cop.offenses.size).to eq(1) + expect(cop.offenses.map(&:line)).to eq([1]) + end + end + + it 'registers no offense when add_column_with_default is used inside an up method' do + inspect_source(cop, 'def up; add_column_with_default :table, :column, default: false; end') + + expect(cop.offenses.size).to eq(0) + end + end + + context 'outside of migration' do + it 'registers no offense' do + inspect_source(cop, 'def change; add_column_with_default :table, :column, default: false; end') + + expect(cop.offenses.size).to eq(0) + end + end +end |