Class: RuboCop::Cop::Obsession::Rails::SafetyAssuredComment

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/obsession/rails/safety_assured_comment.rb

Overview

This cop checks uses of strong_migrations’ ‘safety_assured { … }` without a valid reason.

‘safety_assured { … }` should only be used after carefully following the instructions from the strong_migrations gem. Always add a `# Safe because <reason>` comment explaining how you assured the safety of the DB migration. The reason should be detailed and reviewed by a knowledgeable PR reviewer. Failure to follow instructions may bring your app down.

Examples:


# bad
class RemoveSourceUrlFromBlogPosts < ActiveRecord::Migration[8.0]
  def change
    safety_assured { remove_column :blog_posts, :source_url }
  end
end

# good
class RemoveSourceUrlFromBlogPosts < ActiveRecord::Migration[8.0]
  # Safe because this column was ignored with self.ignored_columns in PR #1234
  def change
    safety_assured { remove_column :blog_posts, :source_url }
  end
end

Constant Summary collapse

MSG =
'Add `# Safe because <reason>` comment above safety_assured. ' \
'An invalid reason may bring the site down.'

Instance Method Summary collapse

Instance Method Details

#on_block(node) ⇒ Object



42
43
44
45
46
47
# File 'lib/rubocop/cop/obsession/rails/safety_assured_comment.rb', line 42

def on_block(node)
  return if !safety_assured_block?(node)
  comment = processed_source.ast_with_comments[node].map(&:text).join("\n")

  add_offense(node) if !comment.match?(/^# Safe because( [^ ]+){4}/)
end