active_sanity 0.3.0 → 0.4.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 +4 -4
- data/.gitignore +1 -0
- data/Gemfile +2 -0
- data/README.md +10 -4
- data/active_sanity.gemspec +4 -4
- data/commits.csv +0 -0
- data/features/check_sanity.feature +7 -0
- data/features/step_definitions/rails_app.rb +7 -3
- data/lib/active_sanity/checker.rb +8 -1
- data/lib/active_sanity/version.rb +1 -1
- data/lib/generators/active_sanity/active_sanity_generator.rb +11 -1
- data/lib/generators/active_sanity/templates/{create_invalid_records.rb → create_invalid_records.rb.erb} +1 -2
- data/test/rails_template.rb +6 -5
- metadata +11 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5cf8143fcceaa8ee3dd6fe9ceff8f66844b62f47
|
4
|
+
data.tar.gz: e07b90a22a9591f456786e02a816d683b876f70c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1072acfdf533b9ae67c1d6d146978e2c472a356c3d2f4acb6f23d3bc72dadb21196c0675a4f63cae9871f78c05c5c763d3f59d1b9d908e0e7960012817f882cd
|
7
|
+
data.tar.gz: 57c275494f381403304da75d3a3193d5e544481c4e2141d56ff959d5fc23ad0adc2b623f51e36300b062dcbfdbf8566a6a306fed49c0bd40836610560eb804f7
|
data/.gitignore
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
# Active Sanity
|
2
2
|
|
3
|
-
[](http://travis-ci.org/pcreux/active_sanity)
|
4
4
|
|
5
5
|
Perform a sanity check on your database through active record
|
6
6
|
validation.
|
7
7
|
|
8
8
|
## Requirements
|
9
9
|
|
10
|
-
* ActiveSanity ~0.3 requires Rails ~4.0
|
10
|
+
* ActiveSanity ~0.3 and ~0.4 requires Rails ~4.0
|
11
11
|
* ActiveSanity ~0.2 requires Rails ~3.1
|
12
12
|
* ActiveSanity ~0.1 requires Rails ~3.0
|
13
13
|
|
@@ -29,7 +29,7 @@ Just run:
|
|
29
29
|
rake db:check_sanity
|
30
30
|
|
31
31
|
ActiveSanity will iterate over every records of all your models to check
|
32
|
-
|
32
|
+
whether they're valid or not. It will save invalid records in the table
|
33
33
|
invalid_records if it exists and output all invalid records.
|
34
34
|
|
35
35
|
The output might look like the following:
|
@@ -39,7 +39,7 @@ The output might look like the following:
|
|
39
39
|
Flight | 123 | { "arrival_time" => ["can't be nil"], "departure_time" => ["is invalid"] }
|
40
40
|
Flight | 323 | { "arrival_time" => ["can't be nil"] }
|
41
41
|
|
42
|
-
By default, the number of records fetched from the database for validation is set to 500. If this causes any issues in your domain/codebase, you can configure it this way in
|
42
|
+
By default, the number of records fetched from the database for validation is set to 500. If this causes any issues in your domain/codebase, you can configure it this way in `config\application.rb` (or `config\environments\test.rb`):
|
43
43
|
|
44
44
|
class Application < Rails::Application
|
45
45
|
config.after_initialize do
|
@@ -47,6 +47,12 @@ By default, the number of records fetched from the database for validation is se
|
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
50
|
+
If you want to ignore certain models from being verified, you can create a file named `active_sanity.ignore.yml` at the root of your project with the following structure
|
51
|
+
|
52
|
+
models:
|
53
|
+
- '<name of class to ignore>'
|
54
|
+
- '<name of class to ignore>'
|
55
|
+
|
50
56
|
|
51
57
|
## Contribute & Dev environment
|
52
58
|
|
data/active_sanity.gemspec
CHANGED
@@ -6,13 +6,13 @@ Gem::Specification.new do |s|
|
|
6
6
|
s.name = 'active_sanity'
|
7
7
|
s.version = ActiveSanity::VERSION
|
8
8
|
s.platform = Gem::Platform::RUBY
|
9
|
-
s.authors = ['
|
10
|
-
s.email = ['
|
9
|
+
s.authors = ['Philippe Creux']
|
10
|
+
s.email = ['pcreux@gmail.com']
|
11
11
|
s.summary = 'Checks Sanity of Active Record records'
|
12
12
|
s.description = 'Performs a Sanity Check of your database by logging all invalid Active Records'
|
13
|
-
s.homepage = 'https://github.com/
|
13
|
+
s.homepage = 'https://github.com/pcreux/active_sanity'
|
14
14
|
|
15
|
-
s.add_dependency 'rails', '
|
15
|
+
s.add_dependency 'rails', '< 5.0'
|
16
16
|
|
17
17
|
s.required_ruby_version = '>= 1.9.3'
|
18
18
|
|
data/commits.csv
ADDED
File without changes
|
@@ -24,3 +24,10 @@ Feature: Check sanity
|
|
24
24
|
Then I should see the following invalid records:
|
25
25
|
| User | 1 | {:username=>["can't be blank", "is too short (minimum is 3 characters)"]} |
|
26
26
|
| Post | 1 | {:category=>["can't be blank"]} |
|
27
|
+
|
28
|
+
Scenario: Check sanity on database with invalid records with ignored classes
|
29
|
+
Given the database contains a few valid records
|
30
|
+
And the first author's username is empty and the first post category_id is nil
|
31
|
+
And the User class is ignored
|
32
|
+
When I run "rake db:check_sanity"
|
33
|
+
Then I should see "Checking the following models: Category, Post"
|
@@ -1,7 +1,7 @@
|
|
1
1
|
def setup_rails_app
|
2
2
|
return if File.directory?('test/rails_app')
|
3
3
|
|
4
|
-
unless system 'bundle exec rails new test/rails_app -m test/rails_template.rb && cd ./test/rails_app && RAILS_ENV=test rake db:migrate'
|
4
|
+
unless system 'bundle exec rails new test/rails_app -m test/rails_template.rb && cd ./test/rails_app && RAILS_ENV=test bundle exec rake db:migrate'
|
5
5
|
system('rm -fr test/rails_app')
|
6
6
|
fail 'Failed to generate test/rails_app'
|
7
7
|
end
|
@@ -23,7 +23,7 @@ end
|
|
23
23
|
Given /^I have a rails app using 'active_sanity' with db storage$/ do
|
24
24
|
setup_rails_app
|
25
25
|
|
26
|
-
fail unless system('cd ./test/rails_app && rails generate active_sanity && RAILS_ENV=test rake db:migrate')
|
26
|
+
fail unless system('cd ./test/rails_app && bundle exec rails generate active_sanity && RAILS_ENV=test bundle exec rake db:migrate')
|
27
27
|
|
28
28
|
require './test/rails_app/config/environment'
|
29
29
|
|
@@ -46,6 +46,10 @@ Given /^the first author's username is empty and the first post category_id is n
|
|
46
46
|
Post.first.update_attribute(:category_id, nil)
|
47
47
|
end
|
48
48
|
|
49
|
+
Given /^the User class is ignored$/ do
|
50
|
+
system("echo 'models: User' > active_sanity.ignore.yml")
|
51
|
+
end
|
52
|
+
|
49
53
|
Given /^the first author's username is "([^"]*)"$/ do |username|
|
50
54
|
Author.first.update_attribute('username', username)
|
51
55
|
end
|
@@ -59,7 +63,7 @@ Given /^the first post title is empty$/ do
|
|
59
63
|
end
|
60
64
|
|
61
65
|
When /^I run "([^"]*)"$/ do |command|
|
62
|
-
puts @output = `cd ./test/rails_app &&
|
66
|
+
puts @output = `cd ./test/rails_app && RAILS_ENV=test bundle exec #{command} --trace; echo "RETURN:$?"`
|
63
67
|
fail unless @output['RETURN:0']
|
64
68
|
end
|
65
69
|
|
@@ -31,6 +31,12 @@ module ActiveSanity
|
|
31
31
|
|
32
32
|
@models ||= direct_active_record_base_descendants
|
33
33
|
@models -= [InvalidRecord]
|
34
|
+
if File.exist?('active_sanity.ignore.yml')
|
35
|
+
yaml_contents = YAML.load_file('active_sanity.ignore.yml')
|
36
|
+
model_names_to_ignore = Array(yaml_contents['models'] || []).uniq
|
37
|
+
@models = @models.reject { |model| model_names_to_ignore.include?(model.name) }
|
38
|
+
end
|
39
|
+
@models
|
34
40
|
end
|
35
41
|
|
36
42
|
protected
|
@@ -54,7 +60,8 @@ module ActiveSanity
|
|
54
60
|
#
|
55
61
|
# Should return: [Account, User]
|
56
62
|
def direct_active_record_base_descendants
|
57
|
-
ActiveRecord::Base
|
63
|
+
parent_class = defined?(ApplicationRecord) ? ApplicationRecord : ActiveRecord::Base
|
64
|
+
parent_class.descendants.select(&:descends_from_active_record?).sort_by(&:name)
|
58
65
|
end
|
59
66
|
|
60
67
|
# Remove records that are now valid from the list of invalid records.
|
@@ -19,8 +19,18 @@ module ActiveSanity
|
|
19
19
|
end
|
20
20
|
|
21
21
|
def create_migration_file
|
22
|
-
migration_template 'create_invalid_records.rb', 'db/migrate/create_invalid_records.rb'
|
22
|
+
migration_template 'create_invalid_records.rb.erb', 'db/migrate/create_invalid_records.rb'
|
23
23
|
end
|
24
|
+
|
25
|
+
# Used by migration file
|
26
|
+
def rails_version
|
27
|
+
if Rails.version >= "5"
|
28
|
+
"[#{Rails.version.gsub(/\.\d+$/, '')}]" # 5.0
|
29
|
+
else
|
30
|
+
nil
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
24
34
|
end
|
25
35
|
end
|
26
36
|
end
|
@@ -1,11 +1,10 @@
|
|
1
|
-
class CreateInvalidRecords < ActiveRecord::Migration
|
1
|
+
class CreateInvalidRecords < ActiveRecord::Migration<%= rails_version %>
|
2
2
|
def self.up
|
3
3
|
create_table :invalid_records do |t|
|
4
4
|
t.references :record, polymorphic: true, null: false
|
5
5
|
t.text :validation_errors
|
6
6
|
t.timestamps
|
7
7
|
end
|
8
|
-
add_index :invalid_records, [:record_type, :record_id]
|
9
8
|
end
|
10
9
|
|
11
10
|
def self.down
|
data/test/rails_template.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
|
-
|
2
1
|
# Generate some test models
|
3
2
|
|
3
|
+
BASE_MODEL_CLASS = Rails.version >= "5." ? "ApplicationRecord" : "ActiveRecord::Base"
|
4
|
+
|
4
5
|
# Post
|
5
6
|
generate :model, 'post title:string body:text published_at:datetime author_id:integer category_id:integer'
|
6
7
|
post_code = <<-CODE
|
@@ -10,7 +11,7 @@ post_code = <<-CODE
|
|
10
11
|
|
11
12
|
validates :author, :category, :title, :published_at, presence: true
|
12
13
|
CODE
|
13
|
-
inject_into_file 'app/models/post.rb', post_code, after: "class Post <
|
14
|
+
inject_into_file 'app/models/post.rb', post_code, after: "class Post < #{BASE_MODEL_CLASS}\n"
|
14
15
|
|
15
16
|
# Category
|
16
17
|
generate :model, 'category name:string description:text'
|
@@ -19,7 +20,7 @@ category_code = <<-CODE
|
|
19
20
|
|
20
21
|
validates :name, presence: true
|
21
22
|
CODE
|
22
|
-
inject_into_file 'app/models/category.rb', category_code, after: "class Category <
|
23
|
+
inject_into_file 'app/models/category.rb', category_code, after: "class Category < #{BASE_MODEL_CLASS}\n"
|
23
24
|
|
24
25
|
# User
|
25
26
|
generate :model, 'user first_name:string last_name:string username:string type:string'
|
@@ -29,7 +30,7 @@ user_code = <<-CODE
|
|
29
30
|
validates :first_name, :last_name, :username, presence: true
|
30
31
|
validates :username, length: { minimum: 3 }
|
31
32
|
CODE
|
32
|
-
inject_into_file 'app/models/user.rb', user_code, after: "class User <
|
33
|
+
inject_into_file 'app/models/user.rb', user_code, after: "class User < #{BASE_MODEL_CLASS}\n"
|
33
34
|
|
34
35
|
# Author < User
|
35
36
|
create_file 'app/models/author.rb', 'class Author < User; end'
|
@@ -52,4 +53,4 @@ CODE
|
|
52
53
|
inject_into_file 'config/application.rb', custom_batch_size_code, after: " < Rails::Application\n"
|
53
54
|
|
54
55
|
run 'bundle'
|
55
|
-
rake 'db:migrate'
|
56
|
+
rake 'db:drop db:create db:migrate'
|
metadata
CHANGED
@@ -1,30 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_sanity
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
- VersaPay
|
8
7
|
- Philippe Creux
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2017-08-20 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: rails
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
16
|
requirements:
|
18
|
-
- - "
|
17
|
+
- - "<"
|
19
18
|
- !ruby/object:Gem::Version
|
20
|
-
version: '
|
19
|
+
version: '5.0'
|
21
20
|
type: :runtime
|
22
21
|
prerelease: false
|
23
22
|
version_requirements: !ruby/object:Gem::Requirement
|
24
23
|
requirements:
|
25
|
-
- - "
|
24
|
+
- - "<"
|
26
25
|
- !ruby/object:Gem::Version
|
27
|
-
version: '
|
26
|
+
version: '5.0'
|
28
27
|
- !ruby/object:Gem::Dependency
|
29
28
|
name: rspec
|
30
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -70,7 +69,7 @@ dependencies:
|
|
70
69
|
description: Performs a Sanity Check of your database by logging all invalid Active
|
71
70
|
Records
|
72
71
|
email:
|
73
|
-
-
|
72
|
+
- pcreux@gmail.com
|
74
73
|
executables: []
|
75
74
|
extensions: []
|
76
75
|
extra_rdoc_files: []
|
@@ -82,6 +81,7 @@ files:
|
|
82
81
|
- README.md
|
83
82
|
- Rakefile
|
84
83
|
- active_sanity.gemspec
|
84
|
+
- commits.csv
|
85
85
|
- features/check_sanity.feature
|
86
86
|
- features/check_sanity_with_db_storage.feature
|
87
87
|
- features/step_definitions/rails_app.rb
|
@@ -93,9 +93,9 @@ files:
|
|
93
93
|
- lib/active_sanity/tasks.rb
|
94
94
|
- lib/active_sanity/version.rb
|
95
95
|
- lib/generators/active_sanity/active_sanity_generator.rb
|
96
|
-
- lib/generators/active_sanity/templates/create_invalid_records.rb
|
96
|
+
- lib/generators/active_sanity/templates/create_invalid_records.rb.erb
|
97
97
|
- test/rails_template.rb
|
98
|
-
homepage: https://github.com/
|
98
|
+
homepage: https://github.com/pcreux/active_sanity
|
99
99
|
licenses: []
|
100
100
|
metadata: {}
|
101
101
|
post_install_message:
|
@@ -114,7 +114,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
114
114
|
version: '0'
|
115
115
|
requirements: []
|
116
116
|
rubyforge_project:
|
117
|
-
rubygems_version: 2.
|
117
|
+
rubygems_version: 2.4.5.1
|
118
118
|
signing_key:
|
119
119
|
specification_version: 4
|
120
120
|
summary: Checks Sanity of Active Record records
|