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.
Files changed (99) hide show
  1. checksums.yaml +5 -5
  2. data/README.md +152 -12
  3. data/lib/active_record_doctor.rb +20 -2
  4. data/lib/active_record_doctor/detectors.rb +13 -0
  5. data/lib/active_record_doctor/detectors/base.rb +64 -0
  6. data/lib/active_record_doctor/{tasks → detectors}/extraneous_indexes.rb +12 -29
  7. data/lib/active_record_doctor/detectors/incorrect_boolean_presence_validation.rb +40 -0
  8. data/lib/active_record_doctor/detectors/incorrect_dependent_option.rb +71 -0
  9. data/lib/active_record_doctor/{tasks → detectors}/missing_foreign_keys.rb +17 -35
  10. data/lib/active_record_doctor/detectors/missing_non_null_constraint.rb +60 -0
  11. data/lib/active_record_doctor/detectors/missing_presence_validation.rb +78 -0
  12. data/lib/active_record_doctor/detectors/missing_unique_indexes.rb +61 -0
  13. data/lib/active_record_doctor/detectors/undefined_table_references.rb +34 -0
  14. data/lib/active_record_doctor/detectors/unindexed_deleted_at.rb +29 -0
  15. data/lib/active_record_doctor/detectors/unindexed_foreign_keys.rb +48 -0
  16. data/lib/active_record_doctor/printers.rb +3 -1
  17. data/lib/active_record_doctor/printers/io_printer.rb +101 -26
  18. data/lib/active_record_doctor/railtie.rb +3 -1
  19. data/lib/active_record_doctor/task.rb +28 -0
  20. data/lib/active_record_doctor/version.rb +3 -1
  21. data/lib/generators/active_record_doctor/add_indexes/add_indexes_generator.rb +15 -11
  22. data/lib/tasks/active_record_doctor.rake +33 -0
  23. data/test/active_record_doctor/detectors/extraneous_indexes_test.rb +67 -0
  24. data/test/active_record_doctor/detectors/incorrect_boolean_presence_validation_test.rb +36 -0
  25. data/test/active_record_doctor/detectors/incorrect_dependent_option_test.rb +117 -0
  26. data/test/active_record_doctor/detectors/missing_foreign_keys_test.rb +24 -0
  27. data/test/active_record_doctor/detectors/missing_non_null_constraint_test.rb +102 -0
  28. data/test/active_record_doctor/detectors/missing_presence_validation_test.rb +107 -0
  29. data/test/active_record_doctor/detectors/missing_unique_indexes_test.rb +114 -0
  30. data/test/active_record_doctor/detectors/undefined_table_references_test.rb +44 -0
  31. data/test/active_record_doctor/detectors/unindexed_deleted_at_test.rb +67 -0
  32. data/test/active_record_doctor/detectors/unindexed_foreign_keys_test.rb +26 -0
  33. data/test/active_record_doctor/printers/io_printer_test.rb +23 -10
  34. data/test/model_factory.rb +78 -0
  35. data/test/setup.rb +126 -0
  36. metadata +93 -149
  37. data/Rakefile +0 -28
  38. data/lib/active_record_doctor/compatibility.rb +0 -11
  39. data/lib/active_record_doctor/tasks.rb +0 -4
  40. data/lib/active_record_doctor/tasks/undefined_table_references.rb +0 -34
  41. data/lib/active_record_doctor/tasks/unindexed_deleted_at.rb +0 -40
  42. data/lib/active_record_doctor/tasks/unindexed_foreign_keys.rb +0 -66
  43. data/lib/tasks/active_record_doctor_tasks.rake +0 -27
  44. data/test/active_record_doctor/tasks/extraneous_indexes_test.rb +0 -27
  45. data/test/active_record_doctor/tasks/missing_foreign_keys_test.rb +0 -19
  46. data/test/active_record_doctor/tasks/undefined_table_references_test.rb +0 -19
  47. data/test/active_record_doctor/tasks/unindexed_deleted_at_test.rb +0 -19
  48. data/test/active_record_doctor/tasks/unindexed_foreign_keys_test.rb +0 -19
  49. data/test/dummy/README.rdoc +0 -28
  50. data/test/dummy/Rakefile +0 -6
  51. data/test/dummy/app/assets/javascripts/application.js +0 -13
  52. data/test/dummy/app/assets/stylesheets/application.css +0 -15
  53. data/test/dummy/app/controllers/application_controller.rb +0 -5
  54. data/test/dummy/app/helpers/application_helper.rb +0 -2
  55. data/test/dummy/app/models/application_record.rb +0 -3
  56. data/test/dummy/app/models/comment.rb +0 -3
  57. data/test/dummy/app/models/contract.rb +0 -3
  58. data/test/dummy/app/models/employer.rb +0 -2
  59. data/test/dummy/app/models/profile.rb +0 -2
  60. data/test/dummy/app/models/user.rb +0 -3
  61. data/test/dummy/app/views/layouts/application.html.erb +0 -14
  62. data/test/dummy/bin/bundle +0 -3
  63. data/test/dummy/bin/rails +0 -4
  64. data/test/dummy/bin/rake +0 -4
  65. data/test/dummy/bin/setup +0 -29
  66. data/test/dummy/config.ru +0 -4
  67. data/test/dummy/config/application.rb +0 -23
  68. data/test/dummy/config/boot.rb +0 -5
  69. data/test/dummy/config/database.yml +0 -19
  70. data/test/dummy/config/database.yml.travis +0 -5
  71. data/test/dummy/config/environment.rb +0 -5
  72. data/test/dummy/config/environments/development.rb +0 -41
  73. data/test/dummy/config/environments/production.rb +0 -79
  74. data/test/dummy/config/environments/test.rb +0 -47
  75. data/test/dummy/config/initializers/assets.rb +0 -11
  76. data/test/dummy/config/initializers/backtrace_silencers.rb +0 -7
  77. data/test/dummy/config/initializers/cookies_serializer.rb +0 -3
  78. data/test/dummy/config/initializers/filter_parameter_logging.rb +0 -4
  79. data/test/dummy/config/initializers/inflections.rb +0 -16
  80. data/test/dummy/config/initializers/mime_types.rb +0 -4
  81. data/test/dummy/config/initializers/session_store.rb +0 -3
  82. data/test/dummy/config/initializers/wrap_parameters.rb +0 -14
  83. data/test/dummy/config/locales/en.yml +0 -23
  84. data/test/dummy/config/routes.rb +0 -56
  85. data/test/dummy/config/secrets.yml +0 -22
  86. data/test/dummy/db/migrate/20160213101213_create_employers.rb +0 -15
  87. data/test/dummy/db/migrate/20160213101221_create_users.rb +0 -23
  88. data/test/dummy/db/migrate/20160213101232_create_profiles.rb +0 -15
  89. data/test/dummy/db/migrate/20160604081452_create_comments.rb +0 -11
  90. data/test/dummy/db/migrate/base_migration.rb +0 -5
  91. data/test/dummy/db/schema.rb +0 -68
  92. data/test/dummy/log/development.log +0 -532
  93. data/test/dummy/log/test.log +0 -2699
  94. data/test/dummy/public/404.html +0 -67
  95. data/test/dummy/public/422.html +0 -67
  96. data/test/dummy/public/500.html +0 -66
  97. data/test/dummy/public/favicon.ico +0 -0
  98. data/test/support/spy_printer.rb +0 -52
  99. 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,11 +0,0 @@
