traceindex 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e1976ad785be5c111087d12387e9a84df779ec397bc278e3824c9fdf66343140
4
- data.tar.gz: 9585e24b1aff78526e791572ae31401ed47dbb243aae0a4044657bc9263a5753
3
+ metadata.gz: 32652626eba05019e1228a39ca642acb95940610c66cd3dd1c316baf0e8e3fcf
4
+ data.tar.gz: 234f0ad667a6af183f33d83b235648471b6708555e2136ff60319a8f742ad0de
5
5
  SHA512:
6
- metadata.gz: b7af461287ee4ab0e7c95ef037b618082603a539ba55057b60f0728bea43635fe6c40388553c7c04796d138caecca37d9fa0eac15b5e550cb2faf236d864bba1
7
- data.tar.gz: 9f51f3ae65db56ef36c5cc2b1d616fa48c9e5cc7e3a71af1ca06dcceb5ab58837cd2cabcd87278cecbd525a6da6f8b0c01035610b178cad39b0ed9b48c44140f
6
+ metadata.gz: db4abce12669dbd750471345fef3c4a1d8b0e27b0403f41e06cbd2b893158df290eee2010087f0056618c361fc44689e407a1893e1263328e14be077a7de1fdb
7
+ data.tar.gz: 3f731c2f5aaac5ead38371cb22dde681cb473325840858319cb3dad955f4f3dd25312339fbd4dc929caf99f7f8ca56f2fce17bf7613bd5eeb6ffad07ac188b24
@@ -0,0 +1,53 @@
1
+ version: 2.1
2
+
3
+ executors:
4
+ default:
5
+ working_directory: ~/app
6
+ docker:
7
+ - image: circleci/ruby:2.6
8
+ environment:
9
+ DB_USER: 'root'
10
+ DB_PASS: 'root'
11
+ DB_HOST: '127.0.0.1'
12
+ - image: circleci/mysql:8-ram
13
+ environment:
14
+ MYSQL_USER: root
15
+ MYSQL_ROOT_PASSWORD: root
16
+ MYSQL_DATABASE: traceindex_test
17
+ command: [--default-authentication-plugin=mysql_native_password]
18
+
19
+ commands:
20
+ setup_bundle:
21
+ steps:
22
+ - restore_cache:
23
+ key: bundle-{{ checksum "traceindex.gemspec" }}
24
+ - run:
25
+ name: install dependencies
26
+ command: |
27
+ bundle install --jobs=4 --retry=3 --path vendor/bundle
28
+ - save_cache:
29
+ key: bundle-{{ checksum "traceindex.gemspec" }}
30
+ paths:
31
+ - vendor/bundle
32
+
33
+ wait_for_db:
34
+ steps:
35
+ - run:
36
+ name: Wait for DB
37
+ command: dockerize -wait tcp://127.0.0.1:3306 -timeout 1m
38
+
39
+ jobs:
40
+ test:
41
+ executor: default
42
+ steps:
43
+ - checkout
44
+ - setup_bundle
45
+ - wait_for_db
46
+ - run: bundle exec rspec ./spec
47
+
48
+ workflows:
49
+ version: 2
50
+
51
+ test:
52
+ jobs:
53
+ - test
data/.gitignore CHANGED
@@ -3,4 +3,3 @@
3
3
  Gemfile.lock
