active_record_doctor 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.rdoc +48 -0
- data/Rakefile +28 -0
- data/lib/active_record_doctor.rb +4 -0
- data/lib/active_record_doctor/printers.rb +4 -0
- data/lib/active_record_doctor/printers/io_printer.rb +15 -0
- data/lib/active_record_doctor/railtie.rb +7 -0
- data/lib/active_record_doctor/tasks.rb +4 -0
- data/lib/active_record_doctor/tasks/unindexed_foreign_keys.rb +50 -0
- data/lib/active_record_doctor/version.rb +3 -0
- data/lib/generators/active_record_doctor/add_indexes/USAGE +2 -0
- data/lib/generators/active_record_doctor/add_indexes/add_indexes_generator.rb +48 -0
- data/lib/tasks/active_record_doctor_tasks.rake +7 -0
- data/test/active_record_doctor/printers/io_printer_test.rb +20 -0
- data/test/active_record_doctor/tasks/unindexed_foreign_keys_test.rb +19 -0
- data/test/dummy/README.rdoc +28 -0
- data/test/dummy/Rakefile +6 -0
- data/test/dummy/app/assets/javascripts/application.js +13 -0
- data/test/dummy/app/assets/stylesheets/application.css +15 -0
- data/test/dummy/app/controllers/application_controller.rb +5 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/models/employer.rb +2 -0
- data/test/dummy/app/models/profile.rb +2 -0
- data/test/dummy/app/models/user.rb +3 -0
- data/test/dummy/app/views/layouts/application.html.erb +14 -0
- data/test/dummy/bin/bundle +3 -0
- data/test/dummy/bin/rails +4 -0
- data/test/dummy/bin/rake +4 -0
- data/test/dummy/bin/setup +29 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/config/application.rb +26 -0
- data/test/dummy/config/boot.rb +5 -0
- data/test/dummy/config/database.yml +25 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +41 -0
- data/test/dummy/config/environments/production.rb +79 -0
- data/test/dummy/config/environments/test.rb +42 -0
- data/test/dummy/config/initializers/assets.rb +11 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
- data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/test/dummy/config/initializers/inflections.rb +16 -0
- data/test/dummy/config/initializers/mime_types.rb +4 -0
- data/test/dummy/config/initializers/session_store.rb +3 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/test/dummy/config/locales/en.yml +23 -0
- data/test/dummy/config/routes.rb +56 -0
- data/test/dummy/config/secrets.yml +22 -0
- data/test/dummy/db/development.sqlite3 +0 -0
- data/test/dummy/db/migrate/20160213101213_create_users.rb +14 -0
- data/test/dummy/db/migrate/20160213101232_create_profiles.rb +10 -0
- data/test/dummy/db/migrate/20160213102131_create_employers.rb +9 -0
- data/test/dummy/db/schema.rb +41 -0
- data/test/dummy/db/test.sqlite3 +0 -0
- data/test/dummy/log/development.log +654 -0
- data/test/dummy/log/test.log +10337 -0
- data/test/dummy/public/404.html +67 -0
- data/test/dummy/public/422.html +67 -0
- data/test/dummy/public/500.html +66 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/support/spy_printer.rb +11 -0
- data/test/test_helper.rb +20 -0
- metadata +182 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d57d331fcbdfdc3db39937880f3828ef85f03c62
|
4
|
+
data.tar.gz: a7ae1374e2328bc93997d9adec3dece14a5db7fd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2410d9d14530b7fffa606263a86d2158a2c5e0d20487c90cfd265b998c179b22044c9dee1177f22fcca9d3e117539332b7dc0bcc8c8c733a798437e1e125bdd4
|
7
|
+
data.tar.gz: 5f30d3ad0e739b262985129e5870ee3af08d212e2124db87dbe03e2d3670b14c8726805d9db0a55782ff30d9f783e30f3a76f11dcbae56de7574dad8c0511281
|
data/README.rdoc
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
= Active Record Doctor
|
2
|
+
|
3
|
+
Active Record Doctor helps to keep the database in a good shape. Currently, it
|
4
|
+
can index unindexed foreign keys. More features coming soon!
|
5
|
+
|
6
|
+
{<img src="https://travis-ci.org/gregnavis/active_record_doctor.svg?branch=master" alt="Build Status" />}[https://travis-ci.org/gregnavis/active_record_doctor]
|
7
|
+
|
8
|
+
== Installation
|
9
|
+
|
10
|
+
The preferred installation method is adding +active_record_doctor+ to your
|
11
|
+
+Gemfile+:
|
12
|
+
|
13
|
+
# git: is required as the gem hasn't been released yet
|
14
|
+
gem 'active_record_doctor', group: :development,
|
15
|
+
github: 'gregnavis/active-record-doctor'
|
16
|
+
|
17
|
+
Then run:
|
18
|
+
|
19
|
+
bundle install
|
20
|
+
|
21
|
+
== Usage
|
22
|
+
|
23
|
+
=== Indexing Unindexed Foreign Keys
|
24
|
+
|
25
|
+
Foreign keys should be indexed unless it's proven ineffective. However, Rails
|
26
|
+
makes it easy to create an unindexed foreign key. Active Record Doctor can
|
27
|
+
automatically generate database migrations that add the missing indexes. It's a
|
28
|
+
three-step process:
|
29
|
+
|
30
|
+
1. Generate a list of unindexed foreign keys by running
|
31
|
+
|
32
|
+
rake active_record_doctor:unindexed_foreign_keys > unindexed_foreign_keys.txt
|
33
|
+
|
34
|
+
2. Remove columns that should _not_ be indexed from +unindexed_foreign_keys.txt+
|
35
|
+
as a column can look like a foreign key (i.e. end with +_id+) without being
|
36
|
+
one.
|
37
|
+
|
38
|
+
3. Generate the migrations
|
39
|
+
|
40
|
+
rails generate active_record_doctor:add_indexes unindexed_foreign_keys.txt
|
41
|
+
|
42
|
+
4. Run the migrations
|
43
|
+
|
44
|
+
rake db:migrate
|
45
|
+
|
46
|
+
== Author
|
47
|
+
|
48
|
+
This gem is developed and maintained by {Greg Navis}[http://www.gregnavis.com].
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'ActiveRecordDoctor'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.rdoc')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
Bundler::GemHelper.install_tasks
|
18
|
+
|
19
|
+
require 'rake/testtask'
|
20
|
+
|
21
|
+
Rake::TestTask.new(:test) do |t|
|
22
|
+
t.libs << 'lib'
|
23
|
+
t.libs << 'test'
|
24
|
+
t.pattern = 'test/**/*_test.rb'
|
25
|
+
t.verbose = false
|
26
|
+
end
|
27
|
+
|
28
|
+
task default: :test
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module ActiveRecordDoctor
|
2
|
+
module Printers
|
3
|
+
class IOPrinter
|
4
|
+
def initialize(io: STDOUT)
|
5
|
+
@io = io
|
6
|
+
end
|
7
|
+
|
8
|
+
def print_unindexed_foreign_keys(unindexed_foreign_keys)
|
9
|
+
@io.puts(unindexed_foreign_keys.sort.map do |table, columns|
|
10
|
+
"#{table} #{columns.sort.join(' ')}"
|
11
|
+
end.join("\n"))
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require "active_record_doctor/printers/io_printer"
|
2
|
+
|
3
|
+
module ActiveRecordDoctor
|
4
|
+
module Tasks
|
5
|
+
class UnindexedForeignKeys
|
6
|
+
def self.run
|
7
|
+
new.run
|
8
|
+
end
|
9
|
+
|
10
|
+
def initialize(printer: ActiveRecordDoctor::Printers::IOPrinter.new)
|
11
|
+
@printer = printer
|
12
|
+
end
|
13
|
+
|
14
|
+
def run
|
15
|
+
@printer.print_unindexed_foreign_keys(unindexed_foreign_keys)
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def unindexed_foreign_keys
|
21
|
+
connection.tables.select do |table|
|
22
|
+
"schema_migrations" != table
|
23
|
+
end.map do |table|
|
24
|
+
[
|
25
|
+
table,
|
26
|
+
connection.columns(table).select do |column|
|
27
|
+
foreign_key?(table, column) && !indexed?(table, column)
|
28
|
+
end.map(&:name)
|
29
|
+
]
|
30
|
+
end.select do |table, columns|
|
31
|
+
!columns.empty?
|
32
|
+
end.to_h
|
33
|
+
end
|
34
|
+
|
35
|
+
def foreign_key?(table, column)
|
36
|
+
column.name.end_with?("_id")
|
37
|
+
end
|
38
|
+
|
39
|
+
def indexed?(table, column)
|
40
|
+
connection.indexes(table).any? do |index|
|
41
|
+
index.columns.first == column.name
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def connection
|
46
|
+
@connection ||= ActiveRecord::Base.connection
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module ActiveRecordDoctor
|
2
|
+
class AddIndexesGenerator < Rails::Generators::Base
|
3
|
+
MigrationDescription = Struct.new(:table, :columns)
|
4
|
+
|
5
|
+
desc 'Generate migrations for the specified indexes'
|
6
|
+
argument :path, type: :string, default: nil, banner: 'PATH'
|
7
|
+
|
8
|
+
def create_migrations
|
9
|
+
migration_descriptions = read_migration_descriptions(path)
|
10
|
+
now = Time.now
|
11
|
+
|
12
|
+
migration_descriptions.each_with_index do |migration_description, index|
|
13
|
+
timestamp = (now + index).strftime("%Y%m%d%H%M%S")
|
14
|
+
file_name = "db/migrate/#{timestamp}_index_foreign_keys_in_#{migration_description.table}.rb"
|
15
|
+
create_file(file_name, content(migration_description))
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def read_migration_descriptions(path)
|
22
|
+
File.readlines(path).map do |line|
|
23
|
+
table, *columns = line.split(" ")
|
24
|
+
MigrationDescription.new(table, columns)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def content(migration_description)
|
29
|
+
<<EOF
|
30
|
+
class IndexForeignKeysIn#{migration_description.table.camelize} < ActiveRecord::Migration
|
31
|
+
def change
|
32
|
+
#{add_indexes(migration_description)}
|
33
|
+
end
|
34
|
+
end
|
35
|
+
EOF
|
36
|
+
end
|
37
|
+
|
38
|
+
def add_indexes(migration_description)
|
39
|
+
migration_description.columns.map do |column|
|
40
|
+
add_index(migration_description.table, column)
|
41
|
+
end.join("\n")
|
42
|
+
end
|
43
|
+
|
44
|
+
def add_index(table, column)
|
45
|
+
" add_index :#{table}, :#{column}"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
require 'active_record_doctor/printers/io_printer'
|
4
|
+
|
5
|
+
class ActiveRecordDoctor::Printers::IOPrinterTest < ActiveSupport::TestCase
|
6
|
+
def test_print_unindexed_foreign_keys
|
7
|
+
assert_equal(<<EOF, print_unindexed_foreign_keys({ "users" => ["profile_id", "account_id"], "account" => ["group_id"] }))
|
8
|
+
account group_id
|
9
|
+
users account_id profile_id
|
10
|
+
EOF
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def print_unindexed_foreign_keys(argument)
|
16
|
+
io = StringIO.new
|
17
|
+
ActiveRecordDoctor::Printers::IOPrinter.new(io: io).print_unindexed_foreign_keys(argument)
|
18
|
+
io.string
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
require 'active_record_doctor/tasks/unindexed_foreign_keys'
|
4
|
+
|
5
|
+
class ActiveRecordDoctor::Tasks::UnindexedForeignKeysTest < ActiveSupport::TestCase
|
6
|
+
def test_unindexed_foreign_keys_are_reported
|
7
|
+
result = run_task
|
8
|
+
|
9
|
+
assert_equal([{ "users" => ["profile_id"] }], result)
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def run_task
|
15
|
+
printer = SpyPrinter.new
|
16
|
+
ActiveRecordDoctor::Tasks::UnindexedForeignKeys.new(printer: printer).run
|
17
|
+
printer.unindexed_foreign_keys
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
== README
|
2
|
+
|
3
|
+
This README would normally document whatever steps are necessary to get the
|
4
|
+
application up and running.
|
5
|
+
|
6
|
+
Things you may want to cover:
|
7
|
+
|
8
|
+
* Ruby version
|
9
|
+
|
10
|
+
* System dependencies
|
11
|
+
|
12
|
+
* Configuration
|
13
|
+
|
14
|
+
* Database creation
|
15
|
+
|
16
|
+
* Database initialization
|
17
|
+
|
18
|
+
* How to run the test suite
|
19
|
+
|
20
|
+
* Services (job queues, cache servers, search engines, etc.)
|
21
|
+
|
22
|
+
* Deployment instructions
|
23
|
+
|
24
|
+
* ...
|
25
|
+
|
26
|
+
|
27
|
+
Please feel free to use a different markup language if you do not plan to run
|
28
|
+
<tt>rake doc:app</tt>.
|
data/test/dummy/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
2
|
+
// listed below.
|
3
|
+
//
|
4
|
+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
5
|
+
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
|
6
|
+
//
|
7
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
8
|
+
// compiled file.
|
9
|
+
//
|
10
|
+
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
|
11
|
+
// about supported directives.
|
12
|
+
//
|
13
|
+
//= require_tree .
|
@@ -0,0 +1,15 @@
|
|
1
|
+
/*
|
2
|
+
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
3
|
+
* listed below.
|
4
|
+
*
|
5
|
+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
|
6
|
+
* or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
|
7
|
+
*
|
8
|
+
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
|
9
|
+
* compiled file so the styles you add here take precedence over styles defined in any styles
|
10
|
+
* defined in the other CSS/SCSS files in this directory. It is generally better to create a new
|
11
|
+
* file per style scope.
|
12
|
+
*
|
13
|
+
*= require_tree .
|
14
|
+
*= require_self
|
15
|
+
*/
|
@@ -0,0 +1,14 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>Dummy</title>
|
5
|
+
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
|
6
|
+
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
|
7
|
+
<%= csrf_meta_tags %>
|
8
|
+
</head>
|
9
|
+
<body>
|
10
|
+
|
11
|
+
<%= yield %>
|
12
|
+
|
13
|
+
</body>
|
14
|
+
</html>
|
data/test/dummy/bin/rake
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'pathname'
|
3
|
+
|
4
|
+
# path to your application root.
|
5
|
+
APP_ROOT = Pathname.new File.expand_path('../../', __FILE__)
|
6
|
+
|
7
|
+
Dir.chdir APP_ROOT do
|
8
|
+
# This script is a starting point to setup your application.
|
9
|
+
# Add necessary setup steps to this file:
|
10
|
+
|
11
|
+
puts "== Installing dependencies =="
|
12
|
+
system "gem install bundler --conservative"
|
13
|
+
system "bundle check || bundle install"
|
14
|
+
|
15
|
+
# puts "\n== Copying sample files =="
|
16
|
+
# unless File.exist?("config/database.yml")
|
17
|
+
# system "cp config/database.yml.sample config/database.yml"
|
18
|
+
# end
|
19
|
+
|
20
|
+
puts "\n== Preparing database =="
|
21
|
+
system "bin/rake db:setup"
|
22
|
+
|
23
|
+
puts "\n== Removing old logs and tempfiles =="
|
24
|
+
system "rm -f log/*"
|
25
|
+
system "rm -rf tmp/cache"
|
26
|
+
|
27
|
+
puts "\n== Restarting application server =="
|
28
|
+
system "touch tmp/restart.txt"
|
29
|
+
end
|