1
- module ActiveRecordDoctor
2
- module Compatibility
3
- def connection_tables
4
- if Rails::VERSION::MAJOR == 5
5
- connection.data_sources
6
- else
7
- connection.tables
8
- end
9
- end
10
- end
11
- end
@@ -1,4 +0,0 @@
1
- module ActiveRecordDoctor
2
- module Tasks
3
- end
4
- end
@@ -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
@@ -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,6 +0,0 @@
1
- # Add your own tasks in files placed in lib/tasks ending in .rake,
2
- # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
-
4
- require File.expand_path('../config/application', __FILE__)
5
-
6
- Rails.application.load_tasks
@@ -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
- */
@@ -1,5 +0,0 @@
1
- class ApplicationController < ActionController::Base
2
- # Prevent CSRF attacks by raising an exception.
3
- # For APIs, you may want to use :null_session instead.
4
- protect_from_forgery with: :exception
5
- end
@@ -1,2 +0,0 @@
1
- module ApplicationHelper
2
- end
@@ -1,3 +0,0 @@
1
- class ApplicationRecord < ActiveRecord::Base
2
- self.table_name = nil
3
- end
@@ -1,3 +0,0 @@
1
- class Comment < ActiveRecord::Base
2
- belongs_to :commentable, polymorphic: true
3
- end
@@ -1,3 +0,0 @@
1
- class Contract < ApplicationRecord
2
- self.table_name = 'contract_records'
3
- end
@@ -1,2 +0,0 @@
1
- class Employer < ActiveRecord::Base
2
- end
@@ -1,2 +0,0 @@
1
- class Profile < ActiveRecord::Base
2
- end
@@ -1,3 +0,0 @@
1
- class User < ActiveRecord::Base
2
- belongs_to :profile
3
- end