Commit 766060bc authored by Yorick Peterse's avatar Yorick Peterse

Enforce use of add_concurrent_foreign_key

This adds a Rubocop rule to enforce the use of
add_concurrent_foreign_key instead of the regular add_foreign_key
method. This cop has been disabled for existing migrations so we don't
need to change those.
parent a97dcc07
...@@ -7,7 +7,7 @@ class AddGroupIdToLabels < ActiveRecord::Migration ...@@ -7,7 +7,7 @@ class AddGroupIdToLabels < ActiveRecord::Migration
def change def change
add_column :labels, :group_id, :integer add_column :labels, :group_id, :integer
add_foreign_key :labels, :namespaces, column: :group_id, on_delete: :cascade add_foreign_key :labels, :namespaces, column: :group_id, on_delete: :cascade # rubocop: disable Migration/AddConcurrentForeignKey
add_concurrent_index :labels, :group_id add_concurrent_index :labels, :group_id
end end
end end
...@@ -28,6 +28,6 @@ class AddPipelineIdToMergeRequestMetrics < ActiveRecord::Migration ...@@ -28,6 +28,6 @@ class AddPipelineIdToMergeRequestMetrics < ActiveRecord::Migration
def change def change
add_column :merge_request_metrics, :pipeline_id, :integer add_column :merge_request_metrics, :pipeline_id, :integer
add_concurrent_index :merge_request_metrics, :pipeline_id add_concurrent_index :merge_request_metrics, :pipeline_id
add_foreign_key :merge_request_metrics, :ci_commits, column: :pipeline_id, on_delete: :cascade add_foreign_key :merge_request_metrics, :ci_commits, column: :pipeline_id, on_delete: :cascade # rubocop: disable Migration/AddConcurrentForeignKey
end end
end end
...@@ -5,7 +5,7 @@ class AddProjectIdToSubscriptions < ActiveRecord::Migration ...@@ -5,7 +5,7 @@ class AddProjectIdToSubscriptions < ActiveRecord::Migration
def up def up
add_column :subscriptions, :project_id, :integer add_column :subscriptions, :project_id, :integer
add_foreign_key :subscriptions, :projects, column: :project_id, on_delete: :cascade add_foreign_key :subscriptions, :projects, column: :project_id, on_delete: :cascade # rubocop: disable Migration/AddConcurrentForeignKey
end end
def down def down
......
require_relative '../../migration_helpers'
module RuboCop
module Cop
module Migration
# Cop that checks if `add_concurrent_foreign_key` is used instead of
# `add_foreign_key`.
class AddConcurrentForeignKey < RuboCop::Cop::Cop
include MigrationHelpers
MSG = '`add_foreign_key` requires downtime, use `add_concurrent_foreign_key` instead'
def on_send(node)
return unless in_migration?(node)
name = node.children[1]
add_offense(node, :selector) if name == :add_foreign_key
end
def method_name(node)
node.children.first
end
end
end
end
end
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' require_relative 'cop/migration/add_column_with_default'
require_relative 'cop/migration/add_concurrent_foreign_key'
require_relative 'cop/migration/add_index' require_relative 'cop/migration/add_index'
require 'spec_helper'
require 'rubocop'
require 'rubocop/rspec/support'
require_relative '../../../../rubocop/cop/migration/add_concurrent_foreign_key'
describe RuboCop::Cop::Migration::AddConcurrentForeignKey do
include CopHelper
let(:cop) { described_class.new }
context 'outside of a migration' do
it 'does not register any offenses' do
inspect_source(cop, 'def up; add_foreign_key(:projects, :users, column: :user_id); end')
expect(cop.offenses).to be_empty
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_foreign_key' do
inspect_source(cop, 'def up; add_foreign_key(:projects, :users, column: :user_id); end')
aggregate_failures do
expect(cop.offenses.size).to eq(1)
expect(cop.offenses.map(&:line)).to eq([1])
end
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