unique_validation_inspector 0.1.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 +7 -0
- data/.gitignore +53 -0
- data/.travis.yml +3 -0
- data/Gemfile +6 -0
- data/LICENSE +21 -0
- data/README.md +67 -0
- data/Rakefile +8 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/tasks/inspect_unique_validations.rake +23 -0
- data/lib/unique_validation_inspector/version.rb +3 -0
- data/lib/unique_validation_inspector.rb +38 -0
- data/unique_validation_inspector.gemspec +30 -0
- metadata +128 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d8d22a62608548622ce222589c6916de85df638f
|
4
|
+
data.tar.gz: 334955c619c2d49aba3f11abac0d3c23c00e3833
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7c9cb2a3b3985708c5ad6dc1c93e1d5a7e952cd3563b24f20ac696713cc9a749a3085b865b1a0f5bc413ebe2f90024f722969020b47664e6482c566b050f1e49
|
7
|
+
data.tar.gz: e7d7a8e3db130b5c994089327623e6515d641b39f19a4e3754fc045ff7be82610a45f6a6713c71d6797deed07b01ea593dffcef2ebaaaf8477ff6ed84aa177d3
|
data/.gitignore
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/spec/examples.txt
|
9
|
+
/test/tmp/
|
10
|
+
/test/version_tmp/
|
11
|
+
/tmp/
|
12
|
+
|
13
|
+
# Used by dotenv library to load environment variables.
|
14
|
+
# .env
|
15
|
+
|
16
|
+
## Specific to RubyMotion:
|
17
|
+
.dat*
|
18
|
+
.repl_history
|
19
|
+
build/
|
20
|
+
*.bridgesupport
|
21
|
+
build-iPhoneOS/
|
22
|
+
build-iPhoneSimulator/
|
23
|
+
|
24
|
+
## Specific to RubyMotion (use of CocoaPods):
|
25
|
+
#
|
26
|
+
# We recommend against adding the Pods directory to your .gitignore. However
|
27
|
+
# you should judge for yourself, the pros and cons are mentioned at:
|
28
|
+
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
|
29
|
+
#
|
30
|
+
# vendor/Pods/
|
31
|
+
|
32
|
+
## Documentation cache and generated files:
|
33
|
+
/.yardoc/
|
34
|
+
/_yardoc/
|
35
|
+
/doc/
|
36
|
+
/rdoc/
|
37
|
+
|
38
|
+
## Environment normalization:
|
39
|
+
/.bundle/
|
40
|
+
/vendor/bundle
|
41
|
+
/lib/bundler/man/
|
42
|
+
|
43
|
+
# for a library or gem, you might want to ignore these files since the code is
|
44
|
+
# intended to run in multiple environments; otherwise, check them in:
|
45
|
+
Gemfile.lock
|
46
|
+
# .ruby-version
|
47
|
+
# .ruby-gemset
|
48
|
+
|
49
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
50
|
+
.rvmrc
|
51
|
+
|
52
|
+
# OS X
|
53
|
+
.DS_Store
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2017 Igor Khomenko
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
# UniqueValidationInspector
|
2
|
+
|
3
|
+

