database_consistency 0.7.6 → 0.8.1
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 +10 -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/associations_processor.rb +1 -1
- data/lib/database_consistency/processors/columns_processor.rb +3 -2
- data/lib/database_consistency/processors/validators_fractions_processor.rb +1 -1
- data/lib/database_consistency/processors/validators_processor.rb +1 -1
- data/lib/database_consistency/templates/rails_defaults.yml +7 -0
- data/lib/database_consistency/version.rb +1 -1
- metadata +8 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 20658faa1cdd6ae16c25dd7a9b2b85b608f81e2da8ffeb74f8f692ad5c8eb6cb
|
4
|
+
data.tar.gz: 7a6c555f10de5253af9744eea4ada2c95525dd127d081e939ffa6ad6c4923bab
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 95ff5510806f67c8e612c488c608f2685e1abebbb3ecba455e706db34a104ab9c1135cac0193692884a7cb9f61301c91e0a27ea07d61eb08a3b89ca3dd5d952d
|
7
|
+
data.tar.gz: 75a78627604fec92c8fd3703c70fa35e125139e188adf1d8592a480fcef44a50a6b4f670f2eaae139dd07c03ca397745b437d44fcdc35f96ea44b41aaaab6b7d
|
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 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,7 @@ module DatabaseConsistency
|
|
37
38
|
def valid?
|
38
39
|
validator?(ActiveModel::Validations::PresenceValidator) ||
|
39
40
|
validator?(ActiveModel::Validations::InclusionValidator) ||
|
41
|
+
nil_exclusion_validator? ||
|
40
42
|
belongs_to_association?
|
41
43
|
end
|
42
44
|
|
@@ -48,6 +50,13 @@ module DatabaseConsistency
|
|
48
50
|
model.record_timestamps? && %w[created_at updated_at].include?(column.name)
|
49
51
|
end
|
50
52
|
|
53
|
+
def nil_exclusion_validator?
|
54
|
+
model.validators.grep(ActiveModel::Validations::ExclusionValidator).any? do |validator|
|
55
|
+
Helper.check_inclusion?(validator.attributes, column.name) &&
|
56
|
+
validator.options[:in].include?(nil)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
51
60
|
def validator?(validator_class)
|
52
61
|
model.validators.grep(validator_class).any? do |validator|
|
53
62
|
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
|
@@ -12,7 +12,7 @@ module DatabaseConsistency
|
|
12
12
|
|
13
13
|
def check
|
14
14
|
Helper.models.flat_map do |model|
|
15
|
-
next unless configuration.enabled?(model)
|
15
|
+
next unless configuration.enabled?(model.name.to_s)
|
16
16
|
|
17
17
|
Helper.first_level_associations(model).flat_map do |association|
|
18
18
|
enabled_checkers.map do |checker_class|
|
@@ -6,14 +6,15 @@ module DatabaseConsistency
|
|
6
6
|
class ColumnsProcessor < BaseProcessor
|
7
7
|
CHECKERS = [
|
8
8
|
Checkers::NullConstraintChecker,
|
9
|
-
Checkers::LengthConstraintChecker
|
9
|
+
Checkers::LengthConstraintChecker,
|
10
|
+
Checkers::PrimaryKeyTypeChecker
|
10
11
|
].freeze
|
11
12
|
|
12
13
|
private
|
13
14
|
|
14
15
|
def check
|
15
16
|
Helper.parent_models.flat_map do |model|
|
16
|
-
next unless configuration.enabled?(model)
|
17
|
+
next unless configuration.enabled?(model.name.to_s)
|
17
18
|
|
18
19
|
model.columns.flat_map do |column|
|
19
20
|
enabled_checkers.map do |checker_class|
|
@@ -13,7 +13,7 @@ module DatabaseConsistency
|
|
13
13
|
# @return [Array<Hash>]
|
14
14
|
def check
|
15
15
|
Helper.parent_models.flat_map do |model|
|
16
|
-
next unless configuration.enabled?(model)
|
16
|
+
next unless configuration.enabled?(model.name.to_s)
|
17
17
|
|
18
18
|
model._validators.flat_map do |attribute, validators|
|
19
19
|
next unless attribute
|
@@ -14,7 +14,7 @@ module DatabaseConsistency
|
|
14
14
|
# @return [Array<Hash>]
|
15
15
|
def check # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
16
16
|
Helper.parent_models.flat_map do |model|
|
17
|
-
next unless configuration.enabled?(model)
|
17
|
+
next unless configuration.enabled?(model.name.to_s)
|
18
18
|
|
19
19
|
model.validators.flat_map do |validator|
|
20
20
|
next unless validator.respond_to?(:attributes)
|
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.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Evgeniy Demin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-06-03 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
|
@@ -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
|