validb 1.0.0 → 1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: aecd94c7cb36508bb2fe17f98a8c8ff7e35fbd36
4
- data.tar.gz: 399a316d78060de84cd0534b5c3d4286aa0d512c
3
+ metadata.gz: 5e66a8a7fe86ed39a2a756c8bb8e62e302ee4576
4
+ data.tar.gz: fc47f3fc3335fd08755b95e3c57df02761d94cc9
5
5
  SHA512:
6
- metadata.gz: 7df0136e326c3dc0fa8da5d58e5d59ba0b44c4224db734b6421d8b956affb96a2a8090ad1f5b2f39b9e1ae2b6eb0b49cc9a8b73592f177d4c84d417a2ba4e456
7
- data.tar.gz: 9966e8375e09f154eeb6326eb3c72a84bb08c9a2448d2952af7fc01cc606931980f291601cd6199ecdc05fd94f3e3089af92a5124ea6f9b40b4cf254973bea69
6
+ metadata.gz: ba05c70df13a9f8268bf6005106feb0e65a961c4a9bac37fb3c552e1b40961c0cc1a3c8a42afad6b2c40f34514385c1b685be12f270ab94cbd44fa02d83f5efa
7
+ data.tar.gz: 38ef49e1b9fa51db2646d07779038234125d893504d54220daa107396b6ecbbe3693c658cf4952f8bac6e2ec6d28027380152dbc14542d99137f3c8c895154bb
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ ## 1.1.0, released 2013-03-28
2
+ * CHANGE: Don't check base models if we have decendants
3
+ * Parameter for the batch size
4
+ * Sort models to be validated
5
+ * Fix progress reporting output
6
+ * Show total model record count
7
+ * Show model table name
8
+
1
9
  ## 1.0.0, released 2013-03-27
2
10
  * Add rake task to generate config file
3
11
  * Allow filtering of unwanted models
data/Gemfile CHANGED
@@ -2,10 +2,6 @@ source 'https://rubygems.org/'
2
2
 
3
3
  gemspec
4
4
 
5
- gem "rake"
6
5
  gem "pry"
7
-
8
6
  gem "rspec"
9
-
10
7
  gem "sqlite3"
11
- gem "activerecord"
data/README.md CHANGED
@@ -38,7 +38,8 @@ Check your database for invalid models.
38
38
  ],
39
39
  "ignored_prefixes": [
40
40
  "Api"
41
- ]
41
+ ],
42
+ "batch_size": 100
42
43
  }
43
44
 
44
45
  ## License
@@ -2,17 +2,17 @@ namespace :validb do
2
2
  desc "Check DB for invalid records"
3
3
  task :validate => :environment do
4
4
  models = ENV["models"] || ""
5
- logger = ENV["logger"] || "Validb::ConsoleLogger"
5
+ logger_class = ENV["logger"] || "Validb::ConsoleLogger"
6
6
  filename = ENV["config"] || default_configuration_file
7
7
 
8
8
  # force all models to load so we can find them
9
9
  Rails.application.eager_load!
10
10
 
11
- configuration = Validb::Configuration.new(filename)
12
- finder = Validb::Finder.new(configuration, models)
11
+ params = Validb::Configuration.new(filename).params
12
+ finder = Validb::Finder.new(params, models)
13
13
 
14
- console_logger = logger.constantize.new
15
- checker = Validb::Checker.new(console_logger)
14
+ logger = logger_class.constantize.new
15
+ checker = Validb::Checker.new(params, logger)
16
16
  checker.check(finder.models)
17
17
  end
18
18
 
@@ -31,7 +31,7 @@ namespace :validb do
31
31
  end
32
32
 
33
33
  def default_configuration
34
- { "ignored_models" => [], "ignored_prefixes" => [] }
34
+ { "ignored_models" => [], "ignored_prefixes" => [], "batch_size" => 100 }
35
35
  end
36
36
 
37
37
  def write_configuration_file
@@ -6,7 +6,7 @@ module Validb
6
6
  end
7
7
 
8
8
  def validate(record_batch)
9
- puts "."
9
+ $stdout.print "."
10
10
  record_batch.each do |record|
11
11
  @record_validator.validate(record)
12
12
  end
@@ -1,8 +1,8 @@
1
1
  module Validb
2
2
  class Checker
3
3
 
4
- def initialize(logger)
5
- @model_validator = Validb::ModelValidator.new(logger)
4
+ def initialize(params, logger)
5
+ @model_validator = Validb::ModelValidator.new(params, logger)
6
6
  end
7
7
 
8
8
  def check(models)
