From abc9548f8afae6d666942f37715d2f7f8258d63b Mon Sep 17 00:00:00 2001 From: Douwe Maan Date: Wed, 8 Feb 2017 16:17:15 -0600 Subject: Add cop that checks if add_column_with_default is used with up/down methods --- rubocop/cop/migration/add_column.rb | 50 ++++++++++++++++++++++++ rubocop/cop/migration/add_column_with_default.rb | 34 ++++++++++++++++ rubocop/cop/migration/add_index.rb | 2 +- rubocop/cop/migration/column_with_default.rb | 50 ------------------------ rubocop/rubocop.rb | 3 +- 5 files changed, 87 insertions(+), 52 deletions(-) create mode 100644 rubocop/cop/migration/add_column.rb create mode 100644 rubocop/cop/migration/add_column_with_default.rb delete mode 100644 rubocop/cop/migration/column_with_default.rb (limited to 'rubocop') diff --git a/rubocop/cop/migration/add_column.rb b/rubocop/cop/migration/add_column.rb new file mode 100644 index 00000000000..d5c8eeeea1f --- /dev/null +++ b/rubocop/cop/migration/add_column.rb @@ -0,0 +1,50 @@ +module RuboCop + module Cop + module Migration + # Cop that checks if columns are added in a way that doesn't require + # downtime. + 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' + + def on_send(node) + return unless in_migration?(node) + + name = node.children[1] + + return unless name == :add_column + + # Ignore whitelisted tables. + return if table_whitelisted?(node.children[2]) + + opts = node.children.last + + return unless opts && opts.type == :hash + + opts.each_node(:pair) do |pair| + if hash_key_type(pair) == :sym && hash_key_name(pair) == :default + add_offense(node, :selector) + end + end + end + + def table_whitelisted?(symbol) + symbol && symbol.type == :sym && + WHITELISTED_TABLES.include?(symbol.children[0]) + end + + def hash_key_type(pair) + pair.children[0].type + end + + def hash_key_name(pair) + pair.children[0].children[0] + end + end + end + end +end 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/cop/migration/column_with_default.rb b/rubocop/cop/migration/column_with_default.rb deleted file mode 100644 index 97ee8b11044..00000000000 --- a/rubocop/cop/migration/column_with_default.rb +++ /dev/null @@ -1,50 +0,0 @@ -module RuboCop - module Cop - module Migration - # Cop that checks if columns are added in a way that doesn't require - # downtime. - class ColumnWithDefault < RuboCop::Cop::Cop - include MigrationHelpers - - WHITELISTED_TABLES = [:application_settings] - - MSG = 'add_column with a default value requires downtime, ' \ - 'use add_column_with_default instead' - - def on_send(node) - return unless in_migration?(node) - - name = node.children[1] - - return unless name == :add_column - - # Ignore whitelisted tables. - return if table_whitelisted?(node.children[2]) - - opts = node.children.last - - return unless opts && opts.type == :hash - - opts.each_node(:pair) do |pair| - if hash_key_type(pair) == :sym && hash_key_name(pair) == :default - add_offense(node, :selector) - end - end - end - - def table_whitelisted?(symbol) - symbol && symbol.type == :sym && - WHITELISTED_TABLES.include?(symbol.children[0]) - end - - def hash_key_type(pair) - pair.children[0].type - end - - def hash_key_name(pair) - pair.children[0].children[0] - end - end - end - end -end 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' -- cgit v1.2.1