Sha256: e921e1f482cf7b0602cbf3a1a9460c982fdb6a295deb1c404c34ca224cd30348

Contents?: true

Size: 1.86 KB

Versions: 21

Compression:

Stored size: 1.86 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Rails
      # This cop checks for add_column call with NOT NULL constraint
      # in migration file.
      #
      # @example
      #   # bad
      #   add_column :users, :name, :string, null: false
      #   add_reference :products, :category, null: false
      #
      #   # good
      #   add_column :users, :name, :string, null: true
      #   add_column :users, :name, :string, null: false, default: ''
      #   add_reference :products, :category
      #   add_reference :products, :category, null: false, default: 1
      class NotNullColumn < Base
        MSG = 'Do not add a NOT NULL column without a default value.'
        RESTRICT_ON_SEND = %i[add_column add_reference].freeze

        def_node_matcher :add_not_null_column?, <<~PATTERN
          (send nil? :add_column _ _ _ (hash $...))
        PATTERN

        def_node_matcher :add_not_null_reference?, <<~PATTERN
          (send nil? :add_reference _ _ (hash $...))
        PATTERN

        def_node_matcher :null_false?, <<~PATTERN
          (pair (sym :null) (false))
        PATTERN

        def_node_matcher :default_option?, <<~PATTERN
          (pair (sym :default) !nil)
        PATTERN

        def on_send(node)
          check_add_column(node)
          check_add_reference(node)
        end

        private

        def check_add_column(node)
          pairs = add_not_null_column?(node)
          check_pairs(pairs)
        end

        def check_add_reference(node)
          pairs = add_not_null_reference?(node)
          check_pairs(pairs)
        end

        def check_pairs(pairs)
          return unless pairs
          return if pairs.any? { |pair| default_option?(pair) }

          null_false = pairs.find { |pair| null_false?(pair) }
          return unless null_false

          add_offense(null_false)
        end
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 2 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rails-2.14.2/lib/rubocop/cop/rails/not_null_column.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rails-2.14.2/lib/rubocop/cop/rails/not_null_column.rb
rubocop-rails-2.14.2 lib/rubocop/cop/rails/not_null_column.rb
rubocop-rails-2.14.1 lib/rubocop/cop/rails/not_null_column.rb
rubocop-rails-2.14.0 lib/rubocop/cop/rails/not_null_column.rb
rubocop-rails-2.13.2 lib/rubocop/cop/rails/not_null_column.rb
rubocop-rails-2.13.1 lib/rubocop/cop/rails/not_null_column.rb
rubocop-rails-2.13.0 lib/rubocop/cop/rails/not_null_column.rb
rubocop-rails-2.12.4 lib/rubocop/cop/rails/not_null_column.rb
rubocop-rails-2.12.3 lib/rubocop/cop/rails/not_null_column.rb
rubocop-rails-2.12.2 lib/rubocop/cop/rails/not_null_column.rb
rubocop-rails-2.12.1 lib/rubocop/cop/rails/not_null_column.rb
rubocop-rails-2.12.0 lib/rubocop/cop/rails/not_null_column.rb
rubocop-rails-2.11.3 lib/rubocop/cop/rails/not_null_column.rb
rubocop-rails-2.11.2 lib/rubocop/cop/rails/not_null_column.rb
rubocop-rails-2.11.1 lib/rubocop/cop/rails/not_null_column.rb
rubocop-rails-2.11.0 lib/rubocop/cop/rails/not_null_column.rb
rubocop-rails-2.10.1 lib/rubocop/cop/rails/not_null_column.rb
rubocop-rails-2.10.0 lib/rubocop/cop/rails/not_null_column.rb
rubocop-rails-2.9.1 lib/rubocop/cop/rails/not_null_column.rb