vendor/plugins/refinery/lib/indexer.rb in refinerycms-0.9.5.13 vs vendor/plugins/refinery/lib/indexer.rb in refinerycms-0.9.5.14

- old
+ new

@@ -1,17 +1,17 @@ module Indexer - - + + def self.sortalize(array) Marshal.load(Marshal.dump(array)).each do |element| element.sort! if element.is_a?(Array) end end - + def self.check_for_indexes(migration_format = false) model_names = [] - Dir.chdir(Rails.root) do + Dir.chdir(Rails.root) do model_names = Dir["**/app/models/*.rb"].collect {|filename| File.basename(filename) }.uniq end model_classes = [] model_names.each do |model_name| @@ -30,11 +30,11 @@ @indexes_required = Hash.new([]) @index_migrations = Hash.new([]) model_classes.each do |class_name| - # check if this is an STI child instance + # check if this is an STI child instance if class_name.base_class.name != class_name.name && (class_name.column_names.include?(class_name.base_class.inheritance_column) || class_name.column_names.include?(class_name.inheritance_column)) # add the inharitance column on the parent table if !(migration_format) @indexes_required[class_name.base_class.table_name] += [class_name.inheritance_column].sort unless @indexes_required[class_name.base_class.table_name].include?([class_name.inheritance_column].sort) else @@ -99,29 +99,29 @@ end end def self.scan_finds file_names = [] - Dir.chdir(Rails.root) do + Dir.chdir(Rails.root) do file_names = Dir["**/app/**/*.rb"].uniq.reject {|file_with_path| file_with_path.include?('test')} end puts "Found #{file_names.size} files." @indexes_required = Hash.new([]) - file_names.each do |file_name| + file_names.each do |file_name| current_file = File.open(File.join(Rails.root, file_name), 'r') current_file.each do |line| find_regexp = Regexp.new(/([A-Z]{1}[A-Za-z]+).(find){1}((_all){0,1}(_by_){0,1}([A-Za-z_]+))?\(([0-9A-Za-z"\':=>. \[\]{},]*)\)/) if matches = find_regexp.match(line) model_name, column_names, options = matches[1], matches[6], matches[7] # puts "Model: #{model_name}, columns: #{column_names}, options: #{options}" - + if model_name.respond_to?(:constantize) - if model_name.constantize.respond_to?(:table_name) + if model_name.constantize.respond_to?(:table_name) table_name = model_name.constantize.table_name else puts "Unable to get the table_name for #{model_name.to_s}. it could be an ActiveResource" next end @@ -159,19 +159,19 @@ existing_indexes = ActiveRecord::Base.connection.indexes(table_name.to_sym).collect(&:columns) keys_to_add = self.sortalize(foreign_keys.uniq) - self.sortalize(existing_indexes) @missing_indexes[table_name] = keys_to_add unless keys_to_add.empty? end end - + @indexes_required end - - def self.key_exists?(table,key_columns) + + def self.key_exists?(table,key_columns) result = (key_columns.to_a - ActiveRecord::Base.connection.indexes(table).map { |i| i.columns }.flatten) result.empty? end - + def self.simple_migration migration_format = true missing_indexes = check_for_indexes(migration_format) unless missing_indexes.keys.empty? @@ -187,52 +187,52 @@ remove << "remove_index :#{table_name}, :column => [#{keys.join(', ')}]" else add << "add_index :#{table_name}, :#{key}" remove << "remove_index :#{table_name}, :#{key}" end - + end end - - migration = <<EOM + + migration = <<EOM class AddMissingIndexes < ActiveRecord::Migration def self.up - + # These indexes were found by searching for AR::Base finds on your application # It is strongly recommanded that you will consult a professional DBA about your infrastucture and implemntation before # changing your database in that matter. # There is a possibility that some of the indexes offered below is not required and can be removed and not added, if you require # further assistance with your rails application, database infrastructure or any other problem, visit: # # http://www.railsmentors.org # http://www.railstutor.org # http://guides.rubyonrails.org - + #{add.uniq.join("\n ")} end - + def self.down #{remove.uniq.join("\n ")} end end EOM puts "## Drop this into a file in db/migrate ##" puts migration end end - + def self.indexes_list check_for_indexes.each do |table_name, keys_to_add| puts "Table '#{table_name}' => #{keys_to_add.to_sentence}" end end - + def self.ar_find_indexes find_indexes = self.scan_finds - + unless find_indexes.keys.empty? add = [] remove = [] find_indexes.each do |table_name, keys_to_add| keys_to_add.each do |key| @@ -244,37 +244,37 @@ remove << "remove_index :#{table_name}, :column => [#{keys.join(', ')}]" else add << "add_index :#{table_name}, :#{key}" remove << "remove_index :#{table_name}, :#{key}" end - + end end - - migration = <<EOM + + migration = <<EOM class AddFindsMissingIndexes < ActiveRecord::Migration def self.up - + # These indexes were found by searching for AR::Base finds on your application # It is strongly recommanded that you will consult a professional DBA about your infrastucture and implemntation before # changing your database in that matter. # There is a possibility that some of the indexes offered below is not required and can be removed and not added, if you require # further assistance with your rails application, database infrastructure or any other problem, visit: # # http://www.railsmentors.org # http://www.railstutor.org # http://guides.rubyonrails.org - + #{add.uniq.join("\n ")} end - + def self.down #{remove.uniq.join("\n ")} end end EOM puts "## Drop this into a file in db/migrate ##" puts migration end end -end \ No newline at end of file +end