lol_dba 1.5.0 → 1.6.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b5f965e8e5463841860f579840400ff6a3464c62
4
+ data.tar.gz: 455fc4389cd58ba70158cfd9f10b14e34500fef0
5
+ SHA512:
6
+ metadata.gz: 054bfcbe1203ed325c3b3cf07ada9a41410f830f2e18b46c59a5c10b0fe7167691e0143f10dd33f9a2c87f1fb6d2ad8bb15a6b6596073f5e4793958baceebebd
7
+ data.tar.gz: 836ebb3454366193e65a80b6d40504efec1f06c63a9a011cb8700c100df6eac462429cbc79829f915a166810a77c6ceccd23fef51f23c609fb9fdd42556db356
data/.travis.yml CHANGED
@@ -1,6 +1,5 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 1.9.2
4
3
  - 1.9.3
5
4
  - 2.0.0
6
5
  gemfile:
data/Gemfile.lock CHANGED
@@ -1,72 +1,59 @@
1
1
  GEM
2
2
  remote: https://rubygems.org/
3
3
  specs:
4
- actionpack (3.2.12)
5
- activemodel (= 3.2.12)
6
- activesupport (= 3.2.12)
7
- builder (~> 3.0.0)
4
+ actionpack (4.0.0)
5
+ activesupport (= 4.0.0)
6
+ builder (~> 3.1.0)
8
7
  erubis (~> 2.7.0)
9
- journey (~> 1.0.4)
10
- rack (~> 1.4.5)
11
- rack-cache (~> 1.2)
12
- rack-test (~> 0.6.1)
13
- sprockets (~> 2.2.1)
14
- activemodel (3.2.12)
15
- activesupport (= 3.2.12)
16
- builder (~> 3.0.0)
17
- activerecord (3.2.12)
18
- activemodel (= 3.2.12)
19
- activesupport (= 3.2.12)
20
- arel (~> 3.0.2)
21
- tzinfo (~> 0.3.29)
22
- activesupport (3.2.12)
23
- i18n (~> 0.6)
24
- multi_json (~> 1.0)
25
- arel (3.0.2)
26
- builder (3.0.4)
27
- diff-lcs (1.2.1)
8
+ rack (~> 1.5.2)
9
+ rack-test (~> 0.6.2)
10
+ activemodel (4.0.0)
11
+ activesupport (= 4.0.0)
12
+ builder (~> 3.1.0)
13
+ activerecord (4.0.0)
14
+ activemodel (= 4.0.0)
15
+ activerecord-deprecated_finders (~> 1.0.2)
16
+ activesupport (= 4.0.0)
17
+ arel (~> 4.0.0)
18
+ activerecord-deprecated_finders (1.0.3)
19
+ activesupport (4.0.0)
20
+ i18n (~> 0.6, >= 0.6.4)
21
+ minitest (~> 4.2)
22
+ multi_json (~> 1.3)
23
+ thread_safe (~> 0.1)
24
+ tzinfo (~> 0.3.37)
25
+ arel (4.0.0)
26
+ atomic (1.1.10)
27
+ builder (3.1.4)
28
+ diff-lcs (1.2.4)
28
29
  erubis (2.7.0)
29
- hike (1.2.1)
30
30
  i18n (0.6.4)
31
- journey (1.0.4)
32
- json (1.7.7)
33
- multi_json (1.6.1)
34
- rack (1.4.5)
35
- rack-cache (1.2)
36
- rack (>= 0.4)
37
- rack-ssl (1.3.3)
38
- rack
31
+ minitest (4.7.5)
32
+ multi_json (1.7.7)
33
+ rack (1.5.2)
39
34
  rack-test (0.6.2)
40
35
  rack (>= 1.0)
41
- railties (3.2.12)
42
- actionpack (= 3.2.12)
43
- activesupport (= 3.2.12)
44
- rack-ssl (~> 1.3.2)
36
+ railties (4.0.0)
37
+ actionpack (= 4.0.0)
38
+ activesupport (= 4.0.0)
45
39
  rake (>= 0.8.7)
