Class: Rails::Clean::Schema::Analyzer
- Inherits:
-
Object
- Object
- Rails::Clean::Schema::Analyzer
- Defined in:
- lib/rails/clean/schema/analyzer.rb
Instance Method Summary collapse
-
#initialize ⇒ Analyzer
constructor
A new instance of Analyzer.
- #run ⇒ Object
Constructor Details
#initialize ⇒ Analyzer
Returns a new instance of Analyzer.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/rails/clean/schema/analyzer.rb', line 6 def initialize @schema_info = ActiveRecord::Base.connection.tables.index_with do |table| { columns: ActiveRecord::Base.connection.columns(table).map(&:name), indexes: ActiveRecord::Base.connection.indexes(table).map(&:columns).flatten } end @model_info = ActiveRecord::Base.descendants.select do |model| model.table_exists? && !model.abstract_class? end.index_with do |model| { table: model.table_name, columns: model.column_names } end end |
Instance Method Details
#run ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/rails/clean/schema/analyzer.rb', line 24 def run unused_tables = @schema_info.keys - @model_info.values.map { |info| info[:table] } unused_columns = {} @model_info.each do |model, info| table = info[:table] next unless @schema_info[table] schema_columns = @schema_info[table][:columns] model_columns = info[:columns] unused = schema_columns - model_columns unused_columns[table] = unused if unused.any? end { unused_tables: unused_tables, unused_columns: unused_columns } end |