Module: RuboCop::Cop::Isucon::Mixin::DatabaseMethods

Included in:
JoinWithoutIndexMethods, NPlusOneQueryMethods, SelectAsteriskMethods, WhereWithoutIndexMethods
Defined in:
lib/rubocop/cop/isucon/mixin/database_methods.rb

Overview

Database util methods for RuboCop::Cop::Isucon::Mysql2

Instance Method Summary collapse

Instance Method Details

#connectionRuboCop::Isucon::DatabaseConnection

Returns:

Raises:



11
12
13
14
15
16
17
18
19
# File 'lib/rubocop/cop/isucon/mixin/database_methods.rb', line 11

def connection
  return @connection if @connection

  unless enabled_database?
    raise RuboCop::Isucon::DatabaseConfigurationError, "`Database` isn't configured in `.rubocop.yml`"
  end

  @connection = RuboCop::Isucon::DatabaseConnection.new(cop_config["Database"])
end

#enabled_database?Boolean

Returns:

  • (Boolean)


22
23
24
25
# File 'lib/rubocop/cop/isucon/mixin/database_methods.rb', line 22

def enabled_database?
  adapter = cop_config.dig("Database", "adapter")
  adapter && !adapter.empty?
end

#find_table_name_from_column_name(table_names:, column_name:) ⇒ String?

Parameters:

  • table_names (Array<String>)
  • column_name (String)

Returns:

  • (String, nil)


30
31
32
33
34
35
36
# File 'lib/rubocop/cop/isucon/mixin/database_methods.rb', line 30

def find_table_name_from_column_name(table_names:, column_name:)
  table_names.each do |table_name|
    column_names = connection.column_names(table_name)
    return table_name if column_names.include?(column_name)
  end
  nil
end