@@ -6,6 +6,12 @@ module Validb
6
6
  print_ignored
7
7
  end
8
8
 
9
+ def params
10
+ Validb::Parameters.new(ignored_models, ignored_prefixes, batch_size)
11
+ end
12
+
13
+ private
14
+
9
15
  def ignored_models
10
16
  convert_model_name_strings_to_constants
11
17
  end
@@ -14,7 +20,9 @@ module Validb
14
20
  config["ignored_prefixes"]
15
21
  end
16
22
 
17
- private
23
+ def batch_size
24
+ config["batch_size"]
25
+ end
18
26
 
19
27
  def print_ignored
20
28
  puts "Ignoring prefixes: #{ignored_prefixes.join(',')}" if ignored_prefixes.any?
@@ -28,7 +36,8 @@ module Validb
28
36
  else
29
37
  {
30
38
  "ignored_models" => [],
31
- "ignored_prefixes" => []
39
+ "ignored_prefixes" => [],
40
+ "batch_size" => 100
32
41
  }
33
42
  end
34
43
  end
@@ -2,7 +2,7 @@ module Validb
2
2
  class ConsoleLogger
3
3
 
4
4
  def out(record_hash)
5
- puts "#{record_hash[:model]}:#{record_hash[:id]} - #{record_hash[:error_messages]}"
5
+ $stdout.puts "\n#{record_hash[:model]}:#{record_hash[:id]} - #{record_hash[:error_messages]}"
6
6
  end
7
7
  end
8
8
  end
data/lib/validb/finder.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  module Validb
2
2
  class Finder
3
3
 
4
- def initialize(configuration, model_name_string)
5
- @model_filterer = Validb::ModelFilterer.new(configuration)
4
+ def initialize(params, model_name_string)
5
+ @model_filterer = Validb::ModelFilterer.new(params)
6
6
  @model_name_string = model_name_string || ""
7
7
  end
8
8
 
@@ -1,25 +1,29 @@
1
1
  module Validb
2
2
  class ModelFilterer
3
3
 
4
- def initialize(configuration)
5
- @configuration = configuration
4
+ def initialize(params)
5
+ @params = params
6
6
  end
7
7
 
8
8
  def filter(models)
9
9
  filtered_models = validatable_models(models)
10
- filtered_models = non_inherited_models(filtered_models)
11
10
  filtered_models = non_ignored_models(filtered_models)
12
- non_ignored_prefix_models(filtered_models)
11
+ filtered_models = non_ignored_prefix_models(filtered_models)
12
+ non_inherited_models(filtered_models).sort_by(&:name)
13
13
  end
14
14
 
15
15
  private
16
16
 
17
17
  def ignored_models
18
- @configuration.ignored_models
18
+ @params.ignored_models
19
19
  end
20
20
 
21
21
  def ignored_prefixes
22
- @configuration.ignored_prefixes
22
+ @params.ignored_prefixes
23
+ end
24
+
25
+ def non_inherited_models(models)
26
+ models.reject { |model| (models & model.descendants).any? }
23
27
  end
24
28
 
25
29
  def non_ignored_prefix_models(models)
@@ -32,10 +36,6 @@ module Validb
32
36
  models - ignored_models
33
37
  end
34
38
 
35
- def non_inherited_models(models)
36
- models.reject { |model| model != model.base_class && models.include?(model.base_class)}
37
- end
38
-
39
39
  def validatable_models(models)
40
40
  models.select { |model| model.validators.any? }
41
41
  end
@@ -1,13 +1,14 @@
1
1
  module Validb
2
2
  class ModelValidator
3
3
 
4
- def initialize(logger)
4
+ def initialize(params, logger)
5
+ @params = params
5
6
  @batcher = Validb::Batcher.new(logger)
6
7
  end
7
8
 
8
9
  def validate(model)
9
- puts "Checking #{model}"
10
- model.find_in_batches do |record_batch|
10
+ $stdout.print "\nChecking #{model}(#{model.table_name}) (#{model.count} records)."
11
+ model.find_in_batches(batch_size: @params.batch_size) do |record_batch|
11
12
  @batcher.validate(record_batch)
12
13
  end
13
14
  end
@@ -0,0 +1,3 @@
1
+ module Validb
2
+ Parameters = Struct.new(:ignored_models, :ignored_prefixes, :batch_size)
3
+ end
@@ -1,3 +1,3 @@
1
1
  module Validb
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
data/lib/validb.rb CHANGED
@@ -13,4 +13,5 @@ module Validb
13
13
  autoload :ModelValidator, 'validb/model_validator'
