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
Jérome Perrin
gitlab-ce
Commits
abc9548f
Commit
abc9548f
authored
Feb 08, 2017
by
Douwe Maan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add cop that checks if add_column_with_default is used with up/down methods
parent
b28d66c3
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
81 additions
and
5 deletions
+81
-5
rubocop/cop/migration/add_column.rb
rubocop/cop/migration/add_column.rb
+3
-3
rubocop/cop/migration/add_column_with_default.rb
rubocop/cop/migration/add_column_with_default.rb
+34
-0
rubocop/cop/migration/add_index.rb
rubocop/cop/migration/add_index.rb
+1
-1
rubocop/rubocop.rb
rubocop/rubocop.rb
+2
-1
spec/rubocop/cop/migration/add_column_with_default_spec.rb
spec/rubocop/cop/migration/add_column_with_default_spec.rb
+41
-0
No files found.
rubocop/cop/migration/
column_with_default
.rb
→
rubocop/cop/migration/
add_column
.rb
View file @
abc9548f
...
@@ -3,13 +3,13 @@ module RuboCop
...
@@ -3,13 +3,13 @@ module RuboCop
module
Migration
module
Migration
# Cop that checks if columns are added in a way that doesn't require
# Cop that checks if columns are added in a way that doesn't require
# downtime.
# downtime.
class
ColumnWithDefault
<
RuboCop
::
Cop
::
Cop
class
AddColumn
<
RuboCop
::
Cop
::
Cop
include
MigrationHelpers
include
MigrationHelpers
WHITELISTED_TABLES
=
[
:application_settings
]
WHITELISTED_TABLES
=
[
:application_settings
]
MSG
=
'
add_column
with a default value requires downtime, '
\
MSG
=
'
`add_column`
with a default value requires downtime, '
\
'use
add_column_with_default
instead'
'use
`add_column_with_default`
instead'
def
on_send
(
node
)
def
on_send
(
node
)
return
unless
in_migration?
(
node
)
return
unless
in_migration?
(
node
)
...
...
rubocop/cop/migration/add_column_with_default.rb
0 → 100644
View file @
abc9548f
require_relative
'../../migration_helpers'
module
RuboCop
module
Cop
module
Migration
# Cop that checks if `add_column_with_default` is used with `up`/`down` methods
# and not `change`.
class
AddColumnWithDefault
<
RuboCop
::
Cop
::
Cop
include
MigrationHelpers
MSG
=
'`add_column_with_default` is not reversible so you must manually define '
\
'the `up` and `down` methods in your migration class, using `remove_column` in `down`'
def
on_send
(
node
)
return
unless
in_migration?
(
node
)
name
=
node
.
children
[
1
]
return
unless
name
==
:add_column_with_default
node
.
each_ancestor
(
:def
)
do
|
def_node
|
next
unless
method_name
(
def_node
)
==
:change
add_offense
(
def_node
,
:name
)
end
end
def
method_name
(
node
)
node
.
children
.
first
end
end
end
end
end
rubocop/cop/migration/add_index.rb
View file @
abc9548f
...
@@ -5,7 +5,7 @@ module RuboCop
...
@@ -5,7 +5,7 @@ module RuboCop
class
AddIndex
<
RuboCop
::
Cop
::
Cop
class
AddIndex
<
RuboCop
::
Cop
::
Cop
include
MigrationHelpers
include
MigrationHelpers
MSG
=
'
add_index requires downtime, use add_concurrent_index
instead'
MSG
=
'
`add_index` requires downtime, use `add_concurrent_index`
instead'
def
on_def
(
node
)
def
on_def
(
node
)
return
unless
in_migration?
(
node
)
return
unless
in_migration?
(
node
)
...
...
rubocop/rubocop.rb
View file @
abc9548f
require_relative
'migration_helpers'
require_relative
'migration_helpers'
require_relative
'cop/migration/add_index'
require_relative
'cop/migration/add_index'
require_relative
'cop/migration/column_with_default'
require_relative
'cop/migration/add_column'
require_relative
'cop/migration/add_column_with_default'
require_relative
'cop/gem_fetcher'
require_relative
'cop/gem_fetcher'
spec/rubocop/cop/migration/add_column_with_default_spec.rb
0 → 100644
View file @
abc9548f
require
'spec_helper'
require
'rubocop'
require
'rubocop/rspec/support'
require_relative
'../../../../rubocop/cop/migration/add_column_with_default'
describe
RuboCop
::
Cop
::
Migration
::
AddColumnWithDefault
do
include
CopHelper
subject
(
:cop
)
{
described_class
.
new
}
context
'in migration'
do
before
do
allow
(
cop
).
to
receive
(
:in_migration?
).
and_return
(
true
)
end
it
'registers an offense when add_column_with_default is used inside a change method'
do
inspect_source
(
cop
,
'def change; add_column_with_default :table, :column, default: false; end'
)
aggregate_failures
do
expect
(
cop
.
offenses
.
size
).
to
eq
(
1
)
expect
(
cop
.
offenses
.
map
(
&
:line
)).
to
eq
([
1
])
end
end
it
'registers no offense when add_column_with_default is used inside an up method'
do
inspect_source
(
cop
,
'def up; add_column_with_default :table, :column, default: false; end'
)
expect
(
cop
.
offenses
.
size
).
to
eq
(
0
)
end
end
context
'outside of migration'
do
it
'registers no offense'
do
inspect_source
(
cop
,
'def change; add_column_with_default :table, :column, default: false; end'
)
expect
(
cop
.
offenses
.
size
).
to
eq
(
0
)
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