lazy_susan 0.7.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/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +23 -0
- data/README.md +29 -0
- data/Rakefile +2 -0
- data/lazy_susan.gemspec +18 -0
- data/lib/generators/lazy_susan_generator.rb +97 -0
- data/lib/lazy_susan.rb +5 -0
- data/lib/lazy_susan/version.rb +3 -0
- metadata +58 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
Copyright (c) 2012 Mark Tabler
|
2
|
+
|
3
|
+
|
4
|
+
MIT License
|
5
|
+
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
7
|
+
a copy of this software and associated documentation files (the
|
8
|
+
"Software"), to deal in the Software without restriction, including
|
9
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
10
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
11
|
+
permit persons to whom the Software is furnished to do so, subject to
|
12
|
+
the following conditions:
|
13
|
+
|
14
|
+
The above copyright notice and this permission notice shall be
|
15
|
+
included in all copies or substantial portions of the Software.
|
16
|
+
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
18
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
19
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
20
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
21
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
22
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
23
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# LazySusan
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'lazy_susan'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install lazy_susan
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lazy_susan.gemspec
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/lazy_susan/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Mark Tabler\n"]
|
6
|
+
gem.email = ["mark.tabler@fallingmanstudios.net"]
|
7
|
+
gem.description = %q{An Automatic Index Migration Generator}
|
8
|
+
gem.summary = %q{LazySusan generates index migrations where they are needed; which is to say any column detected as a primary or foreign key that does not already have an index.}
|
9
|
+
|
10
|
+
gem.homepage = ""
|
11
|
+
|
12
|
+
gem.files = `git ls-files`.split($\)
|
13
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
14
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
15
|
+
gem.name = "lazy_susan"
|
16
|
+
gem.require_paths = ["lib", "lib/generators"]
|
17
|
+
gem.version = LazySusan::VERSION
|
18
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
class LazySusanGenerator < Rails::Generators::Base
|
2
|
+
DATABASE = ActiveRecord::Base.connection
|
3
|
+
def generate_lazy_susan
|
4
|
+
results = {}
|
5
|
+
DATABASE.tables.each do |table_name|
|
6
|
+
missing_indexes = gather_missing_indexes(table_name)
|
7
|
+
results[table_name] = missing_indexes if missing_indexes.any?
|
8
|
+
end
|
9
|
+
if results.keys.any?
|
10
|
+
generate_migration(results)
|
11
|
+
else
|
12
|
+
puts "No missing indexes found."
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def gather_missing_indexes(table_name)
|
19
|
+
results = []
|
20
|
+
results += columns_to_index(table_name)
|
21
|
+
results += joins_to_index(table_name)
|
22
|
+
results
|
23
|
+
end
|
24
|
+
|
25
|
+
def columns_to_index(table_name)
|
26
|
+
results = []
|
27
|
+
columns_to_check = columns_needing_indexes(table_name)
|
28
|
+
columns_to_check.each do |column_name|
|
29
|
+
results << column_name unless DATABASE.index_exists?(table_name, column_name)
|
30
|
+
end
|
31
|
+
results
|
32
|
+
end
|
33
|
+
|
34
|
+
def joins_to_index(table_name)
|
35
|
+
results = []
|
36
|
+
joins_to_check = joins_needing_indexes(table_name)
|
37
|
+
joins_to_check.each do |join|
|
38
|
+
results << join unless DATABASE.index_exists?(table_name, join)
|
39
|
+
results << join.reverse unless DATABASE.index_exists?(table_name, join.reverse)
|
40
|
+
end
|
41
|
+
results
|
42
|
+
end
|
43
|
+
|
44
|
+
# Every column name that ends in _id, or is "id", should be indexed.
|
45
|
+
def columns_needing_indexes(table_name)
|
46
|
+
results = []
|
47
|
+
DATABASE.columns(table_name).each do |column|
|
48
|
+
results << column.name.to_sym if column.name.last(3) == "_id"
|
49
|
+
results << column.name.to_sym if column.name == "id"
|
50
|
+
end
|
51
|
+
results
|
52
|
+
end
|
53
|
+
|
54
|
+
# If a table has two column names ending in _id, it's probably a join
|
55
|
+
# and should be indexed for join lookups in either direction.
|
56
|
+
def joins_needing_indexes(table_name)
|
57
|
+
results = columns_needing_indexes(table_name).reject { |name| name == :id }
|
58
|
+
results.size == 2 ? [results.map(&:to_sym)] : []
|
59
|
+
end
|
60
|
+
|
61
|
+
def generate_migration(missing_indexes)
|
62
|
+
puts "Generating missing index migration."
|
63
|
+
puts timestamped_migration_filename(missing_indexes)
|
64
|
+
migration_file = File.open("#{Rails.root}/db/migrate/#{timestamped_migration_filename(missing_indexes)}", "w") do |f|
|
65
|
+
f.puts header_boilerplate(missing_indexes)
|
66
|
+
f.puts migration_lines(missing_indexes)
|
67
|
+
f.puts footer_boilerplate
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def timestamped_migration_filename(missing_indexes)
|
72
|
+
Time.now.utc.strftime("%Y%m%d%H%M%S") + "_lazy_susan_for_#{missing_indexes.first.first}.rb"
|
73
|
+
end
|
74
|
+
|
75
|
+
def header_boilerplate(missing_indexes)
|
76
|
+
"class LazySusanFor#{missing_indexes.first.first.classify.pluralize} < ActiveRecord::Migration
|
77
|
+
def change"
|
78
|
+
end
|
79
|
+
|
80
|
+
def footer_boilerplate
|
81
|
+
" end\nend"
|
82
|
+
end
|
83
|
+
|
84
|
+
def migration_lines(missing_indexes)
|
85
|
+
results = ""
|
86
|
+
missing_indexes.each do |table_name, indexes|
|
87
|
+
indexes.each do |index|
|
88
|
+
if index.class == Array
|
89
|
+
results << " add_index :#{table_name}, #{index}\n"
|
90
|
+
else
|
91
|
+
results << " add_index :#{table_name}, :#{index}\n"
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
results
|
96
|
+
end
|
97
|
+
end
|
data/lib/lazy_susan.rb
ADDED
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lazy_susan
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.7.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- ! 'Mark Tabler
|
9
|
+
|
10
|
+
'
|
11
|
+
autorequire:
|
12
|
+
bindir: bin
|
13
|
+
cert_chain: []
|
14
|
+
date: 2012-05-10 00:00:00.000000000 Z
|
15
|
+
dependencies: []
|
16
|
+
description: An Automatic Index Migration Generator
|
17
|
+
email:
|
18
|
+
- mark.tabler@fallingmanstudios.net
|
19
|
+
executables: []
|
20
|
+
extensions: []
|
21
|
+
extra_rdoc_files: []
|
22
|
+
files:
|
23
|
+
- .gitignore
|
24
|
+
- Gemfile
|
25
|
+
- LICENSE
|
26
|
+
- README.md
|
27
|
+
- Rakefile
|
28
|
+
- lazy_susan.gemspec
|
29
|
+
- lib/generators/lazy_susan_generator.rb
|
30
|
+
- lib/lazy_susan.rb
|
31
|
+
- lib/lazy_susan/version.rb
|
32
|
+
homepage: ''
|
33
|
+
licenses: []
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options: []
|
36
|
+
require_paths:
|
37
|
+
- lib
|
38
|
+
- lib/generators
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
|
+
none: false
|
47
|
+
requirements:
|
48
|
+
- - ! '>='
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
requirements: []
|
52
|
+
rubyforge_project:
|
53
|
+
rubygems_version: 1.8.19
|
54
|
+
signing_key:
|
55
|
+
specification_version: 3
|
56
|
+
summary: LazySusan generates index migrations where they are needed; which is to say
|
57
|
+
any column detected as a primary or foreign key that does not already have an index.
|
58
|
+
test_files: []
|