diff options
Diffstat (limited to 'rubocop/cop/migration/prevent_global_enable_lock_retries_with_disable_ddl_transaction.rb')
-rw-r--r-- | rubocop/cop/migration/prevent_global_enable_lock_retries_with_disable_ddl_transaction.rb | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/rubocop/cop/migration/prevent_global_enable_lock_retries_with_disable_ddl_transaction.rb b/rubocop/cop/migration/prevent_global_enable_lock_retries_with_disable_ddl_transaction.rb new file mode 100644 index 00000000000..f5343f973e1 --- /dev/null +++ b/rubocop/cop/migration/prevent_global_enable_lock_retries_with_disable_ddl_transaction.rb @@ -0,0 +1,41 @@ +# 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 |