Commit 39d6cc1f authored by Rémy Coutable's avatar Rémy Coutable

Merge branch 'add-remove-column-cop' into 'master'

Add cop for use of remove_column

See merge request gitlab-org/gitlab-ce!15855
parents bd8b6518 1ab33b15
# rubocop:disable Migration/RemoveColumn
class RemoveDeprecatedIssuesTrackerColumnsFromProjects < ActiveRecord::Migration class RemoveDeprecatedIssuesTrackerColumnsFromProjects < ActiveRecord::Migration
def change def change
remove_column :projects, :issues_tracker, :string, default: 'gitlab', null: false remove_column :projects, :issues_tracker, :string, default: 'gitlab', null: false
......
# rubocop:disable Migration/RemoveColumn
class RemoveNotificationLevelFromUsers < ActiveRecord::Migration class RemoveNotificationLevelFromUsers < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers include Gitlab::Database::MigrationHelpers
......
# rubocop:disable Migration/RemoveColumn
# See http://doc.gitlab.com/ce/development/migration_style_guide.html # See http://doc.gitlab.com/ce/development/migration_style_guide.html
# for more information on how to write migrations for GitLab. # for more information on how to write migrations for GitLab.
......
# rubocop:disable Migration/RemoveColumn
# See http://doc.gitlab.com/ce/development/migration_style_guide.html # See http://doc.gitlab.com/ce/development/migration_style_guide.html
# for more information on how to write migrations for GitLab. # for more information on how to write migrations for GitLab.
......
# rubocop:disable Migration/RemoveColumn
# See http://doc.gitlab.com/ce/development/migration_style_guide.html # See http://doc.gitlab.com/ce/development/migration_style_guide.html
# for more information on how to write migrations for GitLab. # for more information on how to write migrations for GitLab.
......
# rubocop:disable Migration/RemoveColumn
# See http://doc.gitlab.com/ce/development/migration_style_guide.html # See http://doc.gitlab.com/ce/development/migration_style_guide.html
# for more information on how to write migrations for GitLab. # for more information on how to write migrations for GitLab.
......
# rubocop:disable Migration/RemoveColumn
# See http://doc.gitlab.com/ce/development/migration_style_guide.html # See http://doc.gitlab.com/ce/development/migration_style_guide.html
# for more information on how to write migrations for GitLab. # for more information on how to write migrations for GitLab.
......
# rubocop:disable Migration/RemoveColumn
class RemovePriorityFromLabels < ActiveRecord::Migration class RemovePriorityFromLabels < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers include Gitlab::Database::MigrationHelpers
......
# rubocop:disable Migration/RemoveColumn
class MigrateProjectStatistics < ActiveRecord::Migration class MigrateProjectStatistics < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers include Gitlab::Database::MigrationHelpers
......
# rubocop:disable Migration/RemoveColumn
# rubocop:disable RemoveIndex # rubocop:disable RemoveIndex
class RemoveOldProjectIdColumns < ActiveRecord::Migration class RemoveOldProjectIdColumns < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers include Gitlab::Database::MigrationHelpers
......
# rubocop:disable Migration/RemoveColumn
# rubocop:disable Migration/Datetime # rubocop:disable Migration/Datetime
class RemoveUnusedCiTablesAndColumns < ActiveRecord::Migration class RemoveUnusedCiTablesAndColumns < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers include Gitlab::Database::MigrationHelpers
......
# rubocop:disable Migration/RemoveColumn
# rubocop:disable Migration/UpdateLargeTable # rubocop:disable Migration/UpdateLargeTable
class RevertAddNotifiedOfOwnActivityToUsers < ActiveRecord::Migration class RevertAddNotifiedOfOwnActivityToUsers < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers include Gitlab::Database::MigrationHelpers
......
# rubocop:disable Migration/RemoveColumn
# See http://doc.gitlab.com/ce/development/migration_style_guide.html # See http://doc.gitlab.com/ce/development/migration_style_guide.html
# for more information on how to write migrations for GitLab. # for more information on how to write migrations for GitLab.
......
require_relative '../../migration_helpers'
module RuboCop
module Cop
module Migration
# Cop that checks if remove_column is used in a regular (not
# post-deployment) migration.
class RemoveColumn < RuboCop::Cop::Cop
include MigrationHelpers
MSG = '`remove_column` must only be used in post-deployment migrations'.freeze
def on_def(node)
def_method = node.children[0]
return unless in_migration?(node) && !in_post_deployment_migration?(node)
return unless def_method == :change || def_method == :up
node.each_descendant(:send) do |send_node|
send_method = send_node.children[1]
if send_method == :remove_column
add_offense(send_node, :selector)
end
end
end
end
end
end
end
...@@ -7,5 +7,11 @@ module RuboCop ...@@ -7,5 +7,11 @@ module RuboCop
dirname.end_with?('db/migrate', 'db/post_migrate') dirname.end_with?('db/migrate', 'db/post_migrate')
end end
def in_post_deployment_migration?(node)
dirname = File.dirname(node.location.expression.source_buffer.name)
dirname.end_with?('db/post_migrate')
end
end end
end end
...@@ -14,6 +14,7 @@ require_relative 'cop/migration/add_index' ...@@ -14,6 +14,7 @@ require_relative 'cop/migration/add_index'
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'
require_relative 'cop/migration/remove_column'
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 'spec_helper'
require 'rubocop'
require 'rubocop/rspec/support'
require_relative '../../../../rubocop/cop/migration/remove_column'
describe RuboCop::Cop::Migration::RemoveColumn do
include CopHelper
subject(:cop) { described_class.new }
def source(meth = 'change')
"def #{meth}; remove_column :table, :column; end"
end
context 'in a regular migration' do
before do
allow(cop).to receive(:in_migration?).and_return(true)
allow(cop).to receive(:in_post_deployment_migration?).and_return(false)
end
it 'registers an offense when remove_column is used in the change method' do
inspect_source(cop, source('change'))
aggregate_failures do
expect(cop.offenses.size).to eq(1)
expect(cop.offenses.map(&:line)).to eq([1])
end
end
it 'registers an offense when remove_column is used in the up method' do
inspect_source(cop, source('up'))
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 remove_column is used in the down method' do
inspect_source(cop, source('down'))
expect(cop.offenses.size).to eq(0)
end
end
context 'in a post-deployment migration' do
before do
allow(cop).to receive(:in_migration?).and_return(true)
allow(cop).to receive(:in_post_deployment_migration?).and_return(true)
end
it 'registers no offense' do
inspect_source(cop, source)
expect(cop.offenses.size).to eq(0)
end
end
context 'outside of a migration' do
it 'registers no offense' do
inspect_source(cop, source)
expect(cop.offenses.size).to eq(0)
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment