Class: RuboCop::Cop::Rails::NotNullColumn
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Rails::NotNullColumn
- Includes:
- DatabaseTypeResolvable
- Defined in:
- lib/rubocop/cop/rails/not_null_column.rb
Overview
Checks for add_column calls with a NOT NULL constraint without a default value.
This cop only applies when adding a column to an existing table, since existing records will not have a value for the new column. New tables can freely use NOT NULL columns without defaults, since there are no records that could violate the constraint.
If you need to add a NOT NULL column to an existing table, you must add it as nullable first, back-fill the data, and then use ‘change_column_null`. Alternatively, you could add the column with a default first to have the database automatically backfill existing rows, and then use `change_column_default` to remove the default.
‘TEXT` cannot have a default value in MySQL. The cop 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 `TEXT` columns.
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
- #on_block(node) ⇒ Object (also: #on_numblock)
- #on_send(node) ⇒ Object
Methods included from DatabaseTypeResolvable
Instance Method Details
#on_block(node) ⇒ Object Also known as: on_numblock
85 86 87 |
# File 'lib/rubocop/cop/rails/not_null_column.rb', line 85 def on_block(node) check_change_table(node) end |
#on_send(node) ⇒ Object
80 81 82 83 |
# File 'lib/rubocop/cop/rails/not_null_column.rb', line 80 def on_send(node) check_add_column(node) check_add_reference(node) end |