Skip to content
This repository
Browse code

Refactoring and fixing issues

  • Loading branch information...
commit 89f92027a7ed5680deea6349733da117e0039d3c 1 parent 1dcd9e2
Elad Meidar authored March 03, 2010
72  lib/indexer.rb
@@ -84,16 +84,23 @@ def self.check_for_indexes(migration_format = false)
84 84
   end
85 85
 
86 86
   def self.scan_finds
  87
+    
  88
+    
  89
+    # Collect all files that can contain queries, in app/ directories (includes plugins and such)
  90
+    # TODO: add lib too ? 
87 91
     file_names = []
88 92
     
89 93
     Dir.chdir(Rails.root) do 
90 94
       file_names = Dir["**/app/**/*.rb"].uniq.reject {|file_with_path| file_with_path.include?('test')}
91 95
     end
92  
-
  96
+    
93 97
     @indexes_required = Hash.new([])
  98
+    
  99
+    # Scan each file
94 100
     file_names.each do |file_name| 
95 101
       current_file = File.open(File.join(Rails.root, file_name), 'r')
96 102
 
  103
+      # Scan each line
97 104
       current_file.each do |line|
98 105
         
99 106
         # by default, try to add index on primary key, based on file name
@@ -105,7 +112,10 @@ def self.scan_finds
105 112
           # NO-OP
106 113
         end
107 114
         
  115
+        # Get the model class
108 116
         klass = current_model_name.split('::').inject(Object){ |klass,part| klass.const_get(part) } rescue nil
  117
+        
  118
+        # Only add primary key for active record dependent classes and non abstract ones too.
109 119
         if klass.present? && klass < ActiveRecord::Base && !klass.abstract_class?
110 120
           current_model = current_model_name.constantize
111 121
           primary_key = current_model.primary_key
@@ -139,48 +149,56 @@ def self.scan_finds
139 149
     @indexes_required
140 150
   end
141 151
   
142  
-  
  152
+  # Check line for find* methods (include find_all, find_by and just find)
143 153
   def self.check_line_for_find_indexes(file_name, line)
144  
-    find_regexp = Regexp.new(/([A-Z]{1}[A-Za-z]+|self).(find){1}((_all){0,1}(_by_){0,1}([A-Za-z_]+))?\(([0-9A-Za-z"\':=>. \[\]{},]*)\)/)
  154
+    
  155
+    # TODO: Assumes that you have a called on #find. you can actually call #find without a caller in a model code. ex:
  156
+    # def something
  157
+    #   find(self.id)
  158
+    # end
  159
+    #
  160
+    # find_regexp = Regexp.new(/([A-Z]{1}[A-Za-z]+|self).(find){1}((_all){0,1}(_by_){0,1}([A-Za-z_]+))?\(([0-9A-Za-z"\':=>. \[\]{},]*)\)/)
  161
+    
  162
+    find_regexp = Regexp.new(/(([A-Z]{1}[A-Za-z]+|self).)?(find){1}((_all){0,1}(_by_){0,1}([A-Za-z_]+))?\(([0-9A-Za-z"\':=>. \[\]{},]*)\)/)
  163
+    
  164
+    # If line matched a finder
145 165
     if matches = find_regexp.match(line)
146 166
 
147  
-      model_name, column_names, options = matches[1], matches[6], matches[7]
148  
-  
149  
-      if model_name == "self"
  167
+      model_name, column_names, options = matches[2], matches[7], matches[8]
  168
+      
  169
+      # if the finder class is "self" or empty (can be a simple "find()" in a model)
  170
+      if model_name == "self" || model_name.blank?
150 171
         model_name = File.basename(file_name).sub(/\.rb$/,'').camelize
151 172
         table_name = model_name.constantize.table_name            
152 173
       else
153 174
         if model_name.respond_to?(:constantize)
154 175
           if model_name.constantize.respond_to?(:table_name)             
155 176
             table_name = model_name.constantize.table_name
156  
-          else
157  
-            #puts "Unable to get the table_name for #{model_name.to_s}. it could be an ActiveResource"
158  
-            next
159 177
           end
160  
-        else
161  
-          #puts "Unable to constantize #{model_name.to_s}, if you are sure that #{model_name.to_s} is a valid class name, please file an issue on\nhttp://github.com/eladmeidar/rails_indexes\nPlease supply the relevant code as well, thanks. =)"
162  
-          next
163 178
         end
164 179
       end
  180
+      
  181
+      # Check that all prerequisites are met
  182
+      if model_name.present? && table_name.present?
  183
+        primary_key = model_name.constantize.primary_key
  184
+        @indexes_required[table_name] += [primary_key] unless @indexes_required[table_name].include?(primary_key)
165 185
   
166  
-      primary_key = model_name.constantize.primary_key
167  
-      @indexes_required[table_name] += [primary_key] unless @indexes_required[table_name].include?(primary_key)
168  
-  
169  
-      if column_names.present?
170  
-        column_names = column_names.split('_and_')
  186
+        if column_names.present?
  187
+          column_names = column_names.split('_and_')
171 188
 
172  
-        # remove find_by_sql references.
173  
-        column_names.delete("sql")
  189
+          # remove find_by_sql references.
  190
+          column_names.delete("sql")
174 191
 
175  
-        column_names = model_name.constantize.column_names & column_names
  192
+          column_names = model_name.constantize.column_names & column_names
176 193
 
177  
-        # Check if there were more than 1 column
178  
-        if column_names.size == 1
179  
-          column_name = column_names.first
180  
-          @indexes_required[table_name] += [column_name] unless @indexes_required[table_name].include?(column_name)
181  
-        else
182  
-          @indexes_required[table_name] += [column_names] unless @indexes_required[table_name].include?(column_names)
183  
-          @indexes_required[table_name] += [column_names.reverse] unless @indexes_required[table_name].include?(column_names.reverse)
  194
+          # Check if there were more than 1 column
  195
+          if column_names.size == 1
  196
+            column_name = column_names.first
  197
+            @indexes_required[table_name] += [column_name] unless @indexes_required[table_name].include?(column_name)
  198
+          else
  199
+            @indexes_required[table_name] += [column_names] unless @indexes_required[table_name].include?(column_names)
  200
+            @indexes_required[table_name] += [column_names.reverse] unless @indexes_required[table_name].include?(column_names.reverse)
  201
+          end
184 202
         end
185 203
       end
186 204
     end
10  tasks/indexer.rake
@@ -2,17 +2,11 @@ require File.join(File.dirname(__FILE__), "../lib/indexer.rb")
2 2
 
3 3
 namespace :db do
4 4
   desc "collect indexes based on AR::Base.find calls."
5  
-  task :show_me_ar_find_indexes => :environment do
  5
+  task :find_query_indexes => :environment do
6 6
     Indexer.ar_find_indexes
7 7
   end
8 8
   
9  
-  desc "scan for possible required indexes"
10  
-  task :show_me_some_indexes => :environment do
11  
-    # Indexer.indexes_list
12  
-    puts "Sorry, simple report is deprecated.\nuse rake db:show_me_a_migration or db:show_me_ar_find_indexes instead"
13  
-  end
14  
-  
15  
-  task :show_me_a_migration => :environment do
  9
+  task :index_migration => :environment do
16 10
     Indexer.simple_migration
17 11
   end
18 12
 end

0 notes on commit 89f9202

Please sign in to comment.
Something went wrong with that request. Please try again.