Class: RuboCop::Cop::Migration::ChangeColumn
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Migration::ChangeColumn
- Defined in:
- lib/rubocop/cop/migration/change_column.rb
Overview
Avoid changing column type that is in use.
Changing the type of a column causes the entire table to be rewritten. During this time, reads and writes are blocked in Postgres, and writes are blocked in MySQL and MariaDB.
Some changes don’t require a table rewrite and are safe in PostgreSQL:
| Type | Safe Changes |
|---|---|
cidr |
Changing to inet |
citext |
Changing to text if not indexed, changing to string with no :limit if not indexed |
datetime |
Increasing or removing :precision, changing to timestamptz when session time zone is UTC in Postgres 12+ |
decimal |
Increasing :precision at same :scale, removing :precision and :scale |
interval |
Increasing or removing :precision |
numeric |
Increasing :precision at same :scale, removing :precision and :scale |
string |
Increasing or removing :limit, changing to text, changing citext if not indexed |
text |
Changing to string with no :limit, changing to citext if not indexed |
time |
Increasing or removing :precision |
timestamptz |
Increasing or removing :limit, changing to datetime when session time zone is UTC in Postgres 12+ |
And some in MySQL and MariaDB:
| Type | Safe Changes |
|---|---|
string |
Increasing :limit from under 255 up to 255, increasing :limit from over 255 to the max |
A safer approach is to:
- Create a new column
- Write to both columns
- Backfill data from the old column to the new column
- Move reads from the old column to the new column
- Stop writing to the old column
- Drop the old column
Constant Summary collapse
- MSG =
'Avoid changing column type that is in use.'- RESTRICT_ON_SEND =
i[ change change_column ].freeze
Instance Method Summary collapse
- #on_send(node) ⇒ void (also: #on_csend)
Instance Method Details
#on_send(node) ⇒ void Also known as: on_csend
This method returns an undefined value.
74 75 76 77 78 |
# File 'lib/rubocop/cop/migration/change_column.rb', line 74 def on_send(node) return unless bad?(node) add_offense(node) end |