database_consistency 1.5.1 → 1.5.3
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/bin/database_consistency +1 -1
- data/lib/database_consistency/checkers/association_checkers/missing_index_checker.rb +1 -1
- data/lib/database_consistency/checkers/validators_fraction_checkers/column_presence_checker.rb +29 -48
- data/lib/database_consistency/processors/associations_processor.rb +1 -1
- data/lib/database_consistency/processors/columns_processor.rb +1 -1
- data/lib/database_consistency/processors/enums_processor.rb +1 -1
- data/lib/database_consistency/processors/indexes_processor.rb +1 -1
- 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/version.rb +1 -1
- data/lib/database_consistency/writers/autofix_writer.rb +1 -0
- data/lib/database_consistency/writers/simple/association_foreign_type_missing_null_constraint.rb +22 -0
- data/lib/database_consistency/writers/simple_writer.rb +1 -0
- data/lib/database_consistency.rb +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f06f5f350f8f4a0dc3eab32d48b01f61da8fde59ebcbc0fcd35296a5a0a2ecd5
|
4
|
+
data.tar.gz: 13d02eb9836ef51570c33b721909517e1abbbb2d7406e5236a718d040e997f0d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: be22b9c36f696268721fdad8fdb8cb5379cb739261d40577754ede248c4cdfd7cf1858f03a6609c762fa500ce87424b3cfc2ed60c93a437bcb2af15dc92f4f8d
|
7
|
+
data.tar.gz: cdfdaa8f8ba2d0e8af3e510407dcfffb36a1568c380a8be115aa87369bf3f97f45d3c2ebb6587f30a0b2048a576193bf90a168e924f3387f6b05e34f1f4d6ced
|
data/bin/database_consistency
CHANGED
@@ -39,7 +39,7 @@ opt_parser = OptionParser.new do |opts|
|
|
39
39
|
options[:todo] = true
|
40
40
|
end
|
41
41
|
|
42
|
-
opts.on('-
|
42
|
+
opts.on('-f', '--autofix', 'Automatically fixes issues by adjusting the code or generating missing migrations.') do
|
43
43
|
options[:autofix] = true
|
44
44
|
end
|
45
45
|
|
data/lib/database_consistency/checkers/validators_fraction_checkers/column_presence_checker.rb
CHANGED
@@ -18,22 +18,17 @@ module DatabaseConsistency
|
|
18
18
|
validator.kind == :presence
|
19
19
|
end
|
20
20
|
|
21
|
-
# We skip the check when there are no presence validators
|
22
21
|
def preconditions
|
23
|
-
|
22
|
+
(regular_column || association) && validators.any?
|
24
23
|
end
|
25
24
|
|
26
|
-
def
|
27
|
-
model._reflect_on_association(attribute)&.macro == :has_one
|
28
|
-
end
|
29
|
-
|
30
|
-
def report_template(status, error_message: nil, error_slug: nil)
|
25
|
+
def report_template(status, column_name:, error_slug: nil)
|
31
26
|
Report.new(
|
32
27
|
status: status,
|
33
28
|
error_slug: error_slug,
|
34
|
-
error_message:
|
29
|
+
error_message: nil,
|
35
30
|
table_name: model.table_name.to_s,
|
36
|
-
column_name:
|
31
|
+
column_name: column_name,
|
37
32
|
**report_attributes
|
38
33
|
)
|
39
34
|
end
|
@@ -42,56 +37,42 @@ module DatabaseConsistency
|
|
42
37
|
validators.all? { |validator| validator.options.slice(*WEAK_OPTIONS).any? }
|
43
38
|
end
|
44
39
|
|
45
|
-
def check
|
46
|
-
|
47
|
-
has_weak_option = weak_option?
|
40
|
+
def check
|
41
|
+
return analyse(attribute.to_s, type: :null_constraint_missing) if regular_column
|
48
42
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
if regular_column
|
53
|
-
Report.new(
|
54
|
-
status: :fail,
|
55
|
-
error_slug: :null_constraint_missing,
|
56
|
-
error_message: nil,
|
57
|
-
table_name: model.table_name.to_s,
|
58
|
-
column_name: attribute.to_s,
|
59
|
-
**report_attributes
|
60
|
-
)
|
61
|
-
else
|
62
|
-
Report.new(
|
63
|
-
status: :fail,
|
64
|
-
error_slug: :association_missing_null_constraint,
|
65
|
-
error_message: nil,
|
66
|
-
table_name: model.table_name.to_s,
|
67
|
-
column_name: association_reflection.foreign_key.to_s,
|
68
|
-
**report_attributes
|
69
|
-
)
|
43
|
+
reports = [analyse(association.foreign_key.to_s, type: :association_missing_null_constraint)]
|
44
|
+
if association.polymorphic?
|
45
|
+
reports << analyse(association.foreign_type.to_s, type: :association_foreign_type_missing_null_constraint)
|
70
46
|
end
|
71
|
-
end
|
72
47
|
|
73
|
-
|
74
|
-
@column ||= (regular_column || association_reference_column)
|
48
|
+
reports
|
75
49
|
end
|
76
50
|
|
77
|
-
def
|
78
|
-
|
79
|
-
end
|
51
|
+
def analyse(column_name, type:)
|
52
|
+
field = column(column_name)
|
80
53
|
|
81
|
-
|
82
|
-
|
83
|
-
|
54
|
+
can_be_null = field.null
|
55
|
+
has_weak_option = weak_option?
|
56
|
+
|
57
|
+
return report_template(:ok, column_name: column_name) if can_be_null == has_weak_option
|
58
|
+
return report_template(:fail, error_slug: :possible_null, column_name: column_name) unless can_be_null
|
84
59
|
|
85
|
-
|
86
|
-
|
60
|
+
report_template(:fail, error_slug: type, column_name: column_name)
|
61
|
+
end
|
87
62
|
|
88
|
-
|
63
|
+
def regular_column
|
64
|
+
@regular_column ||= column(attribute.to_s)
|
89
65
|
end
|
90
66
|
|
91
|
-
def
|
92
|
-
|
67
|
+
def association
|
68
|
+
@association ||=
|
69
|
+
model
|
93
70
|
.reflect_on_all_associations
|
94
|
-
.find { |reflection| reflection.belongs_to? && reflection.name == attribute }
|
71
|
+
.find { |reflection| reflection.belongs_to? && reflection.name.to_s == attribute.to_s }
|
72
|
+
end
|
73
|
+
|
74
|
+
def column(name)
|
75
|
+
model.columns.find { |field| field.name == name.to_s }
|
95
76
|
end
|
96
77
|
end
|
97
78
|
end
|
@@ -18,7 +18,7 @@ module DatabaseConsistency
|
|
18
18
|
next unless configuration.enabled?(model.name.to_s)
|
19
19
|
|
20
20
|
Helper.first_level_associations(model).flat_map do |association|
|
21
|
-
enabled_checkers.
|
21
|
+
enabled_checkers.flat_map do |checker_class|
|
22
22
|
checker = checker_class.new(model, association)
|
23
23
|
checker.report_if_enabled?(configuration)
|
24
24
|
end
|
@@ -17,7 +17,7 @@ module DatabaseConsistency
|
|
17
17
|
next unless configuration.enabled?(model.name.to_s)
|
18
18
|
|
19
19
|
model.columns.flat_map do |column|
|
20
|
-
enabled_checkers.
|
20
|
+
enabled_checkers.flat_map do |checker_class|
|
21
21
|
checker = checker_class.new(model, column)
|
22
22
|
checker.report_if_enabled?(configuration)
|
23
23
|
end
|
@@ -15,7 +15,7 @@ module DatabaseConsistency
|
|
15
15
|
next unless configuration.enabled?(model.name.to_s)
|
16
16
|
|
17
17
|
model.defined_enums.keys.flat_map do |enum|
|
18
|
-
enabled_checkers.
|
18
|
+
enabled_checkers.flat_map do |checker_class|
|
19
19
|
checker = checker_class.new(model, enum)
|
20
20
|
checker.report_if_enabled?(configuration)
|
21
21
|
end
|
@@ -19,7 +19,7 @@ module DatabaseConsistency
|
|
19
19
|
indexes = model.connection.indexes(model.table_name)
|
20
20
|
|
21
21
|
indexes.flat_map do |index|
|
22
|
-
enabled_checkers.
|
22
|
+
enabled_checkers.flat_map do |checker_class|
|
23
23
|
checker = checker_class.new(model, index)
|
24
24
|
checker.report_if_enabled?(configuration)
|
25
25
|
end
|
@@ -18,7 +18,7 @@ module DatabaseConsistency
|
|
18
18
|
model._validators.flat_map do |attribute, validators|
|
19
19
|
next unless attribute
|
20
20
|
|
21
|
-
enabled_checkers.
|
21
|
+
enabled_checkers.flat_map do |checker_class|
|
22
22
|
checker = checker_class.new(model, attribute, validators)
|
23
23
|
checker.report_if_enabled?(configuration)
|
24
24
|
end
|
@@ -19,7 +19,7 @@ module DatabaseConsistency
|
|
19
19
|
next unless validator.respond_to?(:attributes)
|
20
20
|
|
21
21
|
validator.attributes.flat_map do |attribute|
|
22
|
-
enabled_checkers.
|
22
|
+
enabled_checkers.flat_map do |checker_class|
|
23
23
|
checker = checker_class.new(model, attribute, validator)
|
24
24
|
checker.report_if_enabled?(configuration)
|
25
25
|
end
|
@@ -8,6 +8,7 @@ module DatabaseConsistency
|
|
8
8
|
SLUG_TO_GENERATOR = {
|
9
9
|
association_missing_index: Autofix::AssociationMissingIndex,
|
10
10
|
association_missing_null_constraint: Autofix::NullConstraintMissing,
|
11
|
+
association_foreign_type_missing_null_constraint: Autofix::NullConstraintMissing,
|
11
12
|
has_one_missing_unique_index: Autofix::HasOneMissingUniqueIndex,
|
12
13
|
inconsistent_types: Autofix::InconsistentTypes,
|
13
14
|
missing_foreign_key: Autofix::MissingForeignKey,
|
data/lib/database_consistency/writers/simple/association_foreign_type_missing_null_constraint.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DatabaseConsistency
|
4
|
+
module Writers
|
5
|
+
module Simple
|
6
|
+
class AssociationForeignTypeMissingNullConstraint < Base # :nodoc:
|
7
|
+
private
|
8
|
+
|
9
|
+
def template
|
10
|
+
'association foreign type column should be required in the database'
|
11
|
+
end
|
12
|
+
|
13
|
+
def unique_attributes
|
14
|
+
{
|
15
|
+
table_name: report.table_name,
|
16
|
+
column_name: report.column_name
|
17
|
+
}
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -8,6 +8,7 @@ module DatabaseConsistency
|
|
8
8
|
SLUG_TO_WRITER = {
|
9
9
|
association_missing_index: Simple::AssociationMissingIndex,
|
10
10
|
association_missing_null_constraint: Simple::AssociationMissingNullConstraint,
|
11
|
+
association_foreign_type_missing_null_constraint: Simple::AssociationForeignTypeMissingNullConstraint,
|
11
12
|
has_one_missing_unique_index: Simple::HasOneMissingUniqueIndex,
|
12
13
|
inconsistent_types: Simple::InconsistentTypes,
|
13
14
|
length_validator_greater_limit: Simple::LengthValidatorGreaterLimit,
|
data/lib/database_consistency.rb
CHANGED
@@ -22,6 +22,7 @@ require 'database_consistency/writers/simple/missing_foreign_key_cascade'
|
|
22
22
|
require 'database_consistency/writers/simple/redundant_unique_index'
|
23
23
|
require 'database_consistency/writers/simple/association_missing_index'
|
24
24
|
require 'database_consistency/writers/simple/association_missing_null_constraint'
|
25
|
+
require 'database_consistency/writers/simple/association_foreign_type_missing_null_constraint'
|
25
26
|
require 'database_consistency/writers/simple/has_one_missing_unique_index'
|
26
27
|
require 'database_consistency/writers/simple/length_validator_lower_limit'
|
27
28
|
require 'database_consistency/writers/simple/length_validator_greater_limit'
|
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: 1.5.
|
4
|
+
version: 1.5.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Evgeniy Demin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-12-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -201,6 +201,7 @@ files:
|
|
201
201
|
- lib/database_consistency/writers/autofix/templates/redundant_index.tt
|
202
202
|
- lib/database_consistency/writers/autofix_writer.rb
|
203
203
|
- lib/database_consistency/writers/base_writer.rb
|
204
|
+
- lib/database_consistency/writers/simple/association_foreign_type_missing_null_constraint.rb
|
204
205
|
- lib/database_consistency/writers/simple/association_missing_index.rb
|
205
206
|
- lib/database_consistency/writers/simple/association_missing_null_constraint.rb
|
206
207
|
- lib/database_consistency/writers/simple/base.rb
|