database_consistency 0.7.7 → 0.8.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/database_consistency +20 -4
- data/lib/database_consistency.rb +5 -0
- data/lib/database_consistency/checkers/column_checkers/null_constraint_checker.rb +18 -1
- data/lib/database_consistency/checkers/column_checkers/primary_key_type_checker.rb +53 -0
- data/lib/database_consistency/checkers/validator_checkers/belongs_to_presence_checker.rb +1 -1
- data/lib/database_consistency/processors/columns_processor.rb +2 -1
- data/lib/database_consistency/templates/rails_defaults.yml +7 -0
- data/lib/database_consistency/version.rb +1 -1
- metadata +13 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 96d59f04ee02179f11d92477feb260aaa444c9b923ecbc142aa2b69ca12b3808
|
4
|
+
data.tar.gz: 6c79bf6d628c470a45976047ffcc608bddf07ff610f802a04542462b36ae4421
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2c563868a70b3dd6cd1fe7d3f62481e6dee156c75a42a22f7e6ee43ce41d5ad08d6742d099c0a145a2b331726282ec2a71234ab1194361af655f18120e9c1b48
|
7
|
+
data.tar.gz: 4ccdf6f7647b4aac78a7378a15d7796f5ed35997f7f59ec77176ebbc55019faf3a0581852e52d529d7c11aa8d8f59a495295445ed39e0a81a2ec0b6a41f638e5
|
data/bin/database_consistency
CHANGED
@@ -1,9 +1,25 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
if ARGV.include?('install')
|
4
|
+
require_relative '../lib/database_consistency/configuration'
|
5
|
+
config_filename = DatabaseConsistency::Configuration::CONFIGURATION_PATH
|
6
|
+
file_exists = File.exists?(config_filename)
|
7
|
+
rules = Pathname.new(__FILE__).dirname.join('..', 'lib', 'database_consistency', 'templates', 'rails_defaults.yml').read
|
8
|
+
if file_exists && File.foreach(config_filename).grep(Regexp.new(rules.lines.first.chomp)).any?
|
9
|
+
puts "#{config_filename} is already present"
|
10
|
+
else
|
11
|
+
File.open(config_filename, 'a') do |file|
|
12
|
+
file << "\n" * 2 if file_exists
|
13
|
+
file << rules
|
14
|
+
end
|
15
|
+
puts "#{config_filename} #{file_exists ? 'updated' : 'added'}"
|
16
|
+
end
|
17
|
+
exit 0
|
18
|
+
else
|
19
|
+
base_dir = File.join(Dir.pwd, ARGV.first.to_s)
|
20
|
+
unless File.realpath(base_dir).start_with?(Dir.pwd)
|
21
|
+
puts "\nWarning! You are going out of current directory, ruby version may be wrong and some gems may be missing.\n"
|
22
|
+
end
|
7
23
|
end
|
8
24
|
|
9
25
|
# Load Rails project
|
data/lib/database_consistency.rb
CHANGED
@@ -11,14 +11,19 @@ require 'database_consistency/writers/base_writer'
|
|
11
11
|
require 'database_consistency/writers/simple_writer'
|
12
12
|
|
13
13
|
require 'database_consistency/checkers/base_checker'
|
14
|
+
|
14
15
|
require 'database_consistency/checkers/association_checkers/association_checker'
|
15
16
|
require 'database_consistency/checkers/association_checkers/missing_index_checker'
|
17
|
+
|
16
18
|
require 'database_consistency/checkers/column_checkers/column_checker'
|
17
19
|
require 'database_consistency/checkers/column_checkers/null_constraint_checker'
|
18
20
|
require 'database_consistency/checkers/column_checkers/length_constraint_checker'
|
21
|
+
require 'database_consistency/checkers/column_checkers/primary_key_type_checker'
|
22
|
+
|
19
23
|
require 'database_consistency/checkers/validator_checkers/validator_checker'
|
20
24
|
require 'database_consistency/checkers/validator_checkers/belongs_to_presence_checker'
|
21
25
|
require 'database_consistency/checkers/validator_checkers/missing_unique_index_checker'
|
26
|
+
|
22
27
|
require 'database_consistency/checkers/validators_fraction_checkers/validators_fraction_checker'
|
23
28
|
require 'database_consistency/checkers/validators_fraction_checkers/column_presence_checker'
|
24
29
|
|
@@ -25,7 +25,8 @@ module DatabaseConsistency
|
|
25
25
|
# | provided | ok |
|
26
26
|
# | missing | fail |
|
27
27
|
#
|
28
|
-
# We consider PresenceValidation, InclusionValidation
|
28
|
+
# We consider PresenceValidation, InclusionValidation, ExclusionValidation, NumericalityValidator with nil,
|
29
|
+
# or BelongsTo association using this column
|
29
30
|
def check
|
30
31
|
if valid?
|
31
32
|
report_template(:ok)
|
@@ -37,6 +38,8 @@ module DatabaseConsistency
|
|
37
38
|
def valid?
|
38
39
|
validator?(ActiveModel::Validations::PresenceValidator) ||
|
39
40
|
validator?(ActiveModel::Validations::InclusionValidator) ||
|
41
|
+
numericality_validator_without_allow_nil? ||
|
42
|
+
nil_exclusion_validator? ||
|
40
43
|
belongs_to_association?
|
41
44
|
end
|
42
45
|
|
@@ -48,6 +51,20 @@ module DatabaseConsistency
|
|
48
51
|
model.record_timestamps? && %w[created_at updated_at].include?(column.name)
|
49
52
|
end
|
50
53
|
|
54
|
+
def nil_exclusion_validator?
|
55
|
+
model.validators.grep(ActiveModel::Validations::ExclusionValidator).any? do |validator|
|
56
|
+
Helper.check_inclusion?(validator.attributes, column.name) &&
|
57
|
+
validator.options[:in].include?(nil)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def numericality_validator_without_allow_nil?
|
62
|
+
model.validators.grep(ActiveModel::Validations::NumericalityValidator).any? do |validator|
|
63
|
+
Helper.check_inclusion?(validator.attributes, column.name) &&
|
64
|
+
!validator.options[:allow_nil]
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
51
68
|
def validator?(validator_class)
|
52
69
|
model.validators.grep(validator_class).any? do |validator|
|
53
70
|
Helper.check_inclusion?(validator.attributes, column.name)
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DatabaseConsistency
|
4
|
+
module Checkers
|
5
|
+
# This class checks missing presence validator
|
6
|
+
class PrimaryKeyTypeChecker < ColumnChecker
|
7
|
+
# Message templates
|
8
|
+
VALIDATOR_MISSING = 'column has int/serial type but recommended to have bigint/bigserial/uuid'
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
VALID_TYPES = %w[bigserial bigint uuid].freeze
|
13
|
+
SQLITE_ADAPTER_NAME = 'SQLite'
|
14
|
+
|
15
|
+
# We skip check when:
|
16
|
+
# - column is not a primary key
|
17
|
+
# - database is SQLite3
|
18
|
+
def preconditions
|
19
|
+
primary_field? && !sqlite?
|
20
|
+
end
|
21
|
+
|
22
|
+
# Table of possible statuses
|
23
|
+
# | bigint/bigserial/uuid | status |
|
24
|
+
# | --------------------- | ------ |
|
25
|
+
# | yes | ok |
|
26
|
+
# | no | fail |
|
27
|
+
def check
|
28
|
+
if valid?
|
29
|
+
report_template(:ok)
|
30
|
+
else
|
31
|
+
report_template(:fail, VALIDATOR_MISSING)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# @return [Boolean]
|
36
|
+
def valid?
|
37
|
+
VALID_TYPES.any? do |type|
|
38
|
+
column.sql_type.to_s.match?(type)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# @return [Boolean]
|
43
|
+
def primary_field?
|
44
|
+
column.name.to_s == model.primary_key.to_s
|
45
|
+
end
|
46
|
+
|
47
|
+
# @return [Boolean]
|
48
|
+
def sqlite?
|
49
|
+
model.connection.adapter_name == SQLITE_ADAPTER_NAME
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -13,7 +13,7 @@ module DatabaseConsistency
|
|
13
13
|
# - there is no belongs_to association with given name
|
14
14
|
# - belongs_to association is polymorphic
|
15
15
|
def preconditions
|
16
|
-
validator.kind == :presence && association && !association.polymorphic?
|
16
|
+
validator.kind == :presence && association && association.belongs_to? && !association.polymorphic?
|
17
17
|
end
|
18
18
|
|
19
19
|
# Table of possible statuses
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: database_consistency
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Evgeniy Demin
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-06-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -70,16 +70,16 @@ dependencies:
|
|
70
70
|
name: rake
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- - "
|
73
|
+
- - ">="
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version:
|
75
|
+
version: 12.3.3
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- - "
|
80
|
+
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version:
|
82
|
+
version: 12.3.3
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: rspec
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
@@ -122,7 +122,7 @@ dependencies:
|
|
122
122
|
- - "~>"
|
123
123
|
- !ruby/object:Gem::Version
|
124
124
|
version: '1.3'
|
125
|
-
description:
|
125
|
+
description:
|
126
126
|
email:
|
127
127
|
- lawliet.djez@gmail.com
|
128
128
|
executables:
|
@@ -138,6 +138,7 @@ files:
|
|
138
138
|
- lib/database_consistency/checkers/column_checkers/column_checker.rb
|
139
139
|
- lib/database_consistency/checkers/column_checkers/length_constraint_checker.rb
|
140
140
|
- lib/database_consistency/checkers/column_checkers/null_constraint_checker.rb
|
141
|
+
- lib/database_consistency/checkers/column_checkers/primary_key_type_checker.rb
|
141
142
|
- lib/database_consistency/checkers/validator_checkers/belongs_to_presence_checker.rb
|
142
143
|
- lib/database_consistency/checkers/validator_checkers/missing_unique_index_checker.rb
|
143
144
|
- lib/database_consistency/checkers/validator_checkers/validator_checker.rb
|
@@ -151,6 +152,7 @@ files:
|
|
151
152
|
- lib/database_consistency/processors/validators_fractions_processor.rb
|
152
153
|
- lib/database_consistency/processors/validators_processor.rb
|
153
154
|
- lib/database_consistency/rescue_error.rb
|
155
|
+
- lib/database_consistency/templates/rails_defaults.yml
|
154
156
|
- lib/database_consistency/version.rb
|
155
157
|
- lib/database_consistency/writers/base_writer.rb
|
156
158
|
- lib/database_consistency/writers/simple_writer.rb
|
@@ -158,7 +160,7 @@ homepage: https://github.com/djezzzl/database_consistency
|
|
158
160
|
licenses:
|
159
161
|
- MIT
|
160
162
|
metadata: {}
|
161
|
-
post_install_message:
|
163
|
+
post_install_message:
|
162
164
|
rdoc_options: []
|
163
165
|
require_paths:
|
164
166
|
- lib
|
@@ -173,9 +175,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
173
175
|
- !ruby/object:Gem::Version
|
174
176
|
version: '0'
|
175
177
|
requirements: []
|
176
|
-
|
177
|
-
|
178
|
-
signing_key:
|
178
|
+
rubygems_version: 3.1.2
|
179
|
+
signing_key:
|
179
180
|
specification_version: 4
|
180
181
|
summary: Provide an easy way to check the consistency of the database constraints
|
181
182
|
with the application validations.
|