Skip to content

Commit 104e5c9

Browse files
committed
[Fix #591] Add change_column check to Rails/ReversibleMigration
`change_column` is not on the list of the migration definitions supported by the `change` method. This is easy to miss as there are a couple `change_column_*` methods that are supported. When trying to rollback a migration that uses `change_column` in the `change` method, the rollback will fail. This change adds the check to `Rails/ReversibleMigration`.
1 parent baac68e commit 104e5c9

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## master (unreleased)
44

5+
### Changes
6+
7+
* [#591](https://github.com/rubocop/rubocop-rails/issues/591): Add `change_column` to `Rails/ReversibleMigration`. ([@mattmccormick][])
8+
59
## 2.12.4 (2021-10-16)
610

711
### Bug fixes
@@ -484,3 +488,4 @@
484488
[@theunraveler]: https://github.com/theunraveler
485489
[@pirj]: https://github.com/pirj
486490
[@vitormd]: https://github.com/vitormd
491+
[@mattmccormick]: https://github.com/mattmccormick

lib/rubocop/cop/rails/reversible_migration.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ class ReversibleMigration < Base
179179
MSG = '%<action>s is not reversible.'
180180

181181
def_node_matcher :irreversible_schema_statement_call, <<~PATTERN
182-
(send nil? ${:execute :remove_belongs_to} ...)
182+
(send nil? ${:execute :remove_belongs_to :change_column} ...)
183183
PATTERN
184184

185185
def_node_matcher :drop_table_call, <<~PATTERN

spec/rubocop/cop/rails/reversible_migration_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,16 @@ def change
110110
RUBY
111111
end
112112

113+
context 'change_column' do
114+
it_behaves_like 'offense', 'change_column', <<~RUBY
115+
change_column(:posts, :state, :string)
116+
RUBY
117+
118+
it_behaves_like 'offense', 'change_column', <<~RUBY
119+
change_column(:posts, :state, :string, null: false)
120+
RUBY
121+
end
122+
113123
context 'change_column_default' do
114124
it_behaves_like 'accepts',
115125
'change_column_default(with :from and :to)', <<-RUBY

0 commit comments

Comments
 (0)