diff options
Diffstat (limited to 'rubocop/cop/migration/add_column_with_default.rb')
-rw-r--r-- | rubocop/cop/migration/add_column_with_default.rb | 45 |
1 files changed, 13 insertions, 32 deletions
diff --git a/rubocop/cop/migration/add_column_with_default.rb b/rubocop/cop/migration/add_column_with_default.rb index 8d1ab333dcf..d9f8fe62a86 100644 --- a/rubocop/cop/migration/add_column_with_default.rb +++ b/rubocop/cop/migration/add_column_with_default.rb @@ -12,52 +12,33 @@ module RuboCop WHITELISTED_TABLES = [:application_settings].freeze - MSG = '`add_column_with_default` with `allow_null: false` may cause prolonged lock situations and downtime, ' \ + 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 + def on_send(node) return unless in_migration?(node) - name = node.children[1] - - return unless name == :add_column_with_default + add_column_with_default?(node) do |table, options| + break if table_whitelisted?(table) || nulls_allowed?(options) - # Ignore whitelisted tables. - return if table_whitelisted?(node.children[2]) - - opts = node.children.last + add_offense(node, location: :selector) + end + end - return unless opts && opts.type == :hash + private - opts.each_node(:pair) do |pair| - if disallows_null_values?(pair) - add_offense(node, location: :selector) - end - end + def nulls_allowed?(options) + options.find { |opt| opt.key.value == :allow_null && opt.value.true_type? } end def table_whitelisted?(symbol) symbol && symbol.type == :sym && WHITELISTED_TABLES.include?(symbol.children[0]) end - - def disallows_null_values?(pair) - options = [hash_key_type(pair), hash_key_name(pair), hash_value(pair)] - - options == [:sym, :allow_null, :false] # rubocop:disable Lint/BooleanSymbol - end - - def hash_key_type(pair) - pair.children[0].type - end - - def hash_key_name(pair) - pair.children[0].children[0] - end - - def hash_value(pair) - pair.children[1].type - end end end end |