active_record_doctor 1.5.0 → 1.8.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 +5 -5
- data/README.md +152 -12
- data/lib/active_record_doctor.rb +20 -2
- data/lib/active_record_doctor/detectors.rb +13 -0
- data/lib/active_record_doctor/detectors/base.rb +64 -0
- data/lib/active_record_doctor/{tasks → detectors}/extraneous_indexes.rb +12 -29
- data/lib/active_record_doctor/detectors/incorrect_boolean_presence_validation.rb +40 -0
- data/lib/active_record_doctor/detectors/incorrect_dependent_option.rb +71 -0
- data/lib/active_record_doctor/{tasks → detectors}/missing_foreign_keys.rb +17 -35
- data/lib/active_record_doctor/detectors/missing_non_null_constraint.rb +60 -0
- data/lib/active_record_doctor/detectors/missing_presence_validation.rb +78 -0
- data/lib/active_record_doctor/detectors/missing_unique_indexes.rb +61 -0
- data/lib/active_record_doctor/detectors/undefined_table_references.rb +34 -0
- data/lib/active_record_doctor/detectors/unindexed_deleted_at.rb +29 -0
- data/lib/active_record_doctor/detectors/unindexed_foreign_keys.rb +48 -0
- data/lib/active_record_doctor/printers.rb +3 -1
- data/lib/active_record_doctor/printers/io_printer.rb +101 -26
- data/lib/active_record_doctor/railtie.rb +3 -1
- data/lib/active_record_doctor/task.rb +28 -0
- data/lib/active_record_doctor/version.rb +3 -1
- data/lib/generators/active_record_doctor/add_indexes/add_indexes_generator.rb +15 -11
- data/lib/tasks/active_record_doctor.rake +33 -0
- data/test/active_record_doctor/detectors/extraneous_indexes_test.rb +67 -0
- data/test/active_record_doctor/detectors/incorrect_boolean_presence_validation_test.rb +36 -0
- data/test/active_record_doctor/detectors/incorrect_dependent_option_test.rb +117 -0
- data/test/active_record_doctor/detectors/missing_foreign_keys_test.rb +24 -0
- data/test/active_record_doctor/detectors/missing_non_null_constraint_test.rb +102 -0
- data/test/active_record_doctor/detectors/missing_presence_validation_test.rb +107 -0
- data/test/active_record_doctor/detectors/missing_unique_indexes_test.rb +114 -0
- data/test/active_record_doctor/detectors/undefined_table_references_test.rb +44 -0
- data/test/active_record_doctor/detectors/unindexed_deleted_at_test.rb +67 -0
- data/test/active_record_doctor/detectors/unindexed_foreign_keys_test.rb +26 -0
- data/test/active_record_doctor/printers/io_printer_test.rb +23 -10
- data/test/model_factory.rb +78 -0
- data/test/setup.rb +126 -0
- metadata +93 -149
- data/Rakefile +0 -28
- data/lib/active_record_doctor/compatibility.rb +0 -11
- data/lib/active_record_doctor/tasks.rb +0 -4
- data/lib/active_record_doctor/tasks/undefined_table_references.rb +0 -34
- data/lib/active_record_doctor/tasks/unindexed_deleted_at.rb +0 -40
- data/lib/active_record_doctor/tasks/unindexed_foreign_keys.rb +0 -66
- data/lib/tasks/active_record_doctor_tasks.rake +0 -27
- data/test/active_record_doctor/tasks/extraneous_indexes_test.rb +0 -27
- data/test/active_record_doctor/tasks/missing_foreign_keys_test.rb +0 -19
- data/test/active_record_doctor/tasks/undefined_table_references_test.rb +0 -19
- data/test/active_record_doctor/tasks/unindexed_deleted_at_test.rb +0 -19
- data/test/active_record_doctor/tasks/unindexed_foreign_keys_test.rb +0 -19
- data/test/dummy/README.rdoc +0 -28
- data/test/dummy/Rakefile +0 -6
- data/test/dummy/app/assets/javascripts/application.js +0 -13
- data/test/dummy/app/assets/stylesheets/application.css +0 -15
- data/test/dummy/app/controllers/application_controller.rb +0 -5
- data/test/dummy/app/helpers/application_helper.rb +0 -2
- data/test/dummy/app/models/application_record.rb +0 -3
- data/test/dummy/app/models/comment.rb +0 -3
- data/test/dummy/app/models/contract.rb +0 -3
- data/test/dummy/app/models/employer.rb +0 -2
- data/test/dummy/app/models/profile.rb +0 -2
- data/test/dummy/app/models/user.rb +0 -3
- data/test/dummy/app/views/layouts/application.html.erb +0 -14
- data/test/dummy/bin/bundle +0 -3
- data/test/dummy/bin/rails +0 -4
- data/test/dummy/bin/rake +0 -4
- data/test/dummy/bin/setup +0 -29
- data/test/dummy/config.ru +0 -4
- data/test/dummy/config/application.rb +0 -23
- data/test/dummy/config/boot.rb +0 -5
- data/test/dummy/config/database.yml +0 -19
- data/test/dummy/config/database.yml.travis +0 -5
- data/test/dummy/config/environment.rb +0 -5
- data/test/dummy/config/environments/development.rb +0 -41
- data/test/dummy/config/environments/production.rb +0 -79
- data/test/dummy/config/environments/test.rb +0 -47
- data/test/dummy/config/initializers/assets.rb +0 -11
- data/test/dummy/config/initializers/backtrace_silencers.rb +0 -7
- data/test/dummy/config/initializers/cookies_serializer.rb +0 -3
- data/test/dummy/config/initializers/filter_parameter_logging.rb +0 -4
- data/test/dummy/config/initializers/inflections.rb +0 -16
- data/test/dummy/config/initializers/mime_types.rb +0 -4
- data/test/dummy/config/initializers/session_store.rb +0 -3
- data/test/dummy/config/initializers/wrap_parameters.rb +0 -14
- data/test/dummy/config/locales/en.yml +0 -23
- data/test/dummy/config/routes.rb +0 -56
- data/test/dummy/config/secrets.yml +0 -22
- data/test/dummy/db/migrate/20160213101213_create_employers.rb +0 -15
- data/test/dummy/db/migrate/20160213101221_create_users.rb +0 -23
- data/test/dummy/db/migrate/20160213101232_create_profiles.rb +0 -15
- data/test/dummy/db/migrate/20160604081452_create_comments.rb +0 -11
- data/test/dummy/db/migrate/base_migration.rb +0 -5
- data/test/dummy/db/schema.rb +0 -68
- data/test/dummy/log/development.log +0 -532
- data/test/dummy/log/test.log +0 -2699
- data/test/dummy/public/404.html +0 -67
- data/test/dummy/public/422.html +0 -67
- data/test/dummy/public/500.html +0 -66
- data/test/dummy/public/favicon.ico +0 -0
- data/test/support/spy_printer.rb +0 -52
- data/test/test_helper.rb +0 -20
data/Rakefile
DELETED
@@ -1,28 +0,0 @@
|
|
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
|
@@ -1,34 +0,0 @@
|
|
1
|
-
require "active_record_doctor/compatibility"
|
2
|
-
require "active_record_doctor/printers/io_printer"
|
3
|
-
|
4
|
-
module ActiveRecordDoctor
|
5
|
-
module Tasks
|
6
|
-
class UndefinedTableReferences
|
7
|
-
include Compatibility
|
8
|
-
|
9
|
-
def self.run
|
10
|
-
new.run
|
11
|
-
end
|
12
|
-
|
13
|
-
def initialize(printer: ActiveRecordDoctor::Printers::IOPrinter.new)
|
14
|
-
@printer = printer
|
15
|
-
end
|
16
|
-
|
17
|
-
def run
|
18
|
-
@printer.print_undefined_table_references(undefined_table_references)
|
19
|
-
undefined_table_references.present? ? 1 : 0
|
20
|
-
end
|
21
|
-
|
22
|
-
private
|
23
|
-
|
24
|
-
def undefined_table_references
|
25
|
-
Rails.application.eager_load!
|
26
|
-
|
27
|
-
ActiveRecord::Base.descendants.select do |model|
|
28
|
-
model.table_name.present? &&
|
29
|
-
!model.connection.tables.include?(model.table_name)
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
@@ -1,40 +0,0 @@
|
|
1
|
-
require "active_record_doctor/compatibility"
|
2
|
-
require "active_record_doctor/printers/io_printer"
|
3
|
-
|
4
|
-
module ActiveRecordDoctor
|
5
|
-
module Tasks
|
6
|
-
class UnindexedDeletedAt
|
7
|
-
include Compatibility
|
8
|
-
|
9
|
-
def self.run
|
10
|
-
new.run
|
11
|
-
end
|
12
|
-
|
13
|
-
def initialize(printer: ActiveRecordDoctor::Printers::IOPrinter.new)
|
14
|
-
@printer = printer
|
15
|
-
end
|
16
|
-
|
17
|
-
def run
|
18
|
-
@printer.print_unindexed_deleted_at(unindexed_deleted_at)
|
19
|
-
end
|
20
|
-
|
21
|
-
private
|
22
|
-
|
23
|
-
def unindexed_deleted_at
|
24
|
-
connection.tables.select do |table|
|
25
|
-
connection.columns(table).map(&:name).include?('deleted_at')
|
26
|
-
end.flat_map do |table|
|
27
|
-
connection.indexes(table).reject do |index|
|
28
|
-
index.where =~ /\bdeleted_at\s+IS\s+NULL\b/i
|
29
|
-
end.map do |index|
|
30
|
-
index.name
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
def connection
|
36
|
-
@connection ||= ActiveRecord::Base.connection
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
@@ -1,66 +0,0 @@
|
|
1
|
-
require "active_record_doctor/compatibility"
|
2
|
-
require "active_record_doctor/printers/io_printer"
|
3
|
-
|
4
|
-
module ActiveRecordDoctor
|
5
|
-
module Tasks
|
6
|
-
class UnindexedForeignKeys
|
7
|
-
include Compatibility
|
8
|
-
|
9
|
-
def self.run
|
10
|
-
new.run
|
11
|
-
end
|
12
|
-
|
13
|
-
def initialize(printer: ActiveRecordDoctor::Printers::IOPrinter.new)
|
14
|
-
@printer = printer
|
15
|
-
end
|
16
|
-
|
17
|
-
def run
|
18
|
-
@printer.print_unindexed_foreign_keys(unindexed_foreign_keys)
|
19
|
-
end
|
20
|
-
|
21
|
-
private
|
22
|
-
|
23
|
-
def unindexed_foreign_keys
|
24
|
-
hash_from_pairs(connection_tables.select do |table|
|
25
|
-
"schema_migrations" != table
|
26
|
-
end.map do |table|
|
27
|
-
[
|
28
|
-
table,
|
29
|
-
connection.columns(table).select do |column|
|
30
|
-
foreign_key?(table, column) &&
|
31
|
-
!indexed?(table, column) &&
|
32
|
-
!indexed_as_polymorphic?(table, column)
|
33
|
-
end.map(&:name)
|
34
|
-
]
|
35
|
-
end.select do |table, columns|
|
36
|
-
!columns.empty?
|
37
|
-
end)
|
38
|
-
end
|
39
|
-
|
40
|
-
def foreign_key?(table, column)
|
41
|
-
column.name.end_with?("_id")
|
42
|
-
end
|
43
|
-
|
44
|
-
def indexed?(table, column)
|
45
|
-
connection.indexes(table).any? do |index|
|
46
|
-
index.columns.first == column.name
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
def indexed_as_polymorphic?(table, column)
|
51
|
-
type_column_name = column.name.sub(/_id\Z/, '_type')
|
52
|
-
connection.indexes(table).any? do |index|
|
53
|
-
index.columns == [type_column_name, column.name]
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
def connection
|
58
|
-
@connection ||= ActiveRecord::Base.connection
|
59
|
-
end
|
60
|
-
|
61
|
-
def hash_from_pairs(pairs)
|
62
|
-
Hash[*pairs.flatten(1)]
|
63
|
-
end
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
@@ -1,27 +0,0 @@
|
|
1
|
-
require "active_record_doctor/tasks/unindexed_foreign_keys"
|
2
|
-
require "active_record_doctor/tasks/extraneous_indexes"
|
3
|
-
require "active_record_doctor/tasks/missing_foreign_keys"
|
4
|
-
require "active_record_doctor/tasks/undefined_table_references"
|
5
|
-
require "active_record_doctor/tasks/unindexed_deleted_at"
|
6
|
-
|
7
|
-
namespace :active_record_doctor do
|
8
|
-
task :unindexed_foreign_keys => :environment do
|
9
|
-
ActiveRecordDoctor::Tasks::UnindexedForeignKeys.run
|
10
|
-
end
|
11
|
-
|
12
|
-
task :extraneous_indexes => :environment do
|
13
|
-
ActiveRecordDoctor::Tasks::ExtraneousIndexes.run
|
14
|
-
end
|
15
|
-
|
16
|
-
task :missing_foreign_keys => :environment do
|
17
|
-
ActiveRecordDoctor::Tasks::MissingForeignKeys.run
|
18
|
-
end
|
19
|
-
|
20
|
-
task :undefined_table_references => :environment do
|
21
|
-
exit(ActiveRecordDoctor::Tasks::UndefinedTableReferences.run)
|
22
|
-
end
|
23
|
-
|
24
|
-
task :unindexed_soft_delete => :environment do
|
25
|
-
ActiveRecordDoctor::Tasks::UnindexedDeletedAt.run
|
26
|
-
end
|
27
|
-
end
|
@@ -1,27 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
require 'active_record_doctor/tasks/extraneous_indexes'
|
4
|
-
|
5
|
-
class ActiveRecordDoctor::Tasks::ExtraneousIndexesTest < ActiveSupport::TestCase
|
6
|
-
def test_extraneous_indexes_are_reported
|
7
|
-
result = run_task
|
8
|
-
|
9
|
-
assert_equal(
|
10
|
-
[
|
11
|
-
["index_employers_on_id", [:primary_key, "employers"]],
|
12
|
-
["index_users_on_last_name", [:multi_column, "index_users_on_last_name_and_first_name_and_email", "unique_index_on_users_last_name_and_first_name"]],
|
13
|
-
["index_users_on_last_name_and_first_name", [:multi_column, "index_users_on_last_name_and_first_name_and_email", "unique_index_on_users_last_name_and_first_name"]],
|
14
|
-
["index_users_on_email", [:multi_column, "unique_index_on_users_email"]],
|
15
|
-
].sort,
|
16
|
-
result.sort
|
17
|
-
)
|
18
|
-
end
|
19
|
-
|
20
|
-
private
|
21
|
-
|
22
|
-
def run_task
|
23
|
-
printer = SpyPrinter.new
|
24
|
-
ActiveRecordDoctor::Tasks::ExtraneousIndexes.new(printer: printer).run
|
25
|
-
printer.extraneous_indexes
|
26
|
-
end
|
27
|
-
end
|
@@ -1,19 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
require 'active_record_doctor/tasks/missing_foreign_keys'
|
4
|
-
|
5
|
-
class ActiveRecordDoctor::Tasks::MissingForeignKeysTest < ActiveSupport::TestCase
|
6
|
-
def test_missing_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::MissingForeignKeys.new(printer: printer).run
|
17
|
-
printer.missing_foreign_keys
|
18
|
-
end
|
19
|
-
end
|
@@ -1,19 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
require 'active_record_doctor/tasks/undefined_table_references'
|
4
|
-
|
5
|
-
class ActiveRecordDoctor::Tasks::UndefinedTableReferencesTest < ActiveSupport::TestCase
|
6
|
-
def test_undefined_table_references_are_reported
|
7
|
-
result = run_task
|
8
|
-
|
9
|
-
assert_equal([Contract], result)
|
10
|
-
end
|
11
|
-
|
12
|
-
private
|
13
|
-
|
14
|
-
def run_task
|
15
|
-
printer = SpyPrinter.new
|
16
|
-
ActiveRecordDoctor::Tasks::UndefinedTableReferences.new(printer: printer).run
|
17
|
-
printer.undefined_table_references
|
18
|
-
end
|
19
|
-
end
|
@@ -1,19 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
require 'active_record_doctor/tasks/unindexed_deleted_at'
|
4
|
-
|
5
|
-
class ActiveRecordDoctor::Tasks::UnindexedDeletedAtTest < ActiveSupport::TestCase
|
6
|
-
def test_unindexed_deleted_at_are_reported
|
7
|
-
result = run_task
|
8
|
-
|
9
|
-
assert_equal(['index_profiles_on_first_name_and_last_name'], result)
|
10
|
-
end
|
11
|
-
|
12
|
-
private
|
13
|
-
|
14
|
-
def run_task
|
15
|
-
printer = SpyPrinter.new
|
16
|
-
ActiveRecordDoctor::Tasks::UnindexedDeletedAt.new(printer: printer).run
|
17
|
-
printer.unindexed_deleted_at
|
18
|
-
end
|
19
|
-
end
|
@@ -1,19 +0,0 @@
|
|
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
|
data/test/dummy/README.rdoc
DELETED
@@ -1,28 +0,0 @@
|
|
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
DELETED
@@ -1,13 +0,0 @@
|
|
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 .
|
@@ -1,15 +0,0 @@
|
|
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
|
-
*/
|