-
Notifications
You must be signed in to change notification settings - Fork 0
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
Added Migration/StandaloneAddReference
#54
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
82db9ec
Added `Migration/StandaloneAddReference`
Bhacaz 53d82c3
changelog
Bhacaz e6b06e1
update comment
Bhacaz 1a77aed
spec
Bhacaz 266c681
remove_belongs_to
Bhacaz 768a3b7
Merge remote-tracking branch 'origin/main' into limit_add_references
Bhacaz a776557
Comment
Bhacaz 895ab03
add_belongs_to
Bhacaz 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Migration | ||
# Prevent using `add_reference` and `remove_reference` outside of | ||
# a `change_table` block. `add_reference` create multiples `ALTER TABLE` | ||
# statements. Using `change_table` with `bulk: true` is more efficient. | ||
# | ||
# # bad | ||
# add_reference :products, :user, foreign_key: true | ||
# | ||
# # good | ||
# change_table :products, bulk: true do |t| | ||
# t.bigint :user_id, null: false | ||
# t.index :user_id | ||
# t.foreign_key :users, column: :user_id | ||
# end | ||
class StandaloneAddReference < Base | ||
MSG = 'Modifying references must be done in a change_table block.' | ||
|
||
RESTRICT_ON_SEND = %i[add_reference belongs_to remove_reference remove_belongs_to].freeze | ||
|
||
def on_send(node) | ||
reference_method = node.source_range.with(end_pos: node.child_nodes.first.source_range.begin_pos - 1) | ||
|
||
add_offense(reference_method) | ||
end | ||
end | ||
end | ||
end | ||
end |
36 changes: 36 additions & 0 deletions
36
spec/rubocop/cop/migration/standalone_add_reference_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,36 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Migration::StandaloneAddReference, :config do | ||
it 'registers an offense when modifing a references outside a change_table' do | ||
expect_offense(<<~RUBY) | ||
add_reference :products, :user | ||
^^^^^^^^^^^^^ Modifying references must be done in a change_table block. | ||
RUBY | ||
|
||
expect_offense(<<~RUBY) | ||
add_reference :products, :user, index: true | ||
^^^^^^^^^^^^^ Modifying references must be done in a change_table block. | ||
RUBY | ||
|
||
expect_offense(<<~RUBY) | ||
belongs_to :products, :user | ||
^^^^^^^^^^ Modifying references must be done in a change_table block. | ||
RUBY | ||
|
||
expect_offense(<<~RUBY) | ||
remove_reference :products, :user | ||
^^^^^^^^^^^^^^^^ Modifying references must be done in a change_table block. | ||
RUBY | ||
|
||
expect_offense(<<~RUBY) | ||
remove_belongs_to :products, :user | ||
^^^^^^^^^^^^^^^^^ Modifying references must be done in a change_table block. | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when not calling modifying references methods' do | ||
expect_no_offenses(<<~RUBY) | ||
add_index :users, :user_id | ||
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.
Est-ce que peut-être ici on doit ajouter le
add_belongs_to
et supprimer lebelongs_to
parce que seulement lebelongs_to
ne déclenche pas lecop
si on ajoute leadd_belongs_to
.@Bhacaz
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.
Good catch!