lol_dba 1.2.0 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- #Lol DBA
1
+ #lol_dba
2
2
 
3
3
  lol_dba is a small package of rake tasks that scan your application models and displays a list of columns that probably should be indexed. Also, it can generate .sql migration scripts. Most of the code come from rails_indexes and migration_sql_generator.
4
4
 
@@ -20,17 +20,11 @@ Display a migration for adding/removing all necessary indexes based on associati
20
20
 
21
21
  rake db:find_indexes
22
22
 
23
- Display a migration for adding/removing all necessary indexes based on AR::Base#find calls (including: `find`, `find_by`, `find_all_by`, `find_by_x_and_y`, `find_all_by_x_and_y`):
24
-
25
- rake db:find_query_indexes
26
-
27
23
  Generate .sql files for all your migrations inside db/migrate_sql folder:
28
24
 
29
25
  rake db:migrate_sql
30
26
 
31
- *Notice:* At now moment it does not support Arel(the new Rails 3 Active Record Query Interface) calls (including: where, joins, includes, from, select...), but still usefull for indexes based on association.
32
-
33
- Note that add index in big database may take a long time.
27
+ Note that create an index in a big database may take a long time.
34
28
 
35
29
  Compatibility
36
30
  -------------
@@ -179,95 +179,6 @@ EOM
179
179
 
180
180
  end
181
181
 
182
- def self.scan_finds
183
-
184
- # Collect all files that can contain queries, in app/ directories (includes plugins and such)
185
- # TODO: add lib too ?
186
- file_names = []
187
-
188
- Dir.chdir(Rails.root) do
189
- file_names = Dir["**/app/**/*.rb"].uniq.reject {|file_with_path| file_with_path.include?('test')}
190
- end
191
-
192
- @indexes_required = Hash.new([])
193
-
194
- # Scan each file
195
- file_names.each do |file_name|
196
- current_file = File.open(File.join(Rails.root, file_name), 'r')
197
- begin
198
- current_model_name = File.basename(file_name).sub(/\.rb$/,'').camelize
199
- rescue
200
- # No-op
201
- end
202
-
203
- # by default, try to add index on primary key, based on file name
204
- # this will fail if the file is not a model file
205
-
206
- klass = current_model_name.split('::').inject(Object){ |klass,part| klass.const_get(part) } rescue next
207
- next if !klass.present? || klass < ActiveRecord::Base && klass.abstract_class?
208
-
209
- # Scan each line
210
- current_file.each { |line| check_line_for_find_indexes(file_name, line) }
211
- end
212
-
213
- missing_indexes, warning_messages = validate_and_sort_indexes(@indexes_required)
214
-
215
- end
216
-
217
- # Check line for find* methods (include find_all, find_by and just find)
218
- def self.check_line_for_find_indexes(file_name, line)
219
- # TODO: Assumes that you have a called on #find. you can actually call #find without a caller in a model code. ex:
220
- # def something
221
- # find(self.id)
222
- # end
223
- #
224
- # 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"\':=>. \[\]{},]*)\)/)
225
-
226
- 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"\':=>. \[\]{},]*)\)/)
227
-
228
- # If line matched a finder
229
- if matches = find_regexp.match(line)
230
-
231
- model_name, column_names, options = matches[2], matches[7], matches[8]
232
-
233
- # if the finder class is "self" or empty (can be a simple "find()" in a model)
234
- if model_name == "self" || model_name.blank?
235
- model_name = File.basename(file_name).sub(/\.rb$/,'').camelize
236
- table_name = model_name.constantize.table_name
237
- else
238
- if model_name.respond_to?(:constantize)
239
- if model_name.constantize.respond_to?(:table_name)
240
- table_name = model_name.constantize.table_name
241
- end
242
- end
243
- end
244
-
245
- # Check that all prerequisites are met
246
- if model_name.present? && table_name.present? && model_name.constantize.ancestors.include?(ActiveRecord::Base)
247
- primary_key = model_name.constantize.primary_key
248
- @indexes_required[table_name] += [primary_key] unless @indexes_required[table_name].include?(primary_key)
249
-
250
- if column_names.present?
251
- column_names = column_names.split('_and_')
252
-
253
- # remove find_by_sql references.
254
- column_names.delete("sql")
255
-
256
- column_names = model_name.constantize.column_names & column_names
257
-
258
- # Check if there were more than 1 column
259
- if column_names.size == 1
260
- column_name = column_names.first
261
- @indexes_required[table_name] += [column_name] unless @indexes_required[table_name].include?(column_name)
262
- else
263
- @indexes_required[table_name] += [column_names] unless @indexes_required[table_name].include?(column_names)
264
- @indexes_required[table_name] += [column_names.reverse] unless @indexes_required[table_name].include?(column_names.reverse)
265
- end
266
- end
267
- end
268
- end
269
- end
270
-
271
182
  def self.key_exists?(table,key_columns)