46
- rdoc (~> 3.4)
47
- thor (>= 0.14.6, < 2.0)
48
- rake (10.0.3)
49
- rdoc (3.12.2)
50
- json (~> 1.4)
51
- rspec-core (2.13.1)
52
- rspec-expectations (2.13.0)
40
+ thor (>= 0.18.1, < 2.0)
41
+ rake (10.1.0)
42
+ rspec-core (2.14.4)
43
+ rspec-expectations (2.14.0)
53
44
  diff-lcs (>= 1.1.3, < 2.0)
54
- rspec-mocks (2.13.0)
55
- rspec-rails (2.13.0)
45
+ rspec-mocks (2.14.1)
46
+ rspec-rails (2.14.0)
56
47
  actionpack (>= 3.0)
57
48
  activesupport (>= 3.0)
58
49
  railties (>= 3.0)
59
- rspec-core (~> 2.13.0)
60
- rspec-expectations (~> 2.13.0)
61
- rspec-mocks (~> 2.13.0)
62
- sprockets (2.2.2)
63
- hike (~> 1.2)
64
- multi_json (~> 1.0)
65
- rack (~> 1.0)
66
- tilt (~> 1.1, != 1.3.0)
50
+ rspec-core (~> 2.14.0)
51
+ rspec-expectations (~> 2.14.0)
52
+ rspec-mocks (~> 2.14.0)
67
53
  sqlite3 (1.3.7)
68
- thor (0.17.0)
69
- tilt (1.3.5)
54
+ thor (0.18.1)
55
+ thread_safe (0.1.2)
56
+ atomic
70
57
  tzinfo (0.3.37)
71
58
 
72
59
  PLATFORMS
data/README.md CHANGED
@@ -1,32 +1,47 @@
1
- #lol_dba ![travis](https://api.travis-ci.org/plentz/lol_dba.png?branch=master)
1
+ #lol_dba [![travis](https://api.travis-ci.org/plentz/lol_dba.png?branch=master)](https://travis-ci.org/plentz/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
 
5
- ### Installation
5
+ ### Quick example
6
6
 
7
- Add lol_dba to your Gemfile:
7
+ To use lol_dba in the easiest way possible you have to do two things:
8
8
 
9
- gem "lol_dba"
9
+ gem install lol_dba
10
10
 
11
- and install gem
11
+ Then run one of the following commands...
12
12
 
13
- bundle install
13
+ To display a migration for adding/removing all necessary indexes based on associations:
14
+
15
+ lol_dba db:find_indexes
14
16
 
15
- ### Usage
17
+ To generate .sql files for your migrations inside db/migrate_sql folder:
16
18
 
17
- Display a migration for adding/removing all necessary indexes based on associations:
19
+ lol_dba db:migrate_sql # defaults to all migrations
20
+ lol_dba db:migrate_sql[pending] # only pending migrations
21
+ lol_dba db:migrate_sql[20120221205526] # generate sql only for migration 20120221205526
18
22
 
19
- rake db:find_indexes
23
+ ### Not-so-quick example
20
24
 
21
- Generate .sql files for all your migrations inside db/migrate_sql folder:
25
+ If you want to use lol_dba with rake, you should do a few more steps:
22
26
 
23
- rake db:migrate_sql
27
+ Add lol_dba to your Gemfile
24
28
 
25
- Note that create an index in a big database may take a long time.
29
+ gem "lol_dba"
30
+
31
+ Run the install command
32
+
33
+ bundle install
34
+
35
+ Use it the same way you use other rake commands
36
+
37
+ rake db:find_indexes
38
+ rake db:migrate_sql # defaults to all migrations
39
+ rake db:migrate_sql[pending] # only pending migrations
40
+ rake db:migrate_sql[20120221205526] # generate sql only for migration 20120221205526
26
41
 
27
42
  ### Compatibility
28
43
 
29
- Compatible with Ruby 1.9 and Rails 3.x. I think.
44
+ Compatible with Ruby 2.x and Rails 4.x (for now, we still support Ruby 1.9 and Rails 3.x).
30
45
 
