-
-
Notifications
You must be signed in to change notification settings - Fork 14
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
New cop rule: IrreversibleMigration
#43
Merged
cyberdelia
merged 11 commits into
rubocop:master
from
AlasdairWallaceMackie:irreversible-migration-change
Oct 23, 2024
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f7cf608
Starting point code and spec
AlasdairWallaceMackie de64e87
Node traversal
AlasdairWallaceMackie 8edc603
Update config YAML
AlasdairWallaceMackie cc040a3
Code cleanup, additional specs
AlasdairWallaceMackie ae33334
Rename class
AlasdairWallaceMackie 902db3b
Rename files
AlasdairWallaceMackie 28c00f6
Fix require
AlasdairWallaceMackie 7a86094
Rename file
AlasdairWallaceMackie eff740d
Format MSG string with method name
AlasdairWallaceMackie bc97248
Change `add_runtime_dependency` to `add_dependency`
AlasdairWallaceMackie 5001f1e
Small change to `MSG`
AlasdairWallaceMackie 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,57 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Sequel | ||
# IrreversibleMigration looks for methods inside a `change` block that cannot be reversed. | ||
class IrreversibleMigration < Base | ||
# https://sequel.jeremyevans.net/rdoc/files/doc/migration_rdoc.html#label-A+Basic+Migration | ||
VALID_CHANGE_METHODS = %i[ | ||
create_table | ||
create_join_table | ||
create_view | ||
add_column | ||
add_index | ||
rename_column | ||
rename_table | ||
alter_table | ||
add_column | ||
add_constraint | ||
add_foreign_key | ||
add_primary_key | ||
add_index | ||
add_full_text_index | ||
add_spatial_index | ||
rename_column | ||
set_column_allow_null | ||
].freeze | ||
|
||
MSG = 'Avoid using `%<name>s` inside a `change` block. Use `up` & `down` blocks instead.' | ||
PRIMARY_KEY_MSG = 'Avoid using `add_primary_key` with an array argument inside a `change` block.' | ||
|
||
def on_block(node) | ||
return unless node.method_name == :change | ||
|
||
body = node.body | ||
return unless body | ||
|
||
body.each_node(:send) { |child_node| validate_node(child_node) } | ||
end | ||
|
||
private | ||
|
||
def validate_node(node) | ||
name = node.method_name | ||
|
||
add_offense(node.loc.selector, message: format(MSG, name: name)) unless VALID_CHANGE_METHODS.include?(name) | ||
|
||
return unless name == :add_primary_key | ||
|
||
return unless node.arguments.any?(&:array_type?) | ||
|
||
add_offense(node.loc.selector, message: PRIMARY_KEY_MSG) | ||
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
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,76 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Sequel::IrreversibleMigration do | ||
subject(:cop) { described_class.new } | ||
|
||
context 'when inside a change block' do | ||
let(:invalid_source) do | ||
<<~SOURCE | ||
change do | ||
alter_table(:stores) do | ||
drop_column(:products, :name) | ||
drop_index(:products, :price) | ||
end | ||
end | ||
SOURCE | ||
end | ||
|
||
let(:valid_source) do | ||
<<~SOURCE | ||
change do | ||
alter_table(:stores) do | ||
add_primary_key(:id) | ||
add_column(:products, :name) | ||
add_index(:products, :price) | ||
end | ||
end | ||
SOURCE | ||
end | ||
|
||
it 'registers an offense when there is an invalid method' do | ||
offenses = inspect_source(invalid_source) | ||
expect(offenses.size).to eq(2) | ||
end | ||
|
||
it 'does not register an offense with valid methods' do | ||
offenses = inspect_source(valid_source) | ||
expect(offenses).to be_empty | ||
end | ||
|
||
describe 'and an array is passed into `add_primary_key`' do | ||
let(:source) do | ||
<<~SOURCE | ||
change do | ||
alter_table(:stores) do | ||
add_primary_key([:owner_id, :name]) | ||
end | ||
end | ||
SOURCE | ||
end | ||
|
||
it 'registers an offense' do | ||
offenses = inspect_source(source) | ||
expect(offenses.size).to eq(1) | ||
end | ||
end | ||
end | ||
|
||
context 'when inside an up block' do | ||
let(:source) do | ||
<<~SOURCE | ||
up do | ||
alter_table(:stores) do | ||
add_primary_key([:owner_id, :name]) | ||
add_column(:products, :name) | ||
drop_index(:products, :price) | ||
end | ||
end | ||
SOURCE | ||
end | ||
|
||
it 'does not register an offense with any methods' do | ||
offenses = inspect_source(source) | ||
expect(offenses).to be_empty | ||
end | ||
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.
Had to make this change due to failed CI build