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
0
Merge Requests
0
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
Boxiang Sun
gitlab-ce
Commits
187e6c8d
Commit
187e6c8d
authored
May 22, 2017
by
Rémy Coutable
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
New Migration/UpdateColumnInBatches cop
Signed-off-by:
Rémy Coutable
<
remy@rymai.me
>
parent
c9e9c2ff
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
141 additions
and
2 deletions
+141
-2
rubocop/cop/migration/update_column_in_batches.rb
rubocop/cop/migration/update_column_in_batches.rb
+43
-0
rubocop/migration_helpers.rb
rubocop/migration_helpers.rb
+3
-2
rubocop/rubocop.rb
rubocop/rubocop.rb
+1
-0
spec/rubocop/cop/migration/update_column_in_batches_spec.rb
spec/rubocop/cop/migration/update_column_in_batches_spec.rb
+94
-0
No files found.
rubocop/cop/migration/update_column_in_batches.rb
0 → 100644
View file @
187e6c8d
require_relative
'../../migration_helpers'
module
RuboCop
module
Cop
module
Migration
# Cop that checks if a spec file exists for any migration using
# `update_column_in_batches`.
class
UpdateColumnInBatches
<
RuboCop
::
Cop
::
Cop
include
MigrationHelpers
MSG
=
'Migration running `update_column_in_batches` must have a spec file at'
\
' `%s`.'
.
freeze
def
on_send
(
node
)
return
unless
in_migration?
(
node
)
return
unless
node
.
children
[
1
]
==
:update_column_in_batches
spec_path
=
spec_filename
(
node
)
unless
File
.
exist?
(
File
.
expand_path
(
spec_path
,
rails_root
))
add_offense
(
node
,
:expression
,
format
(
MSG
,
spec_path
))
end
end
private
def
spec_filename
(
node
)
source_name
=
node
.
location
.
expression
.
source_buffer
.
name
path
=
Pathname
.
new
(
source_name
).
relative_path_from
(
rails_root
)
dirname
=
File
.
dirname
(
path
)
.
sub
(
%r{
\A
db/(migrate|post_migrate)}
,
'spec/migrations'
)
filename
=
File
.
basename
(
source_name
,
'.rb'
).
sub
(
%r{
\A\d
+_}
,
''
)
File
.
join
(
dirname
,
"
#{
filename
}
_spec.rb"
)
end
def
rails_root
Pathname
.
new
(
File
.
expand_path
(
'../../..'
,
__dir__
))
end
end
end
end
end
rubocop/migration_helpers.rb
View file @
187e6c8d
...
@@ -3,8 +3,9 @@ module RuboCop
...
@@ -3,8 +3,9 @@ module RuboCop
module
MigrationHelpers
module
MigrationHelpers
# Returns true if the given node originated from the db/migrate directory.
# Returns true if the given node originated from the db/migrate directory.
def
in_migration?
(
node
)
def
in_migration?
(
node
)
File
.
dirname
(
node
.
location
.
expression
.
source_buffer
.
name
).
dirname
=
File
.
dirname
(
node
.
location
.
expression
.
source_buffer
.
name
)
end_with?
(
'db/migrate'
)
dirname
.
end_with?
(
'db/migrate'
,
'db/post_migrate'
)
end
end
end
end
end
end
rubocop/rubocop.rb
View file @
187e6c8d
...
@@ -8,3 +8,4 @@ require_relative 'cop/migration/add_index'
...
@@ -8,3 +8,4 @@ require_relative 'cop/migration/add_index'
require_relative
'cop/migration/remove_concurrent_index'
require_relative
'cop/migration/remove_concurrent_index'
require_relative
'cop/migration/remove_index'
require_relative
'cop/migration/remove_index'
require_relative
'cop/migration/reversible_add_column_with_default'
require_relative
'cop/migration/reversible_add_column_with_default'
require_relative
'cop/migration/update_column_in_batches'
spec/rubocop/cop/migration/update_column_in_batches_spec.rb
0 → 100644
View file @
187e6c8d
require
'spec_helper'
require
'rubocop'
require
'rubocop/rspec/support'
require_relative
'../../../../rubocop/cop/migration/update_column_in_batches'
describe
RuboCop
::
Cop
::
Migration
::
UpdateColumnInBatches
do
let
(
:cop
)
{
described_class
.
new
}
let
(
:tmp_rails_root
)
{
Rails
.
root
.
join
(
'tmp'
,
'rails_root'
)
}
let
(
:migration_code
)
do
<<-
END
def up
update_column_in_batches(:projects, :name, "foo") do |table, query|
query.where(table[:name].eq(nil))
end
end
END
end
before
do
allow
(
cop
).
to
receive
(
:rails_root
).
and_return
(
tmp_rails_root
)
end
after
do
FileUtils
.
rm_rf
(
tmp_rails_root
)
end
context
'outside of a migration'
do
it
'does not register any offenses'
do
inspect_source
(
cop
,
migration_code
)
expect
(
cop
.
offenses
).
to
be_empty
end
end
let
(
:spec_filepath
)
{
tmp_rails_root
.
join
(
'spec'
,
'migrations'
,
'my_super_migration_spec.rb'
)
}
shared_context
'with a migration file'
do
before
do
FileUtils
.
mkdir_p
(
File
.
dirname
(
migration_filepath
))
@migration_file
=
File
.
new
(
migration_filepath
,
'w+'
)
end
after
do
@migration_file
.
close
end
end
shared_examples
'a migration file with no spec file'
do
include_context
'with a migration file'
let
(
:relative_spec_filepath
)
{
Pathname
.
new
(
spec_filepath
).
relative_path_from
(
tmp_rails_root
)
}
it
'registers an offense when using update_column_in_batches'
do
inspect_source
(
cop
,
migration_code
,
@migration_file
)
aggregate_failures
do
expect
(
cop
.
offenses
.
size
).
to
eq
(
1
)
expect
(
cop
.
offenses
.
map
(
&
:line
)).
to
eq
([
2
])
expect
(
cop
.
offenses
.
first
.
message
).
to
include
(
"`
#{
relative_spec_filepath
}
`"
)
end
end
end
shared_examples
'a migration file with a spec file'
do
include_context
'with a migration file'
before
do
FileUtils
.
mkdir_p
(
File
.
dirname
(
spec_filepath
))
@spec_file
=
File
.
new
(
spec_filepath
,
'w+'
)
end
after
do
@spec_file
.
close
end
it
'does not register any offenses'
do
inspect_source
(
cop
,
migration_code
,
@migration_file
)
expect
(
cop
.
offenses
).
to
be_empty
end
end
context
'in a migration'
do
let
(
:migration_filepath
)
{
tmp_rails_root
.
join
(
'db'
,
'migrate'
,
'20121220064453_my_super_migration.rb'
)
}
it_behaves_like
'a migration file with no spec file'
it_behaves_like
'a migration file with a spec file'
end
context
'in a post migration'
do
let
(
:migration_filepath
)
{
tmp_rails_root
.
join
(
'db'
,
'post_migrate'
,
'20121220064453_my_super_migration.rb'
)
}
it_behaves_like
'a migration file with no spec file'
it_behaves_like
'a migration file with a spec file'
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