31
46
  ### About primary_key
32
47
 
@@ -36,8 +51,9 @@ For this reason, we no longer suggest to add indexes to primary keys.
36
51
 
37
52
  ### Tests
38
53
 
39
- bundle install
40
- rake
54
+ To run lol_dba tests, just clone the repo and run:
55
+
56
+ bundle install && rake
41
57
 
42
58
  to run the tests.
43
59
 
@@ -53,6 +69,7 @@ All feedback, bug reports and thoughts on this gratefully received.
53
69
  * [Jay Fields](http://jayfields.com/)
54
70
  * [Muness Alrubaie](http://muness.blogspot.com/)
55
71
  * [Vladimir Sharshov](https://github.com/warpc)
72
+ * [Fabio Rehm](http://fabiorehm.com/)
56
73
 
57
74
  ### License
58
75
 
data/lib/lol_dba.rb CHANGED
@@ -78,7 +78,7 @@ EOM
78
78
 
79
79
  model_classes = []
80
80
  ObjectSpace.each_object(Module) do |obj|
81
- if Class == obj.class && obj != ActiveRecord::Base && obj.ancestors.include?(ActiveRecord::Base) && obj != ActiveRecord::SessionStore::Session
81
+ if Class == obj.class && obj != ActiveRecord::Base && obj.ancestors.include?(ActiveRecord::Base) && (!defined?(ActiveRecord::SessionStore::Session) || obj != ActiveRecord::SessionStore::Session)
82
82
  model_classes << obj
83
83
  end
84
84
  end
data/lib/lol_dba/cli.rb CHANGED
@@ -1,14 +1,18 @@
1
1
  require 'optparse'
2
+ require 'lol_dba/sql_generator'
2
3
 
3
4
  module LolDba
4
5
  class CLI
5
6
 
6
7
  class << self
7
8
  def start
8
- options = {:format => 'migration'}
9
+ options = {}
9
10
  OptionParser.new do |opts|
10
- opts.on("-f", '--format=[FORMAT]', 'Specify the format to be used (migration or sql), defaults to migration.') { |v| options[:format] = v }
11
11
  opts.on('-d', '--debug', 'Show stack traces when an error occurs.') { |v| options[:debug] = v }
12
+ opts.on_tail("-v", "--version", "Show version") do
13
+ puts LolDba::VERSION
14
+ exit
15
+ end
12
16
  end.parse!
13
17
  new(Dir.pwd, options).start
14
18
  end
@@ -20,34 +24,25 @@ module LolDba
20
24
 
21
25
  def start
22
26
  load_application
23
- validate_format!
24
- send("generate_#{@options[:format]}")
27
+ arg = ARGV.first
28
+ if arg =~ /db:find_indexes/
29
+ LolDba.simple_migration
30
+ elsif arg !~ /\[/
31
+ LolDba::SqlGenerator.generate("all")
32
+ else
33
+ which = arg.match(/.*\[(.*)\].*/).captures[0]
34
+ LolDba::SqlGenerator.generate(which)
35
+ end
25
36
  rescue Exception => e
26
- $stderr.puts "Failed: #{e.class}: #{e.message}"
37
+ $stderr.puts "Failed: #{e.class}: #{e.message}" if @options[:debug]
27
38
  $stderr.puts e.backtrace.map { |t| " from #{t}" } if @options[:debug]
28
39
  end
29
40
 
30
41
  protected
31
42
 
32
- def validate_format!
33
- unless self.respond_to?("generate_#{@options[:format]}")
34
- $stderr.puts "Unknown format: #{@options[:format]}"
35
- exit 1
36
- end
37
- end
38
-
39
43
  # Tks to https://github.com/voormedia/rails-erd/blob/master/lib/rails_erd/cli.rb
40
44
  def load_application
41
- $stderr.puts "Loading application in '#{File.basename(@path)}'..."
42
45
  require "#{@path}/config/environment"
43
46
  end
44
-
45
- def generate_sql
46
- LolDba::SqlGenerator.generate
47
- end
48
-
49
- def generate_migration
50
- LolDba.simple_migration
51
- end
52
47
  end
53
48
  end
@@ -7,13 +7,25 @@ module LolDba
7
7
  end
8
8
 
9
9
  def methods_to_modify
10
- [:execute, :do_execute, :rename_column, :change_column, :column_for, :tables, :indexes, :select_all] & connection.class.methods
10
+ [:execute, :do_execute, :rename_column, :change_column, :column_for, :tables, :indexes, :select_all] & connection.methods
11
11
  end
12
-
12
+
13
13
  def redefine_execute_methods
14
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) }
15
+ connection.class.send(:define_method, :execute) { |*args|
16
+ if args.first =~ /SELECT "schema_migrations"."version"/ || args.first =~ /^SHOW/
17
+ self.orig_execute(*args)
18
+ else
19
+ Writer.write(to_sql(args.first, args.last))
20
+ end
21
+ }
22
+ connection.class.send(:define_method, :do_execute) { |*args|
23
+ if args.first =~ /SELECT "schema_migrations"."version"/ || args.first =~ /^SHOW/
24
+ self.orig_do_execute(*args)
25
+ else
26
+ Writer.write(to_sql(args.first, args.last))
27
+ end
28
+ }
17
29
  connection.class.send(:define_method, :column_for) { |*args| args.last }
18
30
  connection.class.send(:define_method, :change_column) { |*args| [] }
19
31
  connection.class.send(:define_method, :rename_column) { |*args| [] }
@@ -31,7 +43,7 @@ module LolDba
31
43
 
32
44
  def reset_methods
33
45
  methods_to_modify.each do |method_name|
34
- connection.class.send(:alias_method, method_name, "orig_#{method_name}".to_sym)
46
+ connection.class.send(:alias_method, method_name, "orig_#{method_name}".to_sym) rescue nil
35
47
  end
36
48
  end
37
49
 
@@ -42,12 +54,29 @@ module LolDba
42
54
  reset_methods
43
55
  end
44
56
 
45
- def migrations
46
- Dir.glob(File.join(Rails.root, "db", "migrate", '*.rb'))
57
+ def migrations(which)
58
+ migrator = ActiveRecord::Migrator.new(:up, ActiveRecord::Migrator.migrations_path)
59
+ if which == "all"
60
+ migrator.migrations.collect { |m| m.filename }
61
+ elsif which == "pending"
62
+ pending = migrator.pending_migrations
63
+ if pending.empty?
64
+ puts "No pending migrations."
65
+ exit
66
+ end
67
+ migrator.pending_migrations.collect { |m| m.filename }
68
+ else
69
+ if migration = migrator.migrations.find {|m| m.version == which.to_i}
70
+ [migration.filename]
71
+ else
72
+ puts "There are no migrations for version #{which}."
73
+ exit
74
+ end
75
+ end
47
76
  end
48
77
 
49
- def generate
50
- generate_instead_of_executing { migrations.each { |file| up_and_down(file) } }
78
+ def generate(which)
79
+ generate_instead_of_executing { migrations(which).each { |file| up_and_down(file) } }
51
80
  end
52
81
 
53
82
  def up_and_down(file)
@@ -1,3 +1,3 @@
1
1
  module LolDba
2
- VERSION = "1.5.0" unless defined? LolDba::VERSION
2
+ VERSION = "1.6.0" unless defined? LolDba::VERSION
3
3
  end
@@ -6,9 +6,10 @@ namespace :db do
6
6
  task :find_indexes => :environment do
7
7
  LolDba.simple_migration
8
8
  end
9
- desc "Generate .sql files for all your migrations inside db/migrate_sql folder"
10
- task :migrate_sql => :environment do
11
- LolDba::SqlGenerator.generate
9
+ desc "Generate .sql files for your migrations inside db/migrate_sql folder"
10
+ task :migrate_sql, [:which] => :environment do |t, args|
11
+ args.with_defaults(:which => 'all')
12
+ LolDba::SqlGenerator.generate(args[:which])
12
13
  end
13
14
 
14
15
  end
data/spec/spec_helper.rb CHANGED
@@ -11,7 +11,7 @@ ActiveRecord::Base.establish_connection(
11
11
  :database => ":memory:"
12
12
  )
13
13
 
14
- class Rails
14
+ module Rails
15
15
  def self.root
16
16
  "spec/fixtures/"
17
17
  end
metadata CHANGED
@@ -1,8 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lol_dba
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.0
5
- prerelease:
4
+ version: 1.6.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Diego Plentz
@@ -13,54 +12,48 @@ authors:
13
12
  autorequire:
14
13
  bindir: bin
15
14
  cert_chain: []
16
- date: 2013-03-24 00:00:00.000000000 Z
15
+ date: 2013-07-27 00:00:00.000000000 Z
17
16
  dependencies:
18
17
  - !ruby/object:Gem::Dependency
19
18
  name: activerecord
20
19
  requirement: !ruby/object:Gem::Requirement
21
- none: false
22
20
  requirements:
23
- - - ! '>='
21
+ - - '>='
24
22
  - !ruby/object:Gem::Version
25
23
  version: '3.0'
26
24
  type: :runtime
27
25
  prerelease: false
28
26
  version_requirements: !ruby/object:Gem::Requirement
29
- none: false
30
27
  requirements:
31
- - - ! '>='
28
+ - - '>='
32
29
  - !ruby/object:Gem::Version
33
30
  version: '3.0'
34
31
  - !ruby/object:Gem::Dependency
35
32
  name: actionpack
36
33
  requirement: !ruby/object:Gem::Requirement
37
- none: false
38
34
  requirements:
39
- - - ! '>='
35
+ - - '>='
40
36
  - !ruby/object:Gem::Version
41
37
  version: '3.0'
42
38
  type: :runtime
43
39
  prerelease: false
44
40
  version_requirements: !ruby/object:Gem::Requirement
45
- none: false
46
41
  requirements:
47
- - - ! '>='
42
+ - - '>='
48
43
  - !ruby/object:Gem::Version
49
44
  version: '3.0'
50
45
  - !ruby/object:Gem::Dependency
51
46
  name: railties
52
47
  requirement: !ruby/object:Gem::Requirement
53
- none: false
54
48
  requirements:
55
- - - ! '>='
49
+ - - '>='
56
50
  - !ruby/object:Gem::Version
57
51
  version: '3.0'
58
52
  type: :runtime
59
53
  prerelease: false
60
54
  version_requirements: !ruby/object:Gem::Requirement
61
- none: false
62
55
  requirements:
63
- - - ! '>='
56
+ - - '>='
64
57
  - !ruby/object:Gem::Version
65
58
  version: '3.0'
66
59
  description: lol_dba is a small package of rake tasks that scan your application models
@@ -112,28 +105,27 @@ files:
112
105
  - spec/spec_helper.rb
113
106
  homepage: https://github.com/plentz/lol_dba
114
107
  licenses: []
108
+ metadata: {}
115
109
  post_install_message:
116
110
  rdoc_options: []
117
111
  require_paths:
118
112
  - lib
119
113
  - lib/lol_dba
120
114
  required_ruby_version: !ruby/object:Gem::Requirement
121
- none: false
122
115
  requirements:
123
- - - ! '>='
116
+ - - '>='
124
117
  - !ruby/object:Gem::Version
125
118
  version: '0'
126
119
  required_rubygems_version: !ruby/object:Gem::Requirement
127
- none: false
128
120
  requirements:
129
- - - ! '>='
121
+ - - '>='
130
122
  - !ruby/object:Gem::Version
131
123
  version: '0'
132
124
  requirements: []
133
125
  rubyforge_project:
134
- rubygems_version: 1.8.25
126
+ rubygems_version: 2.0.3
135
127
  signing_key:
136
- specification_version: 3
128
+ specification_version: 4
137
129
  summary: A small package of rake tasks to track down missing database indexes and
138
130
  generate sql migration scripts
139
131
  test_files: