-
-
Notifications
You must be signed in to change notification settings - Fork 266
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add new Rails/ReversibleMigrationMethodDefinition
cop
#457
Merged
koic
merged 1 commit into
rubocop:master
from
hey-leon:feature/implemented-migration-cop
Apr 16, 2021
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
lib/rubocop/cop/rails/reversible_migration_method_definition.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Rails | ||
# This cop checks whether the migration implements | ||
# either a `change` method or both an `up` and a `down` | ||
# method. | ||
# | ||
# @example | ||
# # bad | ||
# class SomeMigration < ActiveRecord::Migration[6.0] | ||
# def up | ||
# # up migration | ||
# end | ||
# | ||
# # <----- missing down method | ||
# end | ||
# | ||
# class SomeMigration < ActiveRecord::Migration[6.0] | ||
# # <----- missing up method | ||
# | ||
# def down | ||
# # down migration | ||
# end | ||
# end | ||
# | ||
# # good | ||
# class SomeMigration < ActiveRecord::Migration[6.0] | ||
# def change | ||
# # reversible migration | ||
# end | ||
# end | ||
# | ||
# # good | ||
# class SomeMigration < ActiveRecord::Migration[6.0] | ||
# def up | ||
# # up migration | ||
# end | ||
# | ||
# def down | ||
# # down migration | ||
# end | ||
# end | ||
class ReversibleMigrationMethodDefinition < Base | ||
MSG = 'Migrations must contain either a `change` method, or ' \ | ||
'both an `up` and a `down` method.' | ||
|
||
def_node_matcher :migration_class?, <<~PATTERN | ||
(class | ||
(const nil? _) | ||
(send | ||
(const (const nil? :ActiveRecord) :Migration) | ||
:[] | ||
(float _)) | ||
_) | ||
PATTERN | ||
|
||
def_node_matcher :change_method?, <<~PATTERN | ||
[ #migration_class? `(def :change (args) _) ] | ||
PATTERN | ||
|
||
def_node_matcher :up_and_down_methods?, <<~PATTERN | ||
[ #migration_class? `(def :up (args) _) `(def :down (args) _) ] | ||
PATTERN | ||
|
||
def on_class(node) | ||
return if change_method?(node) || up_and_down_methods?(node) | ||
|
||
add_offense(node) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
spec/rubocop/cop/rails/reversible_migration_method_definition_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Rails::ReversibleMigrationMethodDefinition, :config do | ||
it 'does not register an offense with a change method' do | ||
expect_no_offenses(<<~RUBY) | ||
class SomeMigration < ActiveRecord::Migration[6.0] | ||
def change | ||
add_column :users, :email, :text, null: false | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense with only an up method' do | ||
expect_offense(<<~RUBY) | ||
class SomeMigration < ActiveRecord::Migration[6.0] | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Migrations must contain either a `change` method, or both an `up` and a `down` method. | ||
|
||
def up | ||
add_column :users, :email, :text, null: false | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense with only a down method' do | ||
expect_offense(<<~RUBY) | ||
class SomeMigration < ActiveRecord::Migration[6.0] | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Migrations must contain either a `change` method, or both an `up` and a `down` method. | ||
|
||
def down | ||
remove_column :users, :email | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense with an up and a down method' do | ||
expect_no_offenses(<<~RUBY) | ||
class SomeMigration < ActiveRecord::Migration[6.0] | ||
def up | ||
add_column :users, :email, :text, null: false | ||
end | ||
|
||
def down | ||
remove_column :users, :email | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
it "registers an offense with a typo'd change method" do | ||
expect_offense(<<~RUBY) | ||
class SomeMigration < ActiveRecord::Migration[6.0] | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Migrations must contain either a `change` method, or both an `up` and a `down` method. | ||
def chance | ||
add_column :users, :email, :text, null: false | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense with helper methods' do | ||
expect_no_offenses(<<~RUBY) | ||
class SomeMigration < ActiveRecord::Migration[6.0] | ||
def change | ||
add_users_column :email, :text, null: false | ||
end | ||
|
||
private | ||
|
||
def add_users_column(column_name, null: false) | ||
add_column :users, column_name, type, null: null | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers offenses correctly with any migration class' do | ||
expect_offense(<<~RUBY) | ||
class SomeMigration < ActiveRecord::Migration[5.2] | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Migrations must contain either a `change` method, or both an `up` and a `down` method. | ||
def chance | ||
add_column :users, :email, :text, null: false | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register offenses correctly with any migration class' do | ||
expect_no_offenses(<<~RUBY) | ||
class SomeMigration < ActiveRecord::Migration[5.2] | ||
def change | ||
add_column :users, :email, :text, null: false | ||
end | ||
end | ||
RUBY | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Include
typo.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, without it it flags all Ruby files :=)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😅 My bad thanks @rhymes