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

Migration helper module #45

Merged
merged 8 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 8 additions & 2 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ Sequel/IrreversibleMigration:
Warns against using certain methods inside a migration file's `change` block that would cause the migration to become irreversible.
Reference: https://www.rubydoc.info/gems/rubocop-sequel/RuboCop/Cop/Sequel/IrreversibleMigration
Enabled: true
VersionAdded: 0.3.4
VersionChanged: 0.3.4
VersionAdded: 0.3.5
VersionChanged: 0.3.6

Sequel/JSONColumn:
Description: >-
Expand Down Expand Up @@ -52,3 +52,9 @@ Sequel/SaveChanges:
SafeAutoCorrect: false
VersionAdded: '0.0.1'
VersionChanged: '0.3.3'

Sequel/Helpers/Migration:
Description: Contains helper methods for detecting if a node is inside a `Sequel.migration` block
Reference: https://www.rubydoc.info/gems/rubocop-sequel/RuboCop/Cop/Sequel/Helpers/Migration
VersionAdded: 0.3.6
VersionChanged: 0.3.6
2 changes: 2 additions & 0 deletions lib/rubocop-sequel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

RuboCop::Sequel::Inject.defaults!

require 'rubocop/cop/sequel/helpers/migration'

require 'rubocop/cop/sequel/concurrent_index'
require 'rubocop/cop/sequel/irreversible_migration'
require 'rubocop/cop/sequel/json_column'
Expand Down
25 changes: 25 additions & 0 deletions lib/rubocop/cop/sequel/helpers/migration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

module RuboCop
module Cop
module Sequel
module Helpers
# Migration contains helper methods for detecting if a node is inside a `Sequel.migration` block
module Migration
extend NodePattern::Macros

def_node_matcher :sequel_migration_block?, <<~MATCHER
(block
(send
(const nil? :Sequel) :migration ...)
...)
MATCHER

def within_sequel_migration?(node)
node.each_ancestor(:block).any? { |ancestor| sequel_migration_block?(ancestor) }
end
end
end
end
end
end
7 changes: 5 additions & 2 deletions lib/rubocop/cop/sequel/irreversible_migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ module Cop
module Sequel
# IrreversibleMigration looks for methods inside a `change` block that cannot be reversed.
class IrreversibleMigration < Base
include RuboCop::Cop::Sequel::Helpers::Migration
Copy link
Collaborator

Choose a reason for hiding this comment

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

You may want to apply this to other migration related cops?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done! I've updated the PR description with these additional changes, and also created a spec helper module to wrap test code in a Sequel.migration block.


# https://sequel.jeremyevans.net/rdoc/files/doc/migration_rdoc.html#label-A+Basic+Migration
VALID_CHANGE_METHODS = %i[
create_table
Expand All @@ -26,11 +28,12 @@ class IrreversibleMigration < Base
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.'
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
return unless within_sequel_migration?(node)

body = node.body
return unless body
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.5'
STRING = '0.3.6'
cyberdelia marked this conversation as resolved.
Show resolved Hide resolved
end
end
end
2 changes: 1 addition & 1 deletion rubocop-sequel.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Gem::Specification.new do |gem|
gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
gem.name = 'rubocop-sequel'
gem.require_paths = ['lib']
gem.version = '0.3.5'
gem.version = '0.3.6'
gem.metadata['rubygems_mfa_required'] = 'true'

gem.required_ruby_version = '>= 2.5'
Expand Down
55 changes: 38 additions & 17 deletions spec/rubocop/cop/sequel/irreversible_migration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,26 @@
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)
Sequel.migration do
change do
alter_table(:stores) do
drop_column(:products, :name)
drop_index(:products, :price)
end
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)
Sequel.migration do
change do
alter_table(:stores) do
add_primary_key(:id)
add_column(:products, :name)
add_index(:products, :price)
end
end
end
SOURCE
Expand All @@ -40,9 +44,11 @@
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])
Sequel.migration do
change do
alter_table(:stores) do
add_primary_key([:owner_id, :name])
end
end
end
SOURCE
Expand All @@ -58,11 +64,13 @@
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)
Sequel.migration do
up do
alter_table(:stores) do
add_primary_key([:owner_id, :name])
add_column(:products, :name)
drop_index(:products, :price)
end
end
end
SOURCE
Expand All @@ -73,4 +81,17 @@
expect(offenses).to be_empty
end
end

context 'when a change block is used outside of a Sequel migration' do
let(:source) do
<<~SOURCE
it { expect { subject }.to change { document_count(user_id) }.by(-1) }
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Test case taken directly from the reported issue:
#44

SOURCE
end

it 'does not register an offense with any methods' do
offenses = inspect_source(source)
expect(offenses).to be_empty
end
end
end
Loading