-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added cop
Migration/ChangeTableReferences
(#47)
- Loading branch information
Showing
5 changed files
with
92 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Migration | ||
# Prevent using `t.references` or `t.belongs_to` in a change_table. | ||
# Internally, `t.references` use multiple `ALTER TABLE` statements. | ||
# Since Rails cannot transform automatically `t.references` inside | ||
# a `change_table bulk: true` we can manually create the equivalent | ||
# `ALTER TABLE` statement using `t.bigint`, `t.index` and `t.foreign_key`. | ||
# | ||
# #bad | ||
# change_table :subscriptions, bulk: true do |t| | ||
# t.references :user, null: false, foreign_key: true | ||
# end | ||
# | ||
# #good | ||
# change_table :subscriptions, bulk: true do |t| | ||
# t.bigint :user_id, null: false | ||
# t.index :user_id | ||
# t.foreign_key :users, column: :user_id | ||
# end | ||
class ChangeTableReferences < Base | ||
MSG = 'Use a combination of `t.bigint`, `t.index` and `t.foreign_key` in a change_table.' | ||
|
||
# @!method add_references_in_block?(node) | ||
def_node_search :add_references_in_block?, <<~PATTERN | ||
(send lvar /references|belongs_to/ ...) | ||
PATTERN | ||
|
||
# @!method change_table?(node) | ||
def_node_search :change_table?, <<~PATTERN | ||
(send nil? :change_table ...) | ||
PATTERN | ||
|
||
def on_block(node) | ||
return unless change_table?(node) | ||
|
||
references_node = node.children.detect { |n| add_references_in_block?(n) } | ||
return unless references_node | ||
|
||
arguments = references_node.child_nodes[1] | ||
references_methods_range = references_node.source_range.with(end_pos: arguments.source_range.begin_pos - 1) | ||
add_offense(references_methods_range) | ||
end | ||
end | ||
end | ||
end | ||
end |
35 changes: 35 additions & 0 deletions
35
spec/rubocop/cop/migration/change_table_references_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,35 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Migration::ChangeTableReferences, :config do | ||
it 'registers an offense when using references in a change_table' do | ||
expect_offense(<<~RUBY) | ||
change_table :subscriptions, bulk: true do |t| | ||
t.references :user, null: false, foreign_key: true | ||
^^^^^^^^^^^^ Use a combination of `t.bigint`, `t.index` and `t.foreign_key` in a change_table. | ||
end | ||
RUBY | ||
|
||
expect_offense(<<~RUBY) | ||
change_table :subscriptions, bulk: true do |t| | ||
t.belongs_to :user, null: false, foreign_key: true | ||
^^^^^^^^^^^^ Use a combination of `t.bigint`, `t.index` and `t.foreign_key` in a change_table. | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using references in a create_table' do | ||
expect_no_offenses(<<~RUBY) | ||
create_table :subscriptions do |t| | ||
t.belongs_to :user, null: false, foreign_key: true | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when not using references in a create_table' do | ||
expect_no_offenses(<<~RUBY) | ||
change_table :subscriptions do |t| | ||
t.index :user_id | ||
end | ||
RUBY | ||
end | ||
end |