Class: LolDba::IndexFinder
- Inherits:
-
Object
- Object
- LolDba::IndexFinder
- Defined in:
- lib/lol_dba/index_finding/index_finder.rb
Class Method Summary collapse
- .check_for_indexes ⇒ Object
- .eager_load_if_needed ⇒ Object
- .existing_indexes(table_name) ⇒ Object
- .missing_indexes(indexes_required) ⇒ Object
- .model_classes ⇒ Object
- .primary_key(table_name) ⇒ Object
- .run ⇒ Object
- .session_store?(obj) ⇒ Boolean
- .table_indexes(table_name) ⇒ Object
- .tables ⇒ Object
Class Method Details
.check_for_indexes ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/lol_dba/index_finding/index_finder.rb', line 9 def self.check_for_indexes eager_load_if_needed required_indexes = Hash.new([]) model_classes.each do |model_class| unless model_class.descends_from_active_record? index_name = [model_class.inheritance_column, model_class.base_class.primary_key].sort required_indexes[model_class.base_class.table_name] += [index_name] end reflections = model_class.reflections.stringify_keys reflections.each_pair do |reflection_name, | begin clazz = RelationInspectorFactory.for(.macro) next unless clazz.present? inspector = clazz.new(model_class, , reflection_name) columns = inspector.relation_columns unless columns.nil? || ..include?(:class) required_indexes[inspector.table_name.to_s] += [columns] end rescue StandardError => exception LolDba::ErrorLogging.log(model_class, , exception) end end end missing_indexes(required_indexes) end |
.eager_load_if_needed ⇒ Object
79 80 81 |
# File 'lib/lol_dba/index_finding/index_finder.rb', line 79 def self.eager_load_if_needed Rails.application.eager_load! if defined?(Rails) && !Rails.env.test? end |
.existing_indexes(table_name) ⇒ Object
54 55 56 |
# File 'lib/lol_dba/index_finding/index_finder.rb', line 54 def self.existing_indexes(table_name) table_indexes(table_name) + primary_key(table_name) end |
.missing_indexes(indexes_required) ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/lol_dba/index_finding/index_finder.rb', line 40 def self.missing_indexes(indexes_required) missing_indexes = {} indexes_required.each do |table_name, foreign_keys| next if foreign_keys.blank? || !tables.include?(table_name.to_s) keys_to_add = foreign_keys.uniq - existing_indexes(table_name) missing_indexes[table_name] = keys_to_add unless keys_to_add.empty? end missing_indexes end |
.model_classes ⇒ Object
69 70 71 72 73 |
# File 'lib/lol_dba/index_finding/index_finder.rb', line 69 def self.model_classes ActiveRecord::Base.descendants.select do |obj| Class == obj.class && session_store?(obj) end end |
.primary_key(table_name) ⇒ Object
65 66 67 |
# File 'lib/lol_dba/index_finding/index_finder.rb', line 65 def self.primary_key(table_name) Array(ActiveRecord::Base.connection.primary_key(table_name.to_s)) end |
.run ⇒ Object
3 4 5 6 7 |
# File 'lib/lol_dba/index_finding/index_finder.rb', line 3 def self.run missing_indexes = check_for_indexes MigrationFormatter.new(missing_indexes).puts_migration_content missing_indexes.any? end |
.session_store?(obj) ⇒ Boolean
75 76 77 |
# File 'lib/lol_dba/index_finding/index_finder.rb', line 75 def self.session_store?(obj) !defined?(ActiveRecord::SessionStore::Session) || obj != ActiveRecord::SessionStore::Session end |
.table_indexes(table_name) ⇒ Object
58 59 60 61 62 63 |
# File 'lib/lol_dba/index_finding/index_finder.rb', line 58 def self.table_indexes(table_name) indexes = ActiveRecord::Base.connection.indexes(table_name.to_sym) indexes.collect do |index| index.columns.size > 1 ? index.columns.sort : index.columns.first end end |
.tables ⇒ Object
50 51 52 |
# File 'lib/lol_dba/index_finding/index_finder.rb', line 50 def self.tables LolDba::RailsCompatibility.tables end |