14
14
  autoload :Batcher, 'validb/batcher'
15
15
  autoload :RecordValidator, 'validb/record_validator'
16
+ autoload :Parameters, 'validb/parameters'
16
17
  end
@@ -6,5 +6,6 @@
6
6
  ],
7
7
  "ignored_prefixes": [
8
8
  "Api"
9
- ]
9
+ ],
10
+ "batch_size": 200
10
11
  }
@@ -15,7 +15,7 @@ describe Validb::Batcher do
15
15
  logger = double('logger')
16
16
  batcher = Validb::Batcher.new(logger)
17
17
 
18
- $stdout.should_receive(:puts).with(".")
18
+ $stdout.should_receive(:print).with(".")
19
19
  batcher.validate(record_batch)
20
20
  end
21
21
  end
@@ -4,17 +4,19 @@ describe Validb::Checker do
4
4
  describe "#initialize" do
5
5
  it "creates a checker" do
6
6
  logger = double('logger')
7
- Validb::ModelValidator.should_receive(:new).with(logger)
8
- Validb::Checker.new(logger)
7
+ params = double('params')
8
+ Validb::ModelValidator.should_receive(:new).with(params, logger)
9
+ Validb::Checker.new(params, logger)
9
10
  end
10
11
  end
11
12
 
12
13
  describe "#check" do
13
14
  it "validates the passed in models records" do
14
15
  logger = double('logger')
16
+ params = double('params')
15
17
  model_validator = double('model_validator')
16
- Validb::ModelValidator.should_receive(:new).with(logger).and_return(model_validator)
17
- checker = Validb::Checker.new(logger)
18
+ Validb::ModelValidator.should_receive(:new).with(params, logger).and_return(model_validator)
19
+ checker = Validb::Checker.new(params, logger)
18
20
 
19
21
  model_validator.should_receive(:validate).with(Blog)
20
22
  model_validator.should_receive(:validate).with(Post)
@@ -8,38 +8,24 @@ describe Validb::Configuration do
8
8
  end
9
9
  end
10
10
 
11
- describe "#ignored_models" do
11
+ describe "#params" do
12
12
  context "with a missing configuration file" do
13
- it "returns an empty array" do
13
+ it "creates a params" do
14
14
  filename = File.expand_path(File.dirname(__FILE__) + '/../config/missing.json')
15
- configuration = Validb::Configuration.new(filename)
16
- configuration.ignored_models.should == []
15
+ params = Validb::Configuration.new(filename).params
16
+ params.ignored_models.should == []
17
+ params.ignored_prefixes.should == []
18
+ params.batch_size.should == 100
17
19
  end
18
20
  end
19
21
 
20
22
  context "with a configuration file" do
21
- it "returns the ignored models" do
23
+ it "returns the batch size" do
22
24
  filename = File.expand_path(File.dirname(__FILE__) + '/../config/validb.json')
23
- configuration = Validb::Configuration.new(filename)
24
- configuration.ignored_models.should == [Blog, Post]
25
- end
26
- end
27
- end
28
-
29
- describe "#ignored_prefixes" do
30
- context "with a missing configuration file" do
31
- it "returns an empty array" do
32
- filename = File.expand_path(File.dirname(__FILE__) + '/../config/missing.json')
33
- configuration = Validb::Configuration.new(filename)
34
- configuration.ignored_prefixes.should == []
35
- end
36
- end
37
-
38
- context "with a configuration file" do
39
- it "returns the ignored models" do
40
- filename = File.expand_path(File.dirname(__FILE__) + '/../config/validb.json')
41
- configuration = Validb::Configuration.new(filename)
42
- configuration.ignored_prefixes.should == ["Api"]
25
+ params = Validb::Configuration.new(filename).params
26
+ params.ignored_models.should == [Blog, Post]
27
+ params.ignored_prefixes.should == ["Api"]
28
+ params.batch_size.should == 200
43
29
  end
44
30
  end
45
31
  end
@@ -6,7 +6,7 @@ describe Validb::ConsoleLogger do
6
6
  hash = { model: Blog, id: 1, error_messages: "Error message" }
7
7
 
8
8
  console_logger = Validb::ConsoleLogger.new
9
- $stdout.should_receive(:puts).with("Blog:1 - Error message")
9
+ $stdout.should_receive(:puts).with("\nBlog:1 - Error message")
10
10
  console_logger.out(hash)
11
11
  end
12
12
  end
@@ -6,34 +6,34 @@ describe Validb::Finder do
6
6
 
7
7
  describe "#initialize" do
8
8
  it "creates a model filterer" do
