Skip to content

Commit 14027cf

Browse files
authored
Merge pull request #592 from mattmccormick/mccormick-prevent-change-column-in-change-method
[Fix #591] Add `change_column` check to `Rails/ReversibleMigration`
2 parents baac68e + 798b39e commit 14027cf

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#591](https://github.com/rubocop/rubocop-rails/issues/591): Add `change_column` check to `Rails/ReversibleMigration`. ([@mattmccormick][])
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* Add `remove_reference` check to `Rails/ReversibleMigration`. ([@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? ${:change_column :execute :remove_belongs_to :remove_reference} ...)
183183
PATTERN
184184

185185
def_node_matcher :drop_table_call, <<~PATTERN

spec/rubocop/cop/rails/reversible_migration_spec.rb

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

113+
context 'change_column' do
114+
it_behaves_like 'accepts', 'up_only', <<~RUBY
115+
up_only { change_column(:posts, :state, :string) }
116+
RUBY
117+
118+
it_behaves_like 'offense', 'change_column', <<~RUBY
119+
change_column(:posts, :state, :string)
120+
RUBY
121+
122+
it_behaves_like 'offense', 'change_column', <<~RUBY
123+
change_column(:posts, :state, :string, null: false)
124+
RUBY
125+
end
126+
113127
context 'change_column_default' do
114128
it_behaves_like 'accepts',
115129
'change_column_default(with :from and :to)', <<-RUBY
@@ -157,6 +171,20 @@ def change
157171
RUBY
158172
end
159173

174+
context 'remove_belongs_to' do
175+
it_behaves_like 'accepts', 'up_only', <<~RUBY
176+
up_only { remove_belongs_to(:products, :user, index: false) }
177+
RUBY
178+
179+
it_behaves_like 'offense', 'remove_belongs_to', <<~RUBY
180+
remove_belongs_to(:products, :user, index: false)
181+
RUBY
182+
183+
it_behaves_like 'offense', 'remove_belongs_to', <<~RUBY
184+
remove_belongs_to(:products, :supplier, polymorphic: true)
185+
RUBY
186+
end
187+
160188
context 'remove_column' do
161189
it_behaves_like 'accepts', 'remove_column(with type)', <<~RUBY
162190
remove_column(:suppliers, :qualification, :string)
@@ -185,6 +213,20 @@ def change
185213
RUBY
186214
end
187215

216+
context 'remove_reference' do
217+
it_behaves_like 'accepts', 'up_only', <<~RUBY
218+
up_only { remove_reference(:products, :user, index: false) }
219+
RUBY
220+
221+
it_behaves_like 'offense', 'remove_reference', <<~RUBY
222+
remove_reference(:products, :user, index: false)
223+
RUBY
224+
225+
it_behaves_like 'offense', 'remove_reference', <<~RUBY
226+
remove_reference(:products, :supplier, polymorphic: true)
227+
RUBY
228+
end
229+
188230
context 'change_table' do
189231
it_behaves_like 'accepts', 'change_table(with reversible calls)', <<~RUBY
190232
change_table :users do |t|

0 commit comments

Comments
 (0)