-
-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This cop enforces the standard index naming provided by Rails, allowing developers to enjoy a standardized schema. A bonus impact of following this convention is that fully duplicated indexes are prevented by default. Custom index names were once a required workaround for default names that were too long for various database implementations. That could happen because of long table names, long field names, or long lists of compound index keys. However, that problem has been fully resolved. The current automated naming approach will deterministically abbreviate indexes that would have been too long before.
- Loading branch information
1 parent
f66b8f7
commit 8abd536
Showing
6 changed files
with
403 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#1378](https://github.com/rubocop/rubocop-rails/pull/1378): Add new `Rails/IndexNames` cop. ([@corsonknowles][]) |
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,106 @@ | ||
# typed: false | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Rails | ||
# Checks for custom index names in migrations. | ||
# | ||
# @example | ||
# # bad | ||
# class ExampleMigration < ActiveRecord::Migration[7.1] | ||
# def change | ||
# change_table :users do |t| | ||
# t.index [:email], name: 'index_custom_name' | ||
# end | ||
# end | ||
# end | ||
# | ||
# # good | ||
# class ExampleMigration < ActiveRecord::Migration[7.1] | ||
# def change | ||
# create_table :users do |t| | ||
# t.index [:email] | ||
# end | ||
# end | ||
# end | ||
class IndexNames < Base | ||
include MigrationsHelper | ||
include RangeHelp | ||
extend AutoCorrector | ||
extend TargetRailsVersion | ||
|
||
minimum_target_rails_version 7.1 | ||
|
||
MSG = 'Avoid specifying a custom name for indexes. Let Rails handle the index name automatically.' | ||
RESTRICT_ON_SEND = %i[index add_index].freeze | ||
|
||
def on_send(node) | ||
return unless node.last_argument&.hash_type? | ||
return unless in_supported_migration?(node, 7.1) | ||
|
||
name_pair = node.last_argument.pairs.find { |pair| pair.key.value == :name || pair.key.value == 'name' } | ||
return unless name_pair | ||
|
||
add_offense(node) do |corrector| | ||
remove_name_argument(corrector, name_pair, node) | ||
end | ||
end | ||
|
||
private | ||
|
||
def remove_name_argument(corrector, name_pair, node) | ||
range = name_argument_range(name_pair, node) | ||
corrector.remove(range) | ||
remove_extra_comma_and_space(corrector, range, node) | ||
end | ||
|
||
def name_argument_range(name_pair, node) | ||
hash_node = name_pair.parent | ||
if hash_node.pairs.size == 1 | ||
# If name: is the only argument, remove the entire hash | ||
range_between(node.arguments[-2].source_range.end_pos, node.source_range.end_pos) | ||
else | ||
# Remove the name: argument and any preceding comma and space | ||
start_pos = previous_comma_pos(name_pair) || name_pair.source_range.begin_pos | ||
range_between(start_pos, name_pair.source_range.end_pos) | ||
end | ||
end | ||
|
||
def previous_comma_pos(pair) | ||
source = pair.parent.source | ||
pair_start = pair.source_range.begin_pos - pair.parent.source_range.begin_pos | ||
previous_content = source[0...pair_start] | ||
comma_index = previous_content.rindex(',') | ||
comma_index ? pair.parent.source_range.begin_pos + comma_index : nil | ||
end | ||
|
||
def remove_extra_comma_and_space(corrector, removed_range, node) | ||
remaining_source = remaining_source(node, removed_range) | ||
next_relevant_content = remaining_source.lstrip | ||
range_to_remove = if next_relevant_content.start_with?(',') | ||
space_after_comma(removed_range, next_relevant_content) | ||
else | ||
leading_space(remaining_source, removed_range) | ||
end | ||
|
||
corrector.remove(range_to_remove) | ||
end | ||
|
||
def remaining_source(node, removed_range) | ||
node.source[removed_range.end_pos - node.source_range.begin_pos..] | ||
end | ||
|
||
def space_after_comma(removed_range, next_relevant_content) | ||
space_after_comma = next_relevant_content[1..].match(/\A\s*/)[0] | ||
range_between(removed_range.end_pos, removed_range.end_pos + 1 + space_after_comma.length) | ||
end | ||
|
||
def leading_space(remaining_source, removed_range) | ||
leading_space = remaining_source.match(/\A\s*/)[0] | ||
range_between(removed_range.end_pos, removed_range.end_pos + leading_space.length) | ||
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
Oops, something went wrong.