Skip to content
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

Bugfix: IrreversibleMigration will now ignore methods within create_table block #47

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Sequel/IrreversibleMigration:
Reference: https://www.rubydoc.info/gems/rubocop-sequel/RuboCop/Cop/Sequel/IrreversibleMigration
Enabled: true
VersionAdded: 0.3.5
VersionChanged: 0.3.6
VersionChanged: 0.3.7

Sequel/JSONColumn:
Description: >-
Expand Down
24 changes: 19 additions & 5 deletions lib/rubocop/cop/sequel/irreversible_migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,29 @@ def on_block(node)
private

def validate_node(node)
name = node.method_name
return if within_create_table_block?(node)

add_offense(node.loc.selector, message: format(MSG, name: name)) unless VALID_CHANGE_METHODS.include?(name)
add_offense(node.loc.selector, message: format(MSG, name: node.method_name)) unless valid_change_method?(node)

return unless name == :add_primary_key
add_offense(node.loc.selector, message: PRIMARY_KEY_MSG) if invalid_primary_key_method?(node)
end

def valid_change_method?(node)
VALID_CHANGE_METHODS.include?(node.method_name)
end

def invalid_primary_key_method?(node)
return false unless node.method_name == :add_primary_key

node.arguments.any?(&:array_type?)
end

return unless node.arguments.any?(&:array_type?)
def within_create_table_block?(node)
return true if node.method_name == :create_table

add_offense(node.loc.selector, message: PRIMARY_KEY_MSG)
node.each_ancestor(:block).any? do |ancestor|
ancestor.method_name == :create_table
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/sequel/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module RuboCop
module Sequel
# This module holds the RuboCop Sequel version information.
module Version
STRING = '0.3.6'
STRING = '0.3.7'
end
end
end
48 changes: 48 additions & 0 deletions spec/rubocop/cop/sequel/irreversible_migration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,54 @@
expect(offenses).to be_empty
end

describe 'and using a create_table block' do
let(:source) do
<<~SOURCE
change do
create_table(:artists) do
primary_key :id
String :name, null: false
end
end
Comment on lines +45 to +50
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Example taken from Sequel docs

image

SOURCE
end

it 'does not register any offenses' do
offenses = inspect_source_within_migration(source)
expect(offenses).to be_empty
end
end

describe 'and using a create_table block and alter_table block' do
let(:source) do
<<~SOURCE
change do
alter_table(:stores) do
drop_column(:products, :name)
drop_index(:products, :price)
end

add_column :books, :name, String

create_table(:artists) do
primary_key :id
String :name, null: false
end
end
SOURCE
end
let(:expected_methods) { %w[drop_column drop_index] }

it 'only registers offenses from within alter_table block' do
messages = inspect_source_within_migration(source).map(&:message)
expected_methods_present = expected_methods.all? do |method|
messages.any? { |message| message.include?(method) }
end

expect(expected_methods_present).to be(true)
end
end

describe 'and an array is passed into `add_primary_key`' do
let(:source) do
<<~SOURCE
Expand Down