9
- configuration = double('configuration')
10
- Validb::ModelFilterer.should_receive(:new).with(configuration)
11
- Validb::Finder.new(configuration, "")
9
+ params = double('params')
10
+ Validb::ModelFilterer.should_receive(:new).with(params)
11
+ Validb::Finder.new(params, "")
12
12
  end
13
13
  end
14
14
 
15
15
  describe "#models" do
16
16
  context "with an empty string" do
17
17
  it "returns all the models with tables" do
18
- configuration = Validb::Configuration.new("")
19
- finder = Validb::Finder.new(configuration, "")
20
- finder.models.should =~ [Post, Blog, Comment, Person]
18
+ params = Validb::Configuration.new("").params
19
+ finder = Validb::Finder.new(params, "")
20
+ finder.models.should == [Author, Blog, Comment, Post]
21
21
  end
22
22
  end
23
23
 
24
24
  context "valid model names" do
25
25
  it "returns all the selected models with tables" do
26
- configuration = Validb::Configuration.new("")
27
- finder = Validb::Finder.new(configuration, "Post, Blog")
28
- finder.models.should =~ [Post, Blog]
26
+ params = Validb::Configuration.new("").params
27
+ finder = Validb::Finder.new(params, "Post, Blog")
28
+ finder.models.should == [Blog, Post]
29
29
  end
30
30
  end
31
31
 
32
32
  context "with invalid model names" do
33
33
  it "returns all the valid models with tables" do
34
- configuration = Validb::Configuration.new("")
35
- finder = Validb::Finder.new(configuration, "Post, Blog, Fake")
36
- finder.models.should =~ [Post, Blog]
34
+ params = Validb::Configuration.new("").params
35
+ finder = Validb::Finder.new(params, "Post, Blog, Fake")
36
+ finder.models.should == [Blog, Post]
37
37
  end
38
38
  end
39
39
  end
@@ -14,12 +14,12 @@ describe Validb::ModelFilterer do
14
14
  describe "#models" do
15
15
  it "returns all the non filtered models" do
16
16
  filename = File.expand_path(File.dirname(__FILE__) + '/../config/validb.json')
17
- configuration = Validb::Configuration.new(filename)
18
- model_filterer = Validb::ModelFilterer.new(configuration)
17
+ params = Validb::Configuration.new(filename).params
18
+ model_filterer = Validb::ModelFilterer.new(params)
19
19
 
20
20
  models = [User, Author, Blog, Post, Api::Approval, Comment, Person]
21
21
 
22
- model_filterer.filter(models).should =~ [Comment, Person]
22
+ model_filterer.filter(models).should == [Author, Comment]
23
23
  end
24
24
  end
25
25
  end
@@ -4,8 +4,9 @@ describe Validb::ModelValidator do
4
4
  describe "#initialize" do
5
5
  it "creates a model validator" do
6
6
  logger = double('logger')
7
+ params = double('params')
7
8
  Validb::Batcher.should_receive(:new).with(logger)
8
- Validb::ModelValidator.new(logger)
9
+ Validb::ModelValidator.new(params, logger)
9
10
  end
10
11
  end
11
12
 
@@ -13,9 +14,11 @@ describe Validb::ModelValidator do
13
14
  it "validates the records of the model" do
14
15
  model = Blog
15
16
  logger = double('logger')
16
- model_validator = Validb::ModelValidator.new(logger)
17
+ params = double('params', batch_size: 200)
18
+ model_validator = Validb::ModelValidator.new(params, logger)
17
19
 
18
- $stdout.should_receive(:puts).with("Checking Blog")
20
+ $stdout.should_receive(:print).with("\nChecking Blog(blogs) (0 records).")
21
+ model.should_receive(:find_in_batches).with(batch_size: 200)
19
22
  model_validator.validate(model)
20
23
  end
21
24
  end
@@ -2,6 +2,6 @@ require 'spec_helper'
2
2
 
3
3
  describe Validb do
4
4
  it "returns the version" do
5
- Validb::VERSION.should == "1.0.0"
5
+ Validb::VERSION.should == "1.1.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joey Geiger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-03-27 00:00:00.000000000 Z
11
+ date: 2013-03-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -45,6 +45,7 @@ files:
45
45
  - lib/validb/finder.rb
46
46
  - lib/validb/model_filterer.rb
47
47
  - lib/validb/model_validator.rb
48
+ - lib/validb/parameters.rb
48
49
  - lib/validb/railtie.rb
49
50
  - lib/validb/record_validator.rb
50
51
  - lib/validb/version.rb