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
8a388f21
Commit
8a388f21
authored
Apr 24, 2017
by
Robert Speicher
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add AddColumnWithDefaultToLargeTable cop
parent
8119f082
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
96 additions
and
0 deletions
+96
-0
rubocop/cop/migration/add_column_with_default_to_large_table.rb
...p/cop/migration/add_column_with_default_to_large_table.rb
+51
-0
rubocop/rubocop.rb
rubocop/rubocop.rb
+1
-0
spec/rubocop/cop/migration/add_column_with_default_to_large_table_spec.rb
.../migration/add_column_with_default_to_large_table_spec.rb
+44
-0
No files found.
rubocop/cop/migration/add_column_with_default_to_large_table.rb
0 → 100644
View file @
8a388f21
require_relative
'../../migration_helpers'
module
RuboCop
module
Cop
module
Migration
# This cop checks for `add_column_with_default` on a table that's been
# explicitly blacklisted because of its size.
#
# Even though this helper performs the update in batches to avoid
# downtime, using it with tables with millions of rows still causes a
# significant delay in the deploy process and is best avoided.
#
# See https://gitlab.com/gitlab-com/infrastructure/issues/1602 for more
# information.
class
AddColumnWithDefaultToLargeTable
<
RuboCop
::
Cop
::
Cop
include
MigrationHelpers
MSG
=
'Using `add_column_with_default` on the `%s` table will take a '
\
'long time to complete, and should be avoided unless absolutely '
\
'necessary'
.
freeze
LARGE_TABLES
=
%i[
events
issues
merge_requests
namespaces
notes
projects
routes
users
]
.
freeze
def_node_matcher
:add_column_with_default?
,
<<~
PATTERN
(send nil :add_column_with_default $(sym ...) ...)
PATTERN
def
on_send
(
node
)
return
unless
in_migration?
(
node
)
matched
=
add_column_with_default?
(
node
)
return
unless
matched
table
=
matched
.
to_a
.
first
return
unless
LARGE_TABLES
.
include?
(
table
)
add_offense
(
node
,
:expression
,
format
(
MSG
,
table
))
end
end
end
end
end
rubocop/rubocop.rb
View file @
8a388f21
require_relative
'cop/custom_error_class'
require_relative
'cop/custom_error_class'
require_relative
'cop/gem_fetcher'
require_relative
'cop/gem_fetcher'
require_relative
'cop/migration/add_column'
require_relative
'cop/migration/add_column'
require_relative
'cop/migration/add_column_with_default_to_large_table'
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'
...
...
spec/rubocop/cop/migration/add_column_with_default_to_large_table_spec.rb
0 → 100644
View file @
8a388f21
require
'spec_helper'
require
'rubocop'
require
'rubocop/rspec/support'
require_relative
'../../../../rubocop/cop/migration/add_column_with_default_to_large_table'
describe
RuboCop
::
Cop
::
Migration
::
AddColumnWithDefaultToLargeTable
do
include
CopHelper
subject
(
:cop
)
{
described_class
.
new
}
context
'in migration'
do
before
do
allow
(
cop
).
to
receive
(
:in_migration?
).
and_return
(
true
)
end
described_class
::
LARGE_TABLES
.
each
do
|
table
|
it
"registers an offense for the
#{
table
}
table"
do
inspect_source
(
cop
,
"add_column_with_default :
#{
table
}
, :column, default: true"
)
aggregate_failures
do
expect
(
cop
.
offenses
.
size
).
to
eq
(
1
)
expect
(
cop
.
offenses
.
map
(
&
:line
)).
to
eq
([
1
])
end
end
end
it
'registers no offense for non-blacklisted tables'
do
inspect_source
(
cop
,
"add_column_with_default :table, :column, default: true"
)
expect
(
cop
.
offenses
).
to
be_empty
end
end
context
'outside of migration'
do
it
'registers no offense'
do
table
=
described_class
::
LARGE_TABLES
.
sample
inspect_source
(
cop
,
"add_column_with_default :
#{
table
}
, :column, default: true"
)
expect
(
cop
.
offenses
).
to
be_empty
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