broken_record 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +50 -0
- data/Rakefile +1 -0
- data/broken_record.gemspec +26 -0
- data/lib/broken_record.rb +16 -0
- data/lib/broken_record/config.rb +11 -0
- data/lib/broken_record/logger.rb +58 -0
- data/lib/broken_record/railtie.rb +8 -0
- data/lib/broken_record/scanner.rb +71 -0
- data/lib/broken_record/tasks.rb +9 -0
- data/lib/broken_record/version.rb +3 -0
- metadata +114 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
MjRjNzkyMjcyN2ZmZTc4ODc1NjQyYTFiMzk4OTdhM2FjOWY3M2IwZQ==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
ZWRjNTRmMWE2N2E2YzAyZWVmYzRiMDBjYjYyOGI0ZGY1YmY0YTlhYw==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
MWQzNmIyNTZhYWUwZjg0NDU3ZjVlYjNlZWMzNjA1NjBhMGNhZGFhNmY5NTMw
|
10
|
+
OGNiYmEzNmE0YTczMmIxOWViNmY4YTVlMjM3MzU5ODAxMjQ5N2NlYzBiMjRm
|
11
|
+
MmQ5YjYzZGUzMDk0NmFjOWUyNzlkYmY2Mzk1MmE3N2RhNjE2MzI=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
N2Y0ZDkwYzQ0Y2EwN2I1YWUyYTRhMmM2NWM2MWY1NzNjNDg0NDFmMjNkOGRi
|
14
|
+
ZGVlZmFiOTBiYmRkZjQ2NDZkODk3YWFmOTZiNjgxMDRkYTJhN2M2YTQ3MTNm
|
15
|
+
ZmU5ZjFlNjFhZGJmMmU0ZTI3NDA1NDM3ODEwMjE1ZTk2NTJiMjc=
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Nicholas Gervasi
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
# BrokenRecord
|
2
|
+
|
3
|
+
Provides a rake task for scanning your ActiveRecord models and detecting validation errors.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'broken_record'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install broken_record
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
To scan all records of all models in your project:
|
22
|
+
|
23
|
+
rake broken_record:scan
|
24
|
+
|
25
|
+
If you want to scan all records of a specific model (e.g. the User model)
|
26
|
+
|
27
|
+
rake broken_record:scan[User]
|
28
|
+
|
29
|
+
## Configuration
|
30
|
+
|
31
|
+
BrokenRecord provides a configure method with two options. Here's an example:
|
32
|
+
|
33
|
+
BrokenRecord.configure do |config|
|
34
|
+
# Skip the Foo and Bar models when scanning.
|
35
|
+
config.classes_to_skip = [Foo, Bar]
|
36
|
+
|
37
|
+
# BrokenRecord will call the block provided in before_scan before scanning
|
38
|
+
# your records. This is useful for skipping validations you want to ignore.
|
39
|
+
config.before_scan do
|
40
|
+
User.skip_callback :validate, :before, :user_must_be_active
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
## Contributing
|
45
|
+
|
46
|
+
1. Fork it
|
47
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
48
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
49
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
50
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'broken_record/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "broken_record"
|
8
|
+
spec.version = BrokenRecord::VERSION
|
9
|
+
spec.authors = ["Nicholas Gervasi"]
|
10
|
+
spec.email = ["nick@zenpayroll.com"]
|
11
|
+
spec.description = %q{Detects ActiveRecord models that are not valid.}
|
12
|
+
spec.summary = %q{Provides a rake task for scanning your ActiveRecord models and detecting validation errors.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
|
23
|
+
spec.add_runtime_dependency "rake"
|
24
|
+
spec.add_runtime_dependency "parallel"
|
25
|
+
spec.add_runtime_dependency "colorize"
|
26
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require "broken_record/version"
|
2
|
+
require "broken_record/config"
|
3
|
+
require "broken_record/scanner"
|
4
|
+
require "broken_record/railtie" if defined? Rails::Railtie
|
5
|
+
|
6
|
+
module BrokenRecord
|
7
|
+
extend self
|
8
|
+
|
9
|
+
def configure
|
10
|
+
yield BrokenRecord::Config
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
BrokenRecord.configure do |config|
|
15
|
+
config.classes_to_skip = []
|
16
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'tempfile'
|
2
|
+
|
3
|
+
module BrokenRecord
|
4
|
+
class Logger
|
5
|
+
|
6
|
+
# Static Methods
|
7
|
+
|
8
|
+
def self.report_output(result, lock = nil)
|
9
|
+
lock.flock File::LOCK_EX if lock
|
10
|
+
$stdout.print result[:stdout]
|
11
|
+
$stdout.flush
|
12
|
+
ensure
|
13
|
+
lock.flock File::LOCK_UN if lock
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.report_results(test_results)
|
17
|
+
total_errors = 0
|
18
|
+
test_results.each { |result| total_errors += result[:errors] }
|
19
|
+
if total_errors == 0
|
20
|
+
puts "\nAll models validated successfully.".green
|
21
|
+
else
|
22
|
+
puts "\n#{total_errors} errors were found while running validations.".red
|
23
|
+
exit 1
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.parallel
|
28
|
+
Tempfile.open 'broken_record_lock' do |lock|
|
29
|
+
yield lock
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
# Instance Methods
|
34
|
+
|
35
|
+
def initialize
|
36
|
+
@errors = 0
|
37
|
+
@stdout = ""
|
38
|
+
end
|
39
|
+
|
40
|
+
def log_error(message)
|
41
|
+
@stdout << "[FAIL]\n".red if @errors == 0
|
42
|
+
@stdout << "#{message.red}\n"
|
43
|
+
@errors += 1
|
44
|
+
end
|
45
|
+
|
46
|
+
def log_message(message)
|
47
|
+
@stdout << "#{message}"
|
48
|
+
end
|
49
|
+
|
50
|
+
def log_result
|
51
|
+
@stdout << "[PASS]\n".green if @errors == 0
|
52
|
+
end
|
53
|
+
|
54
|
+
def result
|
55
|
+
{ stdout: @stdout, errors: @errors}
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require "broken_record/logger"
|
2
|
+
|
3
|
+
module BrokenRecord
|
4
|
+
class Scanner
|
5
|
+
def run(model_name = nil)
|
6
|
+
models = models_to_validate(model_name)
|
7
|
+
|
8
|
+
BrokenRecord::Config.before_scan_callbacks.each { |callback| callback.call }
|
9
|
+
|
10
|
+
results = BrokenRecord::Logger.parallel do |lock|
|
11
|
+
Parallel.map(models) do |model|
|
12
|
+
result = validate_model(model)
|
13
|
+
BrokenRecord::Logger.report_output(result, lock)
|
14
|
+
result
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
BrokenRecord::Logger.report_results(results)
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def models_to_validate(model_name)
|
24
|
+
if model_name
|
25
|
+
[ model_name.constantize ]
|
26
|
+
else
|
27
|
+
load_all_active_record_classes
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def load_all_active_record_classes
|
32
|
+
Dir.glob(Rails.root.to_s + '/app/models/**/*.rb').each { |file| require file }
|
33
|
+
objects = Set.new
|
34
|
+
ObjectSpace.each_object(Class) do |klass|
|
35
|
+
if ActiveRecord::Base > klass
|
36
|
+
# Use base_class so we don't try to validate abstract classes and so we don't validate
|
37
|
+
# STI classes multiple times. See active_record/inheritance.rb for more details.
|
38
|
+
objects.add klass.base_class unless BrokenRecord::Config.classes_to_skip.include?(klass)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
objects.sort_by(&:name)
|
43
|
+
end
|
44
|
+
|
45
|
+
def validate_model(model)
|
46
|
+
ActiveRecord::Base.connection.reconnect!
|
47
|
+
|
48
|
+
logger = BrokenRecord::Logger.new
|
49
|
+
logger.log_message "Validating model #{model}... ".ljust(70)
|
50
|
+
|
51
|
+
begin
|
52
|
+
model.unscoped.all.each do |r|
|
53
|
+
begin
|
54
|
+
if !r.valid?
|
55
|
+
message = " Invalid record in #{model} id=#{r.id}."
|
56
|
+
r.errors.each { |attr,msg| message << "\n #{attr} - #{msg}" }
|
57
|
+
logger.log_error message
|
58
|
+
end
|
59
|
+
rescue Exception => msg
|
60
|
+
logger.log_error " Exception for record in #{model} id=#{r.id} - #{msg}."
|
61
|
+
end
|
62
|
+
end
|
63
|
+
rescue Exception => msg
|
64
|
+
logger.log_error " Error querying model #{model} - #{msg}."
|
65
|
+
end
|
66
|
+
|
67
|
+
logger.log_result
|
68
|
+
logger.result
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
metadata
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: broken_record
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nicholas Gervasi
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-01-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
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: parallel
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
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: colorize
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ! '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Detects ActiveRecord models that are not valid.
|
70
|
+
email:
|
71
|
+
- nick@zenpayroll.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- Gemfile
|
78
|
+
- LICENSE.txt
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- broken_record.gemspec
|
82
|
+
- lib/broken_record.rb
|
83
|
+
- lib/broken_record/config.rb
|
84
|
+
- lib/broken_record/logger.rb
|
85
|
+
- lib/broken_record/railtie.rb
|
86
|
+
- lib/broken_record/scanner.rb
|
87
|
+
- lib/broken_record/tasks.rb
|
88
|
+
- lib/broken_record/version.rb
|
89
|
+
homepage: ''
|
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
|
+
rubyforge_project:
|
109
|
+
rubygems_version: 2.1.11
|
110
|
+
signing_key:
|
111
|
+
specification_version: 4
|
112
|
+
summary: Provides a rake task for scanning your ActiveRecord models and detecting
|
113
|
+
validation errors.
|
114
|
+
test_files: []
|