lol_dba 1.1.1 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Gemfile CHANGED
@@ -6,4 +6,6 @@ gem 'railties'
6
6
  gem 'rspec'
7
7
  gem 'simplecov', :require => false
8
8
  gem 'simplecov-html'
9
- gem 'sqlite3'
9
+ gem 'sqlite3'
10
+
11
+ gemspec
data/README.md CHANGED
@@ -1,9 +1,6 @@
1
1
  #Lol DBA
2
2
 
3
- Lol DBA is a small package of 2 rake tasks that scan your application models and displays a list of columns that _probably_ should be indexed.
4
-
5
- *Note:* there should be mode fields depending on your application design and custom queries.
6
-
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.
7
4
 
8
5
  Installation
9
6
  ------------
@@ -21,13 +18,17 @@ Usage
21
18
 
22
19
  Display a migration for adding/removing all necessary indexes based on associations:
23
20
 
24
- rake db:index_migration
21
+ rake db:find_indexes
25
22
 
26
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`):
27
24
 
28
25
  rake db:find_query_indexes
29
26
 
30
- *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
27
+ Generate .sql files for all your migrations inside db/migrate_sql folder:
28
+
29
+ rake db:migrate_sql
30
+
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.
31
32
 
32
33
  Note that add index in big database may take a long time.
33
34
 
@@ -39,19 +40,8 @@ Compatible with Ruby 1.9 and Rails 3.x. I think.
39
40
  Upcoming features/enhancements
40
41
  ------------------------------
41
42
 
42
- * Support Arel(the new Rails 3 Active Record Query Interface) call for `find_query_indexes` action
43
-
44
-
45
- Differences from the original [rails_indexes](https://github.com/eladmeidar/rails_indexes) and [the fork I used as starting point](https://github.com/warpc/rails_indexes)
46
- -------------------------------------------
47
-
48
- * Compatible with Ruby 1.9 and Rails 3.x
49
- * Installing as Gem
50
- * Analize `has_many :through` associations
51
- * Working STI analize
52
- * Good test coverage with RSpec
53
- * Do not suggest add index for primary key (about reason read below)
54
-
43
+ * support Arel(the new Rails 3 Active Record Query Interface) call for `find_query_indexes` action
44
+ * support for `change` migrations
55
45
 
56
46
  About primary_key
57
47
  -----------------
@@ -59,7 +49,6 @@ About primary_key
59
49
 
60
50
  For this reason, no longer displays a gem suggestions about adding indexes to primary keys.
61
51
 
62
-
63
52
  Tests
64
53
  -----
65
54
 
@@ -73,13 +62,15 @@ Feedback
73
62
 
74
63
  All feedback, bug reports and thoughts on this gratefully received.
75
64
 
76
- Author:
65
+ Contributors
77
66
  ------
78
- [Diego Plentz](http://plentz.org)
79
67
 
80
- Thanks:
81
- [Elad Meidar](http://blog.eizesus.com)
82
- [Eric Davis](http://littlestreamsoftware.com)
68
+ * [Diego Plentz](http://plentz.org)
69
+ * [Elad Meidar](http://blog.eizesus.com)
70
+ * [Eric Davis](http://littlestreamsoftware.com)
71
+ * [Jay Fields](http://jayfields.com/)
72
+ * [Muness Alrubaie](http://muness.blogspot.com/)
73
+ * [Vladimir Sharshov](https://github.com/warpc)
83
74
 
84
75
  License
85
76
  -------
@@ -1,5 +1,7 @@
1
1
  module LolDba
2
2
 
3
+ require "lol_dba/writer"
4
+ require "lol_dba/migration"
3
5
  require "lol_dba/railtie.rb" if defined?(Rails)
4
6
 
5
7
  def self.form_migration_content(migration_name, add_index_array, del_index_array)
@@ -0,0 +1,40 @@
1
+ module LolDba
2
+ class Migration
3
+ attr_accessor :full_name
4
+
5
+ def initialize(migration_file)
6
+ self.full_name = File.basename(migration_file, ".rb")
7
+ require Rails.root + "db/migrate/#{full_name}.rb"
8
+ end
9
+
10
+ def number
11
+ /^(\d+)_(.*)$/.match(full_name)[1]
12
+ end
13
+
14
+ def name
15
+ /^(\d+)_(.*)$/.match(full_name)[2]
16
+ end
17
+
18
+ def to_s
19
+ full_name
20
+ end
21
+
22
+ def migration_class
23
+ name.camelize.constantize
24
+ end
25
+
26
+ def up
27
+ migration_class.up
28
+ connection.execute("INSERT INTO schema_migrations (version) VALUES (#{number})")
29
+ end
30
+
31
+ def down
32
+ migration_class.down
33
+ connection.execute("DELETE FROM schema_migrations WHERE version = #{number}")
34
+ end
35
+
36
+ def connection
37
+ ActiveRecord::Base.connection
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,62 @@
1
+ module LolDba
2
+ class SqlGenerator
3
+ class << self
4
+
5
+ def connection
6
+ ActiveRecord::Base.connection
7
+ end
8
+
9
+ def methods_to_modify
10
+ [:execute, :do_execute, :rename_column, :change_column, :column_for, :tables, :indexes, :select_all] & connection.class.methods
11
+ end
12
+
13
+ def redefine_execute_methods
14
+ save_original_methods
15
+ connection.class.send(:define_method, :execute) {|*args| Writer.write(args.first) }
16
+ connection.class.send(:define_method, :do_execute) {|*args| Writer.write(args.first) }
17
+ connection.class.send(:define_method, :column_for) {|*args| args.last }
18
+ connection.class.send(:define_method, :change_column) {|*args| [] }
19
+ connection.class.send(:define_method, :rename_column) {|*args| [] }
20
+ connection.class.send(:define_method, :tables) {|*args| [] }
21
+ connection.class.send(:define_method, :select_all) {|*args| [] }
22
+ connection.class.send(:define_method, :indexes) {|*args| [] }
23
+ end
24
+
25
+ def save_original_methods
26
+ methods_to_modify.each do |method_name|
27
+ connection.class.send(:alias_method, "orig_#{method_name}".to_sym, method_name)
28
+ end
29
+ end
30
+
31
+ def reset_methods
32
+ methods_to_modify.each do |method_name|
33
+ connection.class.send(:alias_method, method_name, "orig_#{method_name}".to_sym)
34
+ end
35
+ end
36
+
37
+ def generate_instead_of_executing(&block)
38
+ LolDba::Writer.reset
39
+ redefine_execute_methods
40
+ yield
41
+ reset_methods
42
+ end
43
+
44
+ def migrations
45
+ Dir.glob(File.join(Rails.root, "db", "migrate", '*.rb'))
46
+ end
47
+
48
+ def generate
49
+ generate_instead_of_executing { migrations.each{|file| up_and_down(file) } }
50
+ end
51
+
52
+ def up_and_down(file)
53
+ #TODO support to migrations with change
54
+ migration = LolDba::Migration.new(file)
55
+ LolDba::Writer.file_name = "#{migration}.sql"
56
+ migration.up
57
+ #MigrationSqlGenerator::Writer.file_name = "#{migration}_down.sql"
58
+ #migration.down
59
+ end
60
+ end
61
+ end
62
+ end
@@ -1,3 +1,3 @@
1
1
  module LolDba
2
- VERSION = "1.1.1" unless defined? LolDba::VERSION
2
+ VERSION = "1.2.0" unless defined? LolDba::VERSION
3
3
  end
@@ -0,0 +1,24 @@
1
+ module LolDba
2
+ class Writer
3
+ class << self
4
+ attr_accessor :file_name
5
+
6
+ def reset
7
+ FileUtils.rm_rf output_dir
8
+ Dir.mkdir output_dir
9
+ end
10
+
11
+ def output_dir
12
+ File.join(Rails.root, "db", "migrate_sql")
13
+ end
14
+
15
+ def path
16
+ File.join(output_dir, file_name)
17
+ end
18
+
19
+ def write(string)
20
+ File.open(path, 'a') { |file| file << string; file << ";\n" }
21
+ end
22
+ end
23
+ end
24
+ end
@@ -1,12 +1,18 @@
1
1
  require 'lol_dba'
2
+ require 'lol_dba/sql_generator'
2
3
 
3
4
  namespace :db do
4
- desc "Collect indexes based on AR::Base.find calls."
5
+ desc "Display a migration for adding/removing all necessary indexes based on associations"
6
+ task :find_indexes => :environment do
7
+ LolDba.simple_migration
8
+ end
9
+ desc "Display a migration for adding/removing all necessary indexes based on AR::Base#find calls"
5
10
  task :find_query_indexes => :environment do
6
11
  LolDba.ar_find_indexes
7
12
  end
8
- desc "Collect indexes based on associations"
9
- task :index_migration => :environment do
10
- LolDba.simple_migration
13
+ desc "Generate .sql files for all your migrations inside db/migrate_sql folder"
14
+ task :migrate_sql => :environment do
15
+ LolDba::SqlGenerator.generate
11
16
  end
17
+
12
18
  end
@@ -1,4 +1,3 @@
1
- # -*- encoding: utf-8 -*-
2
1
  $LOAD_PATH << File.join(File.dirname(__FILE__), 'lib')
3
2
  require 'lol_dba/version.rb'
4
3
 
@@ -6,20 +5,20 @@ Gem::Specification.new do |s|
6
5
  s.name = "lol_dba"
7
6
  s.version = LolDba::VERSION
8
7
  s.platform = Gem::Platform::RUBY
9
- s.authors = ["Diego Plentz", "Elad Meidar", "Vladimir Sharshov"]
10
- s.email = ["diego@plentz.org", "elad@eizesus.com", "vsharshov@gmail.com"]
8
+
9
+ s.authors = ["Diego Plentz", "Elad Meidar", "Eric Davis", "Muness Alrubaie", "Vladimir Sharshov"]
10
+ s.email = ["diego@plentz.org", "elad@eizesus.com", "", "vsharshov@gmail.com"]
11
11
  s.homepage = "https://github.com/plentz/lol_dba"
12
- s.summary = "A rake task to track down missing database indexes. does not assume that all foreign keys end with the convention of _id"
13
- s.description = "LOL DBA is a small package of 2 rake tasks that scan your application models and displays a list of columns that probably should be indexed."
12
+ s.summary = "A small package of rake tasks to track down missing database indexes and generate sql migration scripts"
13
+ s.description = "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."
14
14
 
15
- #s.rubyforge_project = "lol_dba"
16
15
  s.files = `git ls-files`.split("\n")
17
16
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
- s.require_paths = ["lib"]
17
+ s.require_paths = ["lib", "lib/lol_dba"]
19
18
 
20
- s.add_dependency 'activerecord', '>=2.3.0'
21
- s.add_dependency 'actionpack'
22
- s.add_dependency 'railties'
19
+ s.add_dependency 'activerecord', '>= 3.0'
20
+ s.add_dependency 'actionpack', '>= 3.0'
21
+ s.add_dependency 'railties', '>= 3.0'
23
22
  s.add_development_dependency 'sqlite3'
24
23
  s.add_development_dependency 'rspec'
25
24
  end
metadata CHANGED
@@ -1,54 +1,56 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lol_dba
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - Diego Plentz
9
9
  - Elad Meidar
10
+ - Eric Davis
11
+ - Muness Alrubaie
10
12
  - Vladimir Sharshov
11
13
  autorequire:
12
14
  bindir: bin
13
15
  cert_chain: []
14
- date: 2012-03-02 00:00:00.000000000 Z
16
+ date: 2012-03-08 00:00:00.000000000 Z
15
17
  dependencies:
16
18
  - !ruby/object:Gem::Dependency
17
19
  name: activerecord
18
- requirement: &70320988946760 !ruby/object:Gem::Requirement
20
+ requirement: &70236560955460 !ruby/object:Gem::Requirement
19
21
  none: false
20
22
  requirements:
21
23
  - - ! '>='
22
24
  - !ruby/object:Gem::Version
23
- version: 2.3.0
25
+ version: '3.0'
24
26
  type: :runtime
25
27
  prerelease: false
26
- version_requirements: *70320988946760
28
+ version_requirements: *70236560955460
27
29
  - !ruby/object:Gem::Dependency
28
30
  name: actionpack
29
- requirement: &70320988946340 !ruby/object:Gem::Requirement
31
+ requirement: &70236560954840 !ruby/object:Gem::Requirement
30
32
  none: false
31
33
  requirements:
32
34
  - - ! '>='
33
35
  - !ruby/object:Gem::Version
34
- version: '0'
36
+ version: '3.0'
35
37
  type: :runtime
36
38
  prerelease: false
37
- version_requirements: *70320988946340
39
+ version_requirements: *70236560954840
38
40
  - !ruby/object:Gem::Dependency
39
41
  name: railties
40
- requirement: &70320975804080 !ruby/object:Gem::Requirement
42
+ requirement: &70236560954140 !ruby/object:Gem::Requirement
41
43
  none: false
42
44
  requirements:
43
45
  - - ! '>='
44
46
  - !ruby/object:Gem::Version
45
- version: '0'
47
+ version: '3.0'
46
48
  type: :runtime
47
49
  prerelease: false
48
- version_requirements: *70320975804080
50
+ version_requirements: *70236560954140
49
51
  - !ruby/object:Gem::Dependency
50
52
  name: sqlite3
51
- requirement: &70320975801440 !ruby/object:Gem::Requirement
53
+ requirement: &70236560953260 !ruby/object:Gem::Requirement
52
54
  none: false
53
55
  requirements:
54
56
  - - ! '>='
@@ -56,10 +58,10 @@ dependencies:
56
58
  version: '0'
57
59
  type: :development
58
60
  prerelease: false
59
- version_requirements: *70320975801440
61
+ version_requirements: *70236560953260
60
62
  - !ruby/object:Gem::Dependency
61
63
  name: rspec
62
- requirement: &70320975799940 !ruby/object:Gem::Requirement
64
+ requirement: &70236560952340 !ruby/object:Gem::Requirement
63
65
  none: false
64
66
  requirements:
65
67
  - - ! '>='
@@ -67,12 +69,14 @@ dependencies:
67
69
  version: '0'
68
70
  type: :development
69
71
  prerelease: false
70
- version_requirements: *70320975799940
71
- description: LOL DBA is a small package of 2 rake tasks that scan your application
72
- models and displays a list of columns that probably should be indexed.
72
+ version_requirements: *70236560952340
73
+ description: lol_dba is a small package of rake tasks that scan your application models
74
+ and displays a list of columns that probably should be indexed. Also, it can generate
75
+ .sql migration scripts.
73
76
  email:
74
77
  - diego@plentz.org
75
78
  - elad@eizesus.com
79
+ - ''
76
80
  - vsharshov@gmail.com
77
81
  executables: []
78
82
  extensions: []
@@ -85,8 +89,11 @@ files:
85
89
  - README.md
86
90
  - Rakefile
87
91
  - lib/lol_dba.rb
92
+ - lib/lol_dba/migration.rb
88
93
  - lib/lol_dba/railtie.rb
94
+ - lib/lol_dba/sql_generator.rb
89
95
  - lib/lol_dba/version.rb
96
+ - lib/lol_dba/writer.rb
90
97
  - lib/tasks/lol_dba.rake
91
98
  - lol_dba.gemspec
92
99
  - spec/ar_find_index_spec.rb
@@ -116,6 +123,7 @@ post_install_message:
116
123
  rdoc_options: []
117
124
  require_paths:
118
125
  - lib
126
+ - lib/lol_dba
119
127
  required_ruby_version: !ruby/object:Gem::Requirement
120
128
  none: false
121
129
  requirements:
@@ -133,8 +141,8 @@ rubyforge_project:
133
141
  rubygems_version: 1.8.17
134
142
  signing_key:
135
143
  specification_version: 3
136
- summary: A rake task to track down missing database indexes. does not assume that
137
- all foreign keys end with the convention of _id
144
+ summary: A small package of rake tasks to track down missing database indexes and
145
+ generate sql migration scripts
138
146
  test_files:
139
147
  - spec/ar_find_index_spec.rb
140
148
  - spec/associations_index_spec.rb