Class: RuboCop::Cop::Rails::NotNullColumn

Inherits:
Base
  • Object
show all
Includes:
DatabaseTypeResolvable
Defined in:
lib/rubocop/cop/rails/not_null_column.rb

Overview

Checks for add_column call with NOT NULL constraint in migration file.

‘TEXT` can have default values in PostgreSQL, but not in MySQL. It will automatically detect an adapter from `development` environment in `config/database.yml` or the environment variable `DATABASE_URL` when the `Database` option is not set. If the database is MySQL, this cop ignores offenses for the `TEXT`.

Examples:

# 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

Constant Summary collapse

MSG =
'Do not add a NOT NULL column without a default value.'
RESTRICT_ON_SEND =
%i[add_column add_reference].freeze

Constants included from DatabaseTypeResolvable

DatabaseTypeResolvable::MYSQL, DatabaseTypeResolvable::POSTGRESQL

Instance Method Summary collapse

Methods included from DatabaseTypeResolvable

#database

Instance Method Details

#on_send(node) ⇒ Object



46
47
48
49
# File 'lib/rubocop/cop/rails/not_null_column.rb', line 46

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