Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
e3ff3909
Commit
e3ff3909
authored
Aug 01, 2018
by
Andreas Brandl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add rubocop check for add_reference to require index.
parent
3d2a3e57
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
105 additions
and
1 deletion
+105
-1
db/migrate/20160317092222_add_moved_to_to_issue.rb
db/migrate/20160317092222_add_moved_to_to_issue.rb
+1
-1
rubocop/cop/migration/add_reference.rb
rubocop/cop/migration/add_reference.rb
+49
-0
rubocop/rubocop.rb
rubocop/rubocop.rb
+1
-0
spec/rubocop/cop/migration/add_reference_spec.rb
spec/rubocop/cop/migration/add_reference_spec.rb
+54
-0
No files found.
db/migrate/20160317092222_add_moved_to_to_issue.rb
View file @
e3ff3909
class
AddMovedToToIssue
<
ActiveRecord
::
Migration
class
AddMovedToToIssue
<
ActiveRecord
::
Migration
def
change
def
change
add_reference
:issues
,
:moved_to
,
references: :issues
add_reference
:issues
,
:moved_to
,
references: :issues
# rubocop:disable Migration/AddReference
end
end
end
end
rubocop/cop/migration/add_reference.rb
0 → 100644
View file @
e3ff3909
# frozen_string_literal: true
require_relative
'../../migration_helpers'
module
RuboCop
module
Cop
module
Migration
# Cop that checks if a foreign key constraint is added and require a index for it
class
AddReference
<
RuboCop
::
Cop
::
Cop
include
MigrationHelpers
MSG
=
'`add_reference` requires `index: true`'
def
on_send
(
node
)
return
unless
in_migration?
(
node
)
name
=
node
.
children
[
1
]
return
unless
name
==
:add_reference
opts
=
node
.
children
.
last
add_offense
(
node
,
location: :selector
)
unless
opts
&&
opts
.
type
==
:hash
index_present
=
false
opts
.
each_node
(
:pair
)
do
|
pair
|
index_present
||=
index_enabled?
(
pair
)
end
add_offense
(
node
,
location: :selector
)
unless
index_present
end
private
def
index_enabled?
(
pair
)
hash_key_type
(
pair
)
==
:sym
&&
hash_key_name
(
pair
)
==
:index
&&
pair
.
children
[
1
].
true_type?
end
def
hash_key_type
(
pair
)
pair
.
children
[
0
].
type
end
def
hash_key_name
(
pair
)
pair
.
children
[
0
].
children
[
0
]
end
end
end
end
end
rubocop/rubocop.rb
View file @
e3ff3909
...
@@ -11,6 +11,7 @@ require_relative 'cop/migration/add_column'
...
@@ -11,6 +11,7 @@ require_relative 'cop/migration/add_column'
require_relative
'cop/migration/add_concurrent_foreign_key'
require_relative
'cop/migration/add_concurrent_foreign_key'
require_relative
'cop/migration/add_concurrent_index'
require_relative
'cop/migration/add_concurrent_index'
require_relative
'cop/migration/add_index'
require_relative
'cop/migration/add_index'
require_relative
'cop/migration/add_reference'
require_relative
'cop/migration/add_timestamps'
require_relative
'cop/migration/add_timestamps'
require_relative
'cop/migration/datetime'
require_relative
'cop/migration/datetime'
require_relative
'cop/migration/hash_index'
require_relative
'cop/migration/hash_index'
...
...
spec/rubocop/cop/migration/add_reference_spec.rb
0 → 100644
View file @
e3ff3909
require
'spec_helper'
require
'rubocop'
require
'rubocop/rspec/support'
require_relative
'../../../../rubocop/cop/migration/add_reference'
describe
RuboCop
::
Cop
::
Migration
::
AddReference
do
include
CopHelper
let
(
:cop
)
{
described_class
.
new
}
context
'outside of a migration'
do
it
'does not register any offenses'
do
expect_no_offenses
(
<<~
RUBY
)
def up
add_reference(:projects, :users)
end
RUBY
end
end
context
'in a migration'
do
before
do
allow
(
cop
).
to
receive
(
:in_migration?
).
and_return
(
true
)
end
it
'registers an offense when using add_reference without index'
do
expect_offense
(
<<~
RUBY
)
call do
add_reference(:projects, :users)
^^^^^^^^^^^^^ `add_reference` requires `index: true`
end
RUBY
end
it
'registers an offense when using add_reference index disabled'
do
expect_offense
(
<<~
RUBY
)
def up
add_reference(:projects, :users, index: false)
^^^^^^^^^^^^^ `add_reference` requires `index: true`
end
RUBY
end
it
'does not register an offense when using add_reference with index enabled'
do
expect_no_offenses
(
<<~
RUBY
)
def up
add_reference(:projects, :users, index: true)
end
RUBY
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment