Class: RuboCop::Isucon::DatabaseConnection
- Inherits:
-
Object
- Object
- RuboCop::Isucon::DatabaseConnection
- Defined in:
- lib/rubocop/isucon/database_connection.rb
Overview
Manage database connection
Instance Method Summary collapse
- #column_names(table_name) ⇒ Array<String>
- #indexes(table_name) ⇒ Array<ActiveRecord::ConnectionAdapters::IndexDefinition>
-
#initialize(database_config) ⇒ DatabaseConnection
constructor
A new instance of DatabaseConnection.
-
#primary_keys(table_name) ⇒ Array<String>
Primary key's column names.
-
#unique_index_columns(table_name) ⇒ Array<Array<String>>
Column names of indexes.
Constructor Details
#initialize(database_config) ⇒ DatabaseConnection
Returns a new instance of DatabaseConnection.
9 10 11 12 13 14 |
# File 'lib/rubocop/isucon/database_connection.rb', line 9 def initialize(database_config) ActiveRecord::Base.establish_connection(database_config) @column_names_by_table = {} @indexes_by_table = {} @primary_keys_by_table = {} end |
Instance Method Details
#column_names(table_name) ⇒ Array<String>
18 19 20 |
# File 'lib/rubocop/isucon/database_connection.rb', line 18 def column_names(table_name) @column_names_by_table[table_name] ||= ActiveRecord::Base.connection.columns(table_name).map(&:name) end |
#indexes(table_name) ⇒ Array<ActiveRecord::ConnectionAdapters::IndexDefinition>
25 26 27 |
# File 'lib/rubocop/isucon/database_connection.rb', line 25 def indexes(table_name) @indexes_by_table[table_name] ||= ActiveRecord::Base.connection.indexes(table_name) end |
#primary_keys(table_name) ⇒ Array<String>
Returns primary key's column names.
37 38 39 |
# File 'lib/rubocop/isucon/database_connection.rb', line 37 def primary_keys(table_name) @primary_keys_by_table[table_name] ||= ActiveRecord::Base.connection.primary_keys(table_name) end |
#unique_index_columns(table_name) ⇒ Array<Array<String>>
Returns column names of indexes.
31 32 33 |
# File 'lib/rubocop/isucon/database_connection.rb', line 31 def unique_index_columns(table_name) indexes(table_name).select(&:unique).map(&:columns) end |