This repository has been archived by the owner on Aug 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
105 additions
and
7 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
machine: | ||
ruby: | ||
version: 2.3.0 | ||
dependencies: | ||
pre: | ||
- git clone --depth 1 git://github.com/bbatsov/rubocop.git vendor/rubocop |
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 was deleted.
Oops, something went wrong.
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,36 @@ | ||
module RuboCop | ||
module Cop | ||
module Egad | ||
class AddIndexNonConcurrently < Cop | ||
MSG = 'Use `algorithm: :concurrently` to avoid locking database.'.freeze | ||
|
||
def_node_matcher :add_index_or_add_reference?, <<-PATTERN | ||
(send nil {:add_index :add_reference} ...) | ||
PATTERN | ||
|
||
def on_send(node) | ||
if add_index_or_add_reference?(node) | ||
options = options_hash(node) | ||
if !options[:algorithm].present? || options[:algorithm] != "concurrently" | ||
add_offense(node, :expression, MSG) | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def options_hash(node) | ||
options = {} | ||
node.children.each do |c| | ||
if c.is_a?(AST::Node) && c.type == :hash | ||
c.each_pair do |key_node, value_node| | ||
options[key_node.children.first.to_sym] = value_node.children.first.to_s | ||
end | ||
end | ||
end | ||
options | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
describe RuboCop::Cop::Egad::AddIndexNonConcurrently do | ||
subject(:cop) { described_class.new } | ||
|
||
before do | ||
inspect_source(cop, source) | ||
end | ||
|
||
context "add_index" do | ||
context "basic source, non-concurrent" do | ||
let(:source) { "add_index :users, :some_column" } | ||
|
||
it "registers an offense" do | ||
expect(cop.offenses.size).to eq(1) | ||
expect(cop.offenses.first.message).to eq("Use `algorithm: :concurrently` to avoid locking database.") | ||
expect(cop.highlights).to eq([source]) | ||
end | ||
end | ||
|
||
context "complex source, non-concurrent" do | ||
let(:source) { "add_index :users, [:some_column, :another], unique: true, algorithm: :btree, something: {nested: 1}" } | ||
|
||
it "registers an offense" do | ||
expect(cop.offenses.size).to eq(1) | ||
expect(cop.offenses.first.message).to eq("Use `algorithm: :concurrently` to avoid locking database.") | ||
expect(cop.highlights).to eq([source]) | ||
end | ||
end | ||
|
||
context "basic source, concurrent" do | ||
let(:source) { "add_index :users, :some_column, algorithm: :concurrently" } | ||
|
||
it "doesn't register an offense" do | ||
expect(cop.offenses).to be_empty | ||
end | ||
end | ||
|
||
context "complex source, concurrent" do | ||
let(:source) { "add_index :users, [:some_column, :another], unique: true, algorithm: 'concurrently', something: {nested: 1}" } | ||
|
||
it "doesn't register an offense" do | ||
expect(cop.offenses).to be_empty | ||
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