|
4
|
+
|
5
|
+
A Rake task that helps you find unique validations in models that do not have proper DB indexes.
|
6
|
+
|
7
|
+
If uniqueness validation is enabled, Rails will look for existing records before performing **Model.create**, **Model.save**, **Model.update** ... operations. If a record was found the validation fails and the transaction will be rolled back, if not the record will be saved.
|
8
|
+
|
9
|
+
For example, you have **User** model and uniqueness validation for **facebook_id** field. The following SQL query will be performed on validation:
|
10
|
+
|
11
|
+
```sql
|
12
|
+
SELECT 1 AS one FROM `users` WHERE (`users`.`facebook_id ` = 1523123128921623) LIMIT 1
|
13
|
+
```
|
14
|
+
If you do not have DB index for **facebook_id** field then your **Model.create**, **Model.save**, **Model.update** ... operations will be slower and slower with user base grow.
|
15
|
+
|
16
|
+
So this gem is here to notify you about it.
|
17
|
+
|
18
|
+
Read [How I Reduced my DB Server Load by 80%](https://schneems.com/2017/07/18/how-i-reduced-my-db-server-load-by-80/) article to understand what kind of performance issues you may have without proper indexes.
|
19
|
+
|
20
|
+
## Supported versions
|
21
|
+
* Ruby 1.8.7, 1.9.2, 1.9.3, 2.0, 2.1, 2.2, 2.3, 2.4, 2.5 (trunk)
|
22
|
+
|
23
|
+
* Rails 3.0.x, 3.1.x, 3.2.x, 4.0.x, 4.1.x, 5.0.x
|
24
|
+
|
25
|
+
## Installation
|
26
|
+
|
27
|
+
Add this line to your application's Gemfile:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
gem 'unique_validation_inspector'
|
31
|
+
```
|
32
|
+
|
33
|
+
And then execute:
|
34
|
+
|
35
|
+
$ bundle
|
36
|
+
|
37
|
+
Or install it yourself as:
|
38
|
+
|
39
|
+
$ gem install unique_validation_inspector
|
40
|
+
|
41
|
+
## Usage
|
42
|
+
|
43
|
+
Just run the following command in your Rails app directory:
|
44
|
+
|
45
|
+
$ rake inspect_unique_validations
|
46
|
+
|
47
|
+
## Output
|
48
|
+
|
49
|
+
```bash
|
50
|
+
You have the following unique validations:
|
51
|
+
|
52
|
+
Model 'Application':
|
53
|
+
[:title] (scope 'account_id'). Index exists: false
|
54
|
+
|
55
|
+
Model 'User':
|
56
|
+
[:email] (scope 'application_id'). Index exists: true
|
57
|
+
[:login] (scope 'application_id'). Index exists: false
|
58
|
+
[:facebook_id] (scope 'application_id'). Index exists: true
|
59
|
+
[:twitter_id] (scope 'application_id'). Index exists: false
|
60
|
+
[:external_user_id] (scope 'application_id'). Index exists: false
|
61
|
+
[:blob_id] (scope ''). Index exists: true
|
62
|
+
```
|
63
|
+
All things with **Index exists: false** are problematic and you should fix it by adding proper DB indexes.
|
64
|
+
|
65
|
+
## Copyright
|
66
|
+
|
67
|
+
Copyright © 2017 Igor Khomenko. See LICENSE file for further details.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "unique_validation_inspector"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
desc 'Finds unique validations in models that do not have DB indexes.'
|
2
|
+
task :inspect_unique_validations => :environment do
|
3
|
+
inspector = UniqueValidationInspector::Inspector.new Rails.application
|
4
|
+
|
5
|
+
defined_unique_validations = inspector.defined_unique_validations
|
6
|
+
|
7
|
+
puts
|
8
|
+
puts "You have the following unique validations:"
|
9
|
+
|
10
|
+
defined_unique_validations.each do |item|
|
11
|
+
model = item[:model]
|
12
|
+
puts
|
13
|
+
puts "Model '#{model.name}':"
|
14
|
+
item[:validators].each do |validation|
|
15
|
+
scope = validation.options[:scope]
|
16
|
+
attributes = validation.attributes
|
17
|
+
index_exists = inspector.defined_unique_indexes(model.table_name, attributes, scope)
|
18
|
+
puts "#{attributes} (scope '#{scope}'). Index exists: #{index_exists}"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
puts
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require "unique_validation_inspector/version"
|
2
|
+
|
3
|
+
module UniqueValidationInspector
|
4
|
+
class Inspector
|
5
|
+
VERSION = UniqueValidationInspector::VERSION
|
6
|
+
|
7
|
+
class Railtie < ::Rails::Railtie
|
8
|
+
rake_tasks do
|
9
|
+
load File.join(File.dirname(__FILE__), 'tasks/inspect_unique_validations.rake')
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def initialize(app)
|
14
|
+
@app = app
|
15
|
+
end
|
16
|
+
|
17
|
+
def defined_unique_validations
|
18
|
+
ActiveRecord::Base.descendants.reject do |model|
|
19
|
+
validators = model.validators.select {|v| v.is_a?(ActiveRecord::Validations::UniquenessValidator) }
|
20
|
+
validators.empty?
|
21
|
+
end.collect do |model|
|
22
|
+
validators = model.validators.select {|v| v.is_a?(ActiveRecord::Validations::UniquenessValidator) }
|
23
|
+
{:model => model, :validators => validators}
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
def defined_unique_indexes(table_name, fields, scope)
|
29
|
+
#https://dev.mysql.com/doc/refman/5.7/en/multiple-column-indexes.html
|
30
|
+
|
31
|
+
columns = []
|
32
|
+
columns += fields
|
33
|
+
columns.unshift(scope) if scope
|
34
|
+
ActiveRecord::Base.connection.indexes(table_name.to_sym).any? { |i| [lambda { |i| i.columns == columns.map(&:to_s) }].all? { |check| check[i] } }
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "unique_validation_inspector/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "unique_validation_inspector"
|
8
|
+
spec.version = UniqueValidationInspector::VERSION
|
9
|
+
spec.authors = ["Igor Khomenko"]
|
10
|
+
spec.email = ["igor@quickblox.com"]
|
11
|
+
|
12
|
+
spec.summary = "A Rake task that helps you find unique validations in models that do not have DB indexes."
|
13
|
+
spec.description = "A Rake task investigates the application's models definition, then tells you unique validations that do not have DB indexes."
|
14
|
+
spec.homepage = "https://igor.io"
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_dependency "rails", ['>= 3.0.0']
|
25
|
+
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.15"
|
27
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
28
|
+
spec.add_development_dependency "activerecord", ['>= 3.0.0']
|
29
|
+
spec.add_development_dependency "sqlite3", "~> 1.3.13"
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: unique_validation_inspector
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Igor Khomenko
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-08-11 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: 3.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: 3.0.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.15'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.15'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: activerecord
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 3.0.0
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 3.0.0
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: sqlite3
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 1.3.13
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 1.3.13
|
83
|
+
description: A Rake task investigates the application's models definition, then tells
|
84
|
+
you unique validations that do not have DB indexes.
|
85
|
+
email:
|
86
|
+
- igor@quickblox.com
|
87
|
+
executables: []
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- ".gitignore"
|
92
|
+
- ".travis.yml"
|
93
|
+
- Gemfile
|
94
|
+
- LICENSE
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- bin/console
|
98
|
+
- bin/setup
|
99
|
+
- lib/tasks/inspect_unique_validations.rake
|
100
|
+
- lib/unique_validation_inspector.rb
|
101
|
+
- lib/unique_validation_inspector/version.rb
|
102
|
+
- unique_validation_inspector.gemspec
|
103
|
+
homepage: https://igor.io
|
104
|
+
licenses:
|
105
|
+
- MIT
|
106
|
+
metadata: {}
|
107
|
+
post_install_message:
|
108
|
+
rdoc_options: []
|
109
|
+
require_paths:
|
110
|
+
- lib
|
111
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
112
|
+
requirements:
|
113
|
+
- - ">="
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '0'
|
116
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
117
|
+
requirements:
|
118
|
+
- - ">="
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
requirements: []
|
122
|
+
rubyforge_project:
|
123
|
+
rubygems_version: 2.5.2
|
124
|
+
signing_key:
|
125
|
+
specification_version: 4
|
126
|
+
summary: A Rake task that helps you find unique validations in models that do not
|
127
|
+
have DB indexes.
|
128
|
+
test_files: []
|