272
183
  result = (Array(key_columns) - ActiveRecord::Base.connection.indexes(table).map { |i| i.columns }.flatten)
273
184
  #FIXME: Primary key always indexes, but ActiveRecord::Base.connection.indexes not show it!
@@ -290,11 +201,4 @@ EOM
290
201
 
291
202
  puts_migration_content("AddMissingIndexes", missing_indexes, warning_messages)
292
203
  end
293
-
294
- def self.ar_find_indexes(migration_mode=true)
295
- find_indexes, warning_messages = self.scan_finds
296
- return find_indexes, warning_messages unless migration_mode
297
-
298
- puts_migration_content("AddFindsMissingIndexes", find_indexes, warning_messages)
299
- end
300
204
  end
@@ -1,3 +1,3 @@
1
1
  module LolDba
2
- VERSION = "1.2.0" unless defined? LolDba::VERSION
2
+ VERSION = "1.2.1" unless defined? LolDba::VERSION
3
3
  end
@@ -6,10 +6,6 @@ namespace :db do
6
6
  task :find_indexes => :environment do
7
7
  LolDba.simple_migration
8
8
  end
9
- desc "Display a migration for adding/removing all necessary indexes based on AR::Base#find calls"
10
- task :find_query_indexes => :environment do
11
- LolDba.ar_find_indexes
12
- end
13
9
  desc "Generate .sql files for all your migrations inside db/migrate_sql folder"
14
10
  task :migrate_sql => :environment do
15
11
  LolDba::SqlGenerator.generate
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lol_dba
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -17,7 +17,7 @@ date: 2012-03-08 00:00:00.000000000 Z
17
17
  dependencies:
18
18
  - !ruby/object:Gem::Dependency
19
19
  name: activerecord
20
- requirement: &70236560955460 !ruby/object:Gem::Requirement
20
+ requirement: &70321919228860 !ruby/object:Gem::Requirement
21
21
  none: false
22
22
  requirements:
23
23
  - - ! '>='
@@ -25,10 +25,10 @@ dependencies:
25
25
  version: '3.0'
26
26
  type: :runtime
27
27
  prerelease: false
28
- version_requirements: *70236560955460
28
+ version_requirements: *70321919228860
29
29
  - !ruby/object:Gem::Dependency
30
30
  name: actionpack
31
- requirement: &70236560954840 !ruby/object:Gem::Requirement
31
+ requirement: &70321919228120 !ruby/object:Gem::Requirement
32
32
  none: false
33
33
  requirements:
34
34
  - - ! '>='
@@ -36,10 +36,10 @@ dependencies:
36
36
  version: '3.0'
37
37
  type: :runtime
38
38
  prerelease: false
39
- version_requirements: *70236560954840
39
+ version_requirements: *70321919228120
40
40
  - !ruby/object:Gem::Dependency
41
41
  name: railties
42
- requirement: &70236560954140 !ruby/object:Gem::Requirement
42
+ requirement: &70321919227360 !ruby/object:Gem::Requirement
43
43
  none: false
44
44
  requirements:
45
45
  - - ! '>='
@@ -47,10 +47,10 @@ dependencies:
47
47
  version: '3.0'
48
48
  type: :runtime
49
49
  prerelease: false
50
- version_requirements: *70236560954140
50
+ version_requirements: *70321919227360
51
51
  - !ruby/object:Gem::Dependency
52
52
  name: sqlite3
53
- requirement: &70236560953260 !ruby/object:Gem::Requirement
53
+ requirement: &70321919226900 !ruby/object:Gem::Requirement
54
54
  none: false
55
55
  requirements:
56
56
  - - ! '>='
@@ -58,10 +58,10 @@ dependencies:
58
58
  version: '0'
59
59
  type: :development
60
60
  prerelease: false
61
- version_requirements: *70236560953260
61
+ version_requirements: *70321919226900
62
62
  - !ruby/object:Gem::Dependency
63
63
  name: rspec
64
- requirement: &70236560952340 !ruby/object:Gem::Requirement
64
+ requirement: &70321919226000 !ruby/object:Gem::Requirement
65
65
  none: false
66
66
  requirements:
67
67
  - - ! '>='
@@ -69,7 +69,7 @@ dependencies:
69
69
  version: '0'
70
70
  type: :development
71
71
  prerelease: false
72
- version_requirements: *70236560952340
72
+ version_requirements: *70321919226000
73
73
  description: lol_dba is a small package of rake tasks that scan your application models
74
74
  and displays a list of columns that probably should be indexed. Also, it can generate
75
75
  .sql migration scripts.