traceindex 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e1976ad785be5c111087d12387e9a84df779ec397bc278e3824c9fdf66343140
4
+ data.tar.gz: 9585e24b1aff78526e791572ae31401ed47dbb243aae0a4044657bc9263a5753
5
+ SHA512:
6
+ metadata.gz: b7af461287ee4ab0e7c95ef037b618082603a539ba55057b60f0728bea43635fe6c40388553c7c04796d138caecca37d9fa0eac15b5e550cb2faf236d864bba1
7
+ data.tar.gz: 9f51f3ae65db56ef36c5cc2b1d616fa48c9e5cc7e3a71af1ca06dcceb5ab58837cd2cabcd87278cecbd525a6da6f8b0c01035610b178cad39b0ed9b48c44140f
@@ -0,0 +1,6 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ *.log
6
+ spec/database.yml
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in traceroute.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Akira Kusumoto
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,48 @@
1
+ # traceindex
2
+
3
+ A Rake task that helps you find missing indexes in your Rails app.
4
+
5
+ ## Install
6
+
7
+ Put this line in your Gemfile:
8
+ ```
9
+ gem 'traceindex'
10
+ ```
11
+
12
+ Then bundle:
13
+ ```
14
+ % bundle
15
+ ```
16
+
17
+
18
+ ## Usage
19
+
20
+ Just run the following command in your Rails app directory.
21
+
22
+ ```
23
+ % rake traceindex
24
+ ```
25
+
26
+ If you want the rake task to fail when errors are found.
27
+
28
+ ```
29
+ % FAIL_ON_ERROR=1 rake traceindex
30
+ ```
31
+
32
+ ## How do I tell it to ignore columns?
33
+
34
+ Create a .traceindex.yaml or .traceindex.yml file in your root directory.
35
+
36
+ ```yaml
37
+ ignore_columns:
38
+ - users.created_user_id
39
+ ignore_models:
40
+ - ActiveStorage::Blob
41
+ - ActiveStorage::Attachment
42
+ - ActionText::RichText
43
+ - ActionMailbox::InboundEmail
44
+ ```
45
+
46
+ ## Copyright
47
+
48
+ Copyright (c) 2020 Akira Kusumoto. See MIT-LICENSE file for further details.
@@ -0,0 +1,7 @@
1
+ require 'bundler'
2
+ require 'rspec/core/rake_task'
3
+ Bundler::GemHelper.install_tasks
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task default: :spec
@@ -0,0 +1,12 @@
1
+ desc 'Prints out missing indexes'
2
+ task traceindex: :environment do
3
+ traceindex = Traceindex.new(Rails.application)
4
+ column_names = traceindex.missing_index_column_names
5
+
6
+ if column_names.size > 0
7
+ puts "Missing index columns (#{column_names.size}):"
8
+ column_names.each { |column| puts " #{column}" }
9
+
10
+ raise 'Missing indexes detected.' if ENV['FAIL_ON_ERROR']
11
+ end
12
+ end
@@ -0,0 +1,62 @@
1
+ class Traceindex
2
+ VERSION = Gem.loaded_specs['traceindex'].version.to_s
3
+
4
+ class Railtie < ::Rails::Railtie
5
+ rake_tasks do
6
+ load File.join(File.dirname(__FILE__), "tasks/traceindex.rake")
7
+ end
8
+ end
9
+
10
+ def initialize(app)
11
+ @app = app
12
+ @ignore_models = []
13
+ @ignore_columns = []
14
+
15
+ (config["ignore_models"] || []).each do |ignored_model|
16
+ @ignore_models << ignored_model
17
+ end
18
+
19
+ (config["ignore_columns"] || []).each do |ignored_column|
20
+ @ignore_columns << ignored_column
21
+ end
22
+ end
23
+
24
+ def missing_index_column_names
25
+ models.each.with_object([]) do |model, missing_index_columns|
26
+ id_columns = model.columns.select {|column| column.name.end_with?("_id") }
27
+
28
+ indexes = ActiveRecord::Base.connection.indexes(model.table_name)
29
+
30
+ id_columns.each do |id_column|
31
+ if @ignore_columns.include?("#{model.table_name}.#{id_column.name}")
32
+ next
33
+ end
34
+
35
+ next unless indexes.none? {|index| index.columns.first == id_column.name }
36
+
37
+ missing_index_columns << "#{model.table_name}.#{id_column.name}"
38
+ end
39
+ rescue => e
40
+ puts e.message
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def config_filename
47
+ %w[.traceindex.yaml .traceindex.yml].detect {|f| File.exist?(f) }
48
+ end
49
+
50
+ def config
51
+ @config ||= config_filename ? YAML.load_file(config_filename) : {}
52
+ end
53
+
54
+ def models
55
+ return @models if @models
56
+
57
+ @app.eager_load!
58
+ @models ||= ActiveRecord::Base.descendants.reject(&:abstract_class).reject do |model|
59
+ @ignore_models.include?(model.name)
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/all'
4
+ require 'action_controller/railtie'
5
+
6
+ require 'traceindex'
7
+
8
+ module DummyApp
9
+ class Application < Rails::Application
10
+ config.root = File.expand_path(__dir__)
11
+ end
12
+ end
13
+
14
+ ActiveRecord::Base.establish_connection(
15
+ YAML.load(File.read('spec/database.yml'))['test']
16
+ )
17
+
18
+ ActiveRecord::Schema.define version: 0 do
19
+ create_table :users, force: true do |t|
20
+ t.integer :created_user_id, index: true
21
+ t.integer :updated_user_id, index: false
22
+ end
23
+ end
24
+
25
+ class User < ActiveRecord::Base
26
+ end
@@ -0,0 +1,15 @@
1
+ default: &default
2
+ adapter: mysql2
3
+ encoding: utf8
4
+ pool: 5
5
+ username: root
6
+ password:
7
+ socket: /tmp/mysql.sock
8
+
9
+ development:
10
+ <<: *default
11
+ database: traceindex_development
12
+
13
+ test:
14
+ <<: *default
15
+ database: traceindex_test
@@ -0,0 +1,15 @@
1
+ $LOAD_PATH.unshift File.expand_path('../lib', __dir__)
2
+ require 'pry'
3
+ require 'rails'
4
+ require 'traceindex'
5
+ require_relative 'app'
6
+
7
+ RSpec.configure do |config|
8
+ config.around do |example|
9
+ ActiveRecord::Base.transaction do
10
+ example.run
11
+
12
+ raise ActiveRecord::Rollback
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Traceindex do
6
+ it 'has a version number' do
7
+ expect(Traceindex::VERSION).not_to be nil
8
+ end
9
+
10
+ describe '#missing_index_column_names' do
11
+ it 'missing index found.' do
12
+ traceindex = Traceindex.new(Rails.application)
13
+ expect(traceindex.missing_index_column_names).to eq(['users.updated_user_id'])
14
+ end
15
+
16
+ context 'If you have a configuration file' do
17
+ before do
18
+ File.open '.traceindex.yml', 'w' do |file|
19
+ file.puts 'ignore_columns:'
20
+ file.puts ' - users.updated_user_id'
21
+ file.puts 'ignore_models:'
22
+ file.puts ' - User'
23
+ end
24
+ end
25
+
26
+ after do
27
+ File.delete '.traceindex.yml'
28
+ end
29
+
30
+ it 'missing index not found.' do
31
+ traceindex = Traceindex.new(Rails.application)
32
+ expect(traceindex.missing_index_column_names).to eq([])
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,24 @@
1
+ $:.push File.expand_path('lib', __dir__)
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'traceindex'
5
+ s.version = '0.0.1'
6
+ s.platform = Gem::Platform::RUBY
7
+ s.authors = ['Akira Kusumoto']
8
+ s.email = ['akirakusumo10@gmail.com']
9
+ s.homepage = 'https://github.com/bluerabbit/traceindex'
10
+ s.summary = 'A Rake task that helps you find the missing indexes for your Rails app'
11
+ s.description = "This Rake task investigates the application's tables definition, then tells you missing indexes"
12
+
13
+ s.files = `git ls-files`.split("\n")
14
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
15
+ s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
16
+ s.require_paths = ['lib']
17
+
18
+ s.licenses = ['MIT']
19
+
20
+ s.add_dependency 'rails', ['>= 4.0.0']
21
+ s.add_development_dependency 'mysql2'
22
+ s.add_development_dependency 'pry-byebug'
23
+ s.add_development_dependency 'rspec', '~> 3.9'
24
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: traceindex
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Akira Kusumoto
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-09-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 4.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 4.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: mysql2
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry-byebug
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.9'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.9'
69
+ description: This Rake task investigates the application's tables definition, then
70
+ tells you missing indexes
71
+ email:
72
+ - akirakusumo10@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - Gemfile
79
+ - MIT-LICENSE
80
+ - README.md
81
+ - Rakefile
82
+ - lib/tasks/traceindex.rake
83
+ - lib/traceindex.rb
84
+ - spec/app.rb
85
+ - spec/database.yml.sample
86
+ - spec/spec_helper.rb
87
+ - spec/traceroute_spec.rb
88
+ - traceindex.gemspec
89
+ homepage: https://github.com/bluerabbit/traceindex
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubygems_version: 3.0.3
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: A Rake task that helps you find the missing indexes for your Rails app
112
+ test_files:
113
+ - spec/app.rb
114
+ - spec/database.yml.sample
115
+ - spec/spec_helper.rb
116
+ - spec/traceroute_spec.rb