Commit d15720c9 authored by Dylan Griffith's avatar Dylan Griffith

Merge branch 'prevent-disable-ddl-transaction-with-enable-lock-retries' into 'master'

Add rubocop rule for enable_lock_retries

See merge request gitlab-org/gitlab!77668
parents 77a3516e 8da4c1e6
# frozen_string_literal: true
require_relative '../../migration_helpers'
module RuboCop
module Cop
module Migration
# Cop that prevents usage of `enable_lock_retries!` within the `disable_ddl_transaction!` method.
class PreventGlobalEnableLockRetriesWithDisableDdlTransaction < RuboCop::Cop::Cop
include MigrationHelpers
MSG = '`enable_lock_retries!` cannot be used with `disable_ddl_transaction!`. Use the `with_lock_retries` helper method to define retriable code blocks.'
def_node_matcher :enable_lock_retries?, <<~PATTERN
(send _ :enable_lock_retries! ...)
PATTERN
def_node_matcher :disable_ddl_transaction?, <<~PATTERN
(send _ :disable_ddl_transaction! ...)
PATTERN
def on_begin(node)
return unless in_migration?(node)
has_enable_lock_retries = false
has_disable_ddl_transaction = false
node.each_descendant(:send) do |send_node|
has_enable_lock_retries = true if enable_lock_retries?(send_node)
has_disable_ddl_transaction = true if disable_ddl_transaction?(send_node)
if has_enable_lock_retries && has_disable_ddl_transaction
add_offense(send_node, message: MSG)
break
end
end
end
end
end
end
end
# frozen_string_literal: true
require 'fast_spec_helper'
require_relative '../../../../rubocop/cop/migration/prevent_global_enable_lock_retries_with_disable_ddl_transaction'
RSpec.describe RuboCop::Cop::Migration::PreventGlobalEnableLockRetriesWithDisableDdlTransaction do
subject(:cop) { described_class.new }
context 'when in migration' do
before do
allow(cop).to receive(:in_migration?).and_return(true)
end
it 'registers an offense when `enable_lock_retries` and `disable_ddl_transaction` is used together' do
code = <<~RUBY
class SomeMigration < ActiveRecord::Migration[6.0]
enable_lock_retries!
disable_ddl_transaction!
end
RUBY
expect_offense(<<~RUBY, node: code, msg: described_class::MSG)
class SomeMigration < ActiveRecord::Migration[6.0]
enable_lock_retries!
disable_ddl_transaction!
^^^^^^^^^^^^^^^^^^^^^^^^ %{msg}
end
RUBY
end
it 'registers no offense when `enable_lock_retries!` is used' do
expect_no_offenses(<<~RUBY)
class SomeMigration < ActiveRecord::Migration[6.0]
enable_lock_retries!
end
RUBY
end
it 'registers no offense when `disable_ddl_transaction!` is used' do
expect_no_offenses(<<~RUBY)
class SomeMigration < ActiveRecord::Migration[6.0]
disable_ddl_transaction!
end
RUBY
end
end
context 'when outside of migration' do
it 'registers no offense' do
expect_no_offenses(<<~RUBY)
class SomeMigration
enable_lock_retries!
disable_ddl_transaction!
end
RUBY
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