4
4
  pkg/*
5
5
  *.log
6
- spec/database.yml
data/README.md CHANGED
@@ -29,6 +29,12 @@ If you want the rake task to fail when errors are found.
29
29
  % FAIL_ON_ERROR=1 rake traceindex
30
30
  ```
31
31
 
32
+ If you want the rake task to ignore foreign_keys.
33
+
34
+ ```
35
+ % IGNORE_FOREIGN_KEY=1 rake traceindex
36
+ ```
37
+
32
38
  ## How do I tell it to ignore columns?
33
39
 
34
40
  Create a .traceindex.yaml or .traceindex.yml file in your root directory.
@@ -41,6 +47,8 @@ ignore_models:
41
47
  - ActiveStorage::Attachment
42
48
  - ActionText::RichText
43
49
  - ActionMailbox::InboundEmail
50
+ ignore_foreign_keys:
51
+ - users.created_user_id
44
52
  ```
45
53
 
46
54
  ## Copyright
@@ -3,10 +3,21 @@ task traceindex: :environment do
3
3
  traceindex = Traceindex.new(Rails.application)
4
4
  column_names = traceindex.missing_index_column_names
5
5
 
6
- if column_names.size > 0
6
+ unless column_names.empty?
7
7
  puts "Missing index columns (#{column_names.size}):"
8
8
  column_names.each { |column| puts " #{column}" }
9
+ end
10
+
11
+ if ENV['IGNORE_FOREIGN_KEY'].nil?
12
+ fk_column_names = traceindex.missing_foreign_keys
13
+
14
+ unless fk_column_names.empty?
15
+ puts "Missing foreign keys (#{fk_column_names.size}):"
16
+ fk_column_names.each { |column| puts " #{column}" }
17
+ end
18
+ end
9
19
 
10
- raise 'Missing indexes detected.' if ENV['FAIL_ON_ERROR']
20
+ if ENV['FAIL_ON_ERROR'] && (!column_names.empty? || !fk_column_names.empty?)
21
+ raise 'Missing indexes detected.'
11
22
  end
12
23
  end
@@ -8,9 +8,10 @@ class Traceindex
8
8
  end
9
9
 
10
10
  def initialize(app)
11
- @app = app
12
- @ignore_models = []
13
- @ignore_columns = []
11
+ @app = app
12
+ @ignore_models = []
13
+ @ignore_columns = []
14
+ @ignore_foreign_keys = []
14
15
 
15
16
  (config["ignore_models"] || []).each do |ignored_model|
16
17
  @ignore_models << ignored_model
@@ -19,6 +20,10 @@ class Traceindex
19
20
  (config["ignore_columns"] || []).each do |ignored_column|
20
21
  @ignore_columns << ignored_column
21
22
  end
23
+
24
+ (config["ignore_foreign_keys"] || []).each do |ignored_column|
25
+ @ignore_foreign_keys << ignored_column
26
+ end
22
27
  end
23
28
 
24
29
  def missing_index_column_names
@@ -41,6 +46,25 @@ class Traceindex
41
46
  end
42
47
  end
43
48
 
49
+ def missing_foreign_keys
50
+ models.each.with_object([]) do |model, missing_columns|
51
+ id_columns = model.columns.select {|column| column.name.end_with?("_id") }
52
+
53
+ foreign_keys = ActiveRecord::Base.connection.foreign_keys(model.table_name)
54
+
55
+ id_columns.each do |id_column|
56
+ if @ignore_foreign_keys.include?("#{model.table_name}.#{id_column.name}")
57
+ next
58
+ end
59
+
60
+ next unless foreign_keys.none? { |index| index.column == id_column.name }
61
+ missing_columns << "#{model.table_name}.#{id_column.name}"
62
+ end
63
+ rescue => e
64
+ puts e.message
65
+ end
66
+ end
67
+
44
68
  private
45
69
 
46
70
  def config_filename
@@ -12,13 +12,13 @@ module DummyApp
12
12
  end
13
13
 
14
14
  ActiveRecord::Base.establish_connection(
15
- YAML.load(File.read('spec/database.yml'))['test']
15
+ YAML.load(ERB.new(File.read('spec/database.yml')).result)['test']
16
16
  )
17
17
 
18
18
  ActiveRecord::Schema.define version: 0 do
19
19
  create_table :users, force: true do |t|
20
- t.integer :created_user_id, index: true
21
- t.integer :updated_user_id, index: false
20
+ t.references :created_user, foreign_key: { to_table: :users }
21
+ t.integer :updated_user_id, index: false
22
22
  end
23
23
  end
24
24
 
@@ -0,0 +1,18 @@
1
+ default: &default
2
+ adapter: mysql2
3
+ encoding: utf8mb4
4
+ charset: utf8mb4
5
+ collation: utf8mb4_general_ci
6
+ pool: 5
7
+ username: <%= ENV.fetch("DB_USER") { 'root' } %>
8
+ password: <%= ENV.fetch("DB_PASS") { '' } %>
9
+ host: <%= ENV.fetch("DB_HOST") { '127.0.0.1' } %>
10
+ socket: /tmp/mysql.sock
11
+
12
+ development:
13
+ <<: *default
14
+ database: traceindex_development
15
+
16
+ test:
17
+ <<: *default
18
+ database: traceindex_test
@@ -7,30 +7,97 @@ describe Traceindex do
7
7
  expect(Traceindex::VERSION).not_to be nil
8
8
  end
9
9
 
10
+ let(:traceindex) { Traceindex.new(Rails.application) }
11
+
12
+ after { File.delete '.traceindex.yml' if File.exists? '.traceindex.yml' }
13
+
10
14
  describe '#missing_index_column_names' do
11
15
  it 'missing index found.' do
12
- traceindex = Traceindex.new(Rails.application)
13
16
  expect(traceindex.missing_index_column_names).to eq(['users.updated_user_id'])
14
17
  end
15
18
 
16
- context 'If you have a configuration file' do
19
+ context 'If you have a configuration file(ignore_columns)' do
17
20
  before do
18
21
  File.open '.traceindex.yml', 'w' do |file|
19
22
  file.puts 'ignore_columns:'
20
23
  file.puts ' - users.updated_user_id'
21
- file.puts 'ignore_models:'
22
- file.puts ' - User'
23
24
  end
24
25
  end
25
26
 
26
- after do
27
- File.delete '.traceindex.yml'
27
+ it 'missing index not found.' do
28
+ expect(traceindex.missing_index_column_names).to eq([])
29
+ end
30
+ end
31
+
32
+ context 'If you have a configuration file(ignore_models)' do
33
+ before do
34
+ File.open '.traceindex.yml', 'w' do |file|
35
+ file.puts 'ignore_models:'
36
+ file.puts ' - User'
37
+ end
28
38
  end
29
39
 
30
40
  it 'missing index not found.' do
31
- traceindex = Traceindex.new(Rails.application)
32
41
  expect(traceindex.missing_index_column_names).to eq([])
33
42
  end
34
43
  end
44
+
45
+ context 'If you have a configuration file(ignore_foreign_keys)' do
46
+ before do
47
+ File.open '.traceindex.yml', 'w' do |file|
48
+ file.puts 'ignore_foreign_keys:'
49
+ file.puts ' - users.updated_user_id'
50
+ end
51
+ end
52
+
53
+ it 'missing index exists.' do
54
+ expect(traceindex.missing_index_column_names).to eq(['users.updated_user_id'])
55
+ end
56
+ end
57
+ end
58
+
59
+ describe '#missing_foreign_keys' do
60
+ it 'missing index found.' do
61
+ expect(traceindex.missing_foreign_keys).to eq(['users.updated_user_id'])
62
+ end
63
+
64
+ context 'If you have a configuration file(ignore_columns)' do
65
+ before do
66
+ File.open '.traceindex.yml', 'w' do |file|
67
+ file.puts 'ignore_columns:'
68
+ file.puts ' - users.updated_user_id'
69
+ end
70
+ end
71
+
72
+ it 'missing foreign keys exists.' do
73
+ expect(traceindex.missing_foreign_keys).to eq(['users.updated_user_id'])
74
+ end
75
+ end
76
+
77
+ context 'If you have a configuration file(ignore_foreign_keys)' do
78
+ before do
79
+ File.open '.traceindex.yml', 'w' do |file|
80
+ file.puts 'ignore_foreign_keys:'
81
+ file.puts ' - users.updated_user_id'
82
+ end
83
+ end
84
+
85
+ it 'missing foreign keys not found.' do
86
+ expect(traceindex.missing_foreign_keys).to eq([])
87
+ end
88
+ end
89
+
90
+ context 'If you have a configuration file(ignore_models)' do
91
+ before do
92
+ File.open '.traceindex.yml', 'w' do |file|
93
+ file.puts 'ignore_models:'
94
+ file.puts ' - User'
95
+ end
96
+ end
97
+
98
+ it 'missing foreign keys not found.' do
99
+ expect(traceindex.missing_foreign_keys).to eq([])
100
+ end
101
+ end
35
102
  end
36
103
  end
@@ -2,7 +2,7 @@ $:.push File.expand_path('lib', __dir__)
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'traceindex'
5
- s.version = '0.0.1'
5
+ s.version = '0.0.2'
6
6
  s.platform = Gem::Platform::RUBY
7
7
  s.authors = ['Akira Kusumoto']
8
8
  s.email = ['akirakusumo10@gmail.com']
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: traceindex
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Akira Kusumoto
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-09-17 00:00:00.000000000 Z
11
+ date: 2020-10-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -74,6 +74,7 @@ executables: []
74
74
  extensions: []
75
75
  extra_rdoc_files: []
76
76
  files:
77
+ - ".circleci/config.yml"
77
78
  - ".gitignore"
78
79
  - Gemfile
79
80
  - MIT-LICENSE
@@ -82,7 +83,7 @@ files:
82
83
  - lib/tasks/traceindex.rake
83
84
  - lib/traceindex.rb
84
85
  - spec/app.rb
85
- - spec/database.yml.sample
86
+ - spec/database.yml
86
87
  - spec/spec_helper.rb
87
88
  - spec/traceroute_spec.rb
88
89
  - traceindex.gemspec
@@ -111,6 +112,6 @@ specification_version: 4
111
112
  summary: A Rake task that helps you find the missing indexes for your Rails app
112
113
  test_files:
113
114
  - spec/app.rb
114
- - spec/database.yml.sample
115
+ - spec/database.yml
115
116
  - spec/spec_helper.rb
116
117
  - spec/traceroute_spec.rb
@@ -1,15 +0,0 @@
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