diff options
Diffstat (limited to 'rubocop/cop/migration')
-rw-r--r-- | rubocop/cop/migration/add_column.rb | 50 | ||||
-rw-r--r-- | rubocop/cop/migration/add_column_with_default.rb | 28 | ||||
-rw-r--r-- | rubocop/cop/migration/add_columns_to_wide_tables.rb | 1 | ||||
-rw-r--r-- | rubocop/cop/migration/reversible_add_column_with_default.rb | 35 |
4 files changed, 3 insertions, 111 deletions
diff --git a/rubocop/cop/migration/add_column.rb b/rubocop/cop/migration/add_column.rb deleted file mode 100644 index 0af90fb7fd1..00000000000 --- a/rubocop/cop/migration/add_column.rb +++ /dev/null @@ -1,50 +0,0 @@ -require_relative '../../migration_helpers' - -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 - - MSG = '`add_column` with a default value requires downtime, ' \ - 'use `add_column_with_default` instead'.freeze - - 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, location: :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 index 383653ef5a5..355319b0dfe 100644 --- a/rubocop/cop/migration/add_column_with_default.rb +++ b/rubocop/cop/migration/add_column_with_default.rb @@ -5,39 +5,17 @@ require_relative '../../migration_helpers' module RuboCop module Cop module Migration - # Cop that checks if columns are added in a way that doesn't require - # downtime. class AddColumnWithDefault < RuboCop::Cop::Cop include MigrationHelpers - MSG = '`add_column_with_default` without `allow_null: true` may cause prolonged lock situations and downtime, ' \ - 'see https://gitlab.com/gitlab-org/gitlab/issues/38060'.freeze - - def_node_matcher :add_column_with_default?, <<~PATTERN - (send _ :add_column_with_default $_ ... (hash $...)) - PATTERN + MSG = '`add_column_with_default` is deprecated, use `add_column` instead'.freeze def on_send(node) return unless in_migration?(node) - add_column_with_default?(node) do |table, options| - add_offense(node, location: :selector) if offensive?(table, options) - end - end - - private - - def offensive?(table, options) - table_blacklisted?(table) && !nulls_allowed?(options) - end - - def nulls_allowed?(options) - options.find { |opt| opt.key.value == :allow_null && opt.value.true_type? } - end + name = node.children[1] - def table_blacklisted?(symbol) - symbol && symbol.type == :sym && - BLACKLISTED_TABLES.include?(symbol.children[0]) + add_offense(node, location: :selector) if name == :add_column_with_default end end end diff --git a/rubocop/cop/migration/add_columns_to_wide_tables.rb b/rubocop/cop/migration/add_columns_to_wide_tables.rb index 4618e4ae890..2880783dc3e 100644 --- a/rubocop/cop/migration/add_columns_to_wide_tables.rb +++ b/rubocop/cop/migration/add_columns_to_wide_tables.rb @@ -14,7 +14,6 @@ module RuboCop BLACKLISTED_METHODS = %i[ add_column - add_column_with_default add_reference add_timestamps_with_timezone ].freeze diff --git a/rubocop/cop/migration/reversible_add_column_with_default.rb b/rubocop/cop/migration/reversible_add_column_with_default.rb deleted file mode 100644 index dd49188defa..00000000000 --- a/rubocop/cop/migration/reversible_add_column_with_default.rb +++ /dev/null @@ -1,35 +0,0 @@ -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 ReversibleAddColumnWithDefault < RuboCop::Cop::Cop - include MigrationHelpers - - def_node_matcher :add_column_with_default?, <<~PATTERN - (send nil? :add_column_with_default $...) - PATTERN - - def_node_matcher :defines_change?, <<~PATTERN - (def :change ...) - PATTERN - - 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`'.freeze - - def on_send(node) - return unless in_migration?(node) - return unless add_column_with_default?(node) - - node.each_ancestor(:def) do |def_node| - next unless defines_change?(def_node) - - add_offense(def_node, location: :name) - end - end - end - end - end -end |