class RuboCop::Cop::Migration::UnsafeMigration

Constants

ERROR_NOTICE
SCHEMA_STATEMENTS

List of all public methods that can be used within a migration method, like `add_index`, `rename_table`, etc.

SCHEMA_STATEMENTS_PATTERN

Public Instance Methods

investigate(processed_source) click to toggle source
# File lib/rubocop/cop/migration/unsafe_migration.rb, line 41
def investigate(processed_source)
  ast = processed_source.ast
  return if !ast || !migration_class?(ast)
  ast.each_child_node do |child_node|
    migration_methods = migration_method_match(child_node)
    migration_methods&.each { |method_node| investigate_migration_method(method_node) }
  end
end

Private Instance Methods

investigate_migration_method(method_node) click to toggle source
# File lib/rubocop/cop/migration/unsafe_migration.rb, line 52
def investigate_migration_method(method_node)
  schema_statement_match(method_node) do |statement_node, method_name, args_nodes|
    # TODO: Better/safer way to do this?
    args_source = "[#{args_nodes.map(&:source).join(', ')}]"
    args = eval(args_source)

    checker = RuboCop::Migration::StrongMigrationsChecker.new
    error = checker.check_operation(method_name, *args)
    if error
      error += "\n#{ERROR_NOTICE}"
      add_offense(statement_node, :expression, error)
    end
  end
end