Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added #sortalize method, to sort 1st level arrays from AR::Base to en…

…sure that the order is the same as in the required indexes array. (english please!)
  • Loading branch information...
commit fed8b19504f5c408a7ae98c0d0ddf10bd6fd000c 1 parent 24c7501
@eladmeidar authored
Showing with 19 additions and 4 deletions.
  1. +18 −3 lib/indexer.rb
  2. +1 −1  tasks/indexer.rake
View
21 lib/indexer.rb
@@ -1,6 +1,12 @@
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
@@ -82,8 +88,8 @@ def self.check_for_indexes(migration_format = false)
@indexes_required.each do |table_name, foreign_keys|
unless foreign_keys.blank?
- existing_indexes = ActiveRecord::Base.connection.indexes(table_name.to_sym).collect(&:columns).flatten
- keys_to_add = foreign_keys.uniq - existing_indexes
+ 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
@@ -139,6 +145,16 @@ def self.scan_finds
end
end
end
+ @missing_indexes = {}
+ @indexes_required.each do |table_name, foreign_keys|
+
+ unless foreign_keys.blank?
+ 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
@@ -192,7 +208,6 @@ def self.down
puts migration
end
end
- end
def self.indexes_list
check_for_indexes.each do |table_name, keys_to_add|
View
2  tasks/indexer.rake
@@ -1,4 +1,4 @@
-require File.join(File.dirname(__FILE__), "../lib/annotate_models.rb")
+require File.join(File.dirname(__FILE__), "../lib/indexer.rb")
namespace :db do
desc "collect indexes based on AR::Base.find calls."
Please sign in to comment.
Something went wrong with that request. Please try again.