database_consistency 1.5.3 → 1.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/database_consistency/checkers/column_checkers/enum_value_checker.rb +87 -0
- data/lib/database_consistency/checkers/validator_checkers/case_sensitive_unique_validation_checker.rb +34 -0
- data/lib/database_consistency/helper.rb +4 -0
- data/lib/database_consistency/processors/columns_processor.rb +2 -1
- data/lib/database_consistency/processors/validators_processor.rb +2 -1
- data/lib/database_consistency/version.rb +1 -1
- data/lib/database_consistency/writers/simple/enum_values_inconsistent_with_ar_enum.rb +30 -0
- data/lib/database_consistency/writers/simple/enum_values_inconsistent_with_inclusion.rb +30 -0
- data/lib/database_consistency/writers/simple/redundant_case_insensitive_option.rb +22 -0
- data/lib/database_consistency/writers/simple_writer.rb +4 -1
- data/lib/database_consistency.rb +5 -0
- metadata +7 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8049cc73c646f8be58c7f7b3491a0dc02f55f3c75eac32803b850f8c272d11fa
|
4
|
+
data.tar.gz: fdf3df62bb8df2995889a5c717e22fb437260ebfd77d0bd80bdb5d7b9f90494a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e05dcd8d5f1dc062e482a7028f2e10c3232377f1fb7c44609ea41fffb9f14fb3a331db407a693fe5ffe1c0f69d0105e3c39b65166210a6ded5365a1e239ebb50
|
7
|
+
data.tar.gz: 4dba6ecf15c4f3b6ae3553c28873e23ef77b930a4275124f14b9dfae8b84cb8bfee53033fc4f84a573064c1ebd9b702bb32dddf0fa66c8838e373bb0a372f8ee
|
@@ -0,0 +1,87 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DatabaseConsistency
|
4
|
+
module Checkers
|
5
|
+
# This class checks missing presence validator
|
6
|
+
class EnumValueChecker < ColumnChecker
|
7
|
+
Report = ReportBuilder.define(
|
8
|
+
DatabaseConsistency::Report,
|
9
|
+
:enum_values,
|
10
|
+
:declared_values
|
11
|
+
)
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def report_template(status, declared_values, error_slug: nil)
|
16
|
+
Report.new(
|
17
|
+
status: status,
|
18
|
+
error_slug: error_slug,
|
19
|
+
error_message: nil,
|
20
|
+
enum_values: enum_column_values,
|
21
|
+
declared_values: declared_values,
|
22
|
+
**report_attributes
|
23
|
+
)
|
24
|
+
end
|
25
|
+
|
26
|
+
def preconditions
|
27
|
+
Helper.postgresql? && column.type == :enum && (enum || inclusion_validator)
|
28
|
+
end
|
29
|
+
|
30
|
+
def check
|
31
|
+
[
|
32
|
+
(verify_enum if enum),
|
33
|
+
(verify_inclusion_validator if inclusion_validator)
|
34
|
+
].compact
|
35
|
+
end
|
36
|
+
|
37
|
+
def enum_column_values
|
38
|
+
@enum_column_values ||= begin
|
39
|
+
_, values = model.connection.enum_types.find { |(enum, _)| enum == column.sql_type }
|
40
|
+
values.split(',').map(&:strip)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def verify_enum
|
45
|
+
values = enum.values.uniq
|
46
|
+
|
47
|
+
if enum_column_values == values
|
48
|
+
report_template(:ok, values)
|
49
|
+
else
|
50
|
+
report_template(:fail, values, error_slug: :enum_values_inconsistent_with_ar_enum)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def verify_inclusion_validator
|
55
|
+
values = validator_values(inclusion_validator).uniq
|
56
|
+
|
57
|
+
if enum_column_values == values
|
58
|
+
report_template(:ok, values)
|
59
|
+
else
|
60
|
+
report_template(:fail, values, error_slug: :enum_values_inconsistent_with_inclusion)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def validator_values(validator)
|
65
|
+
validator.options[:in] || validator.options[:within]
|
66
|
+
end
|
67
|
+
|
68
|
+
def simple_values_validator?(validator)
|
69
|
+
values = validator_values(validator)
|
70
|
+
|
71
|
+
values.is_a?(Array) && values.all? { |val| val.is_a?(String) }
|
72
|
+
end
|
73
|
+
|
74
|
+
def inclusion_validator
|
75
|
+
@inclusion_validator ||= model.validators.find do |validator|
|
76
|
+
validator.kind == :inclusion &&
|
77
|
+
Helper.check_inclusion?(validator.attributes, column.name) &&
|
78
|
+
simple_values_validator?(validator)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def enum
|
83
|
+
@enum ||= model.defined_enums[column.name]
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DatabaseConsistency
|
4
|
+
module Checkers
|
5
|
+
# This class checks if uniqueness validator has unique index in the database
|
6
|
+
class CaseSensitiveUniqueValidationChecker < ValidatorChecker
|
7
|
+
private
|
8
|
+
|
9
|
+
def preconditions
|
10
|
+
validator.kind == :uniqueness && Helper.postgresql? && citext?
|
11
|
+
end
|
12
|
+
|
13
|
+
def check
|
14
|
+
if validator.options[:case_sensitive].nil? || validator.options[:case_sensitive]
|
15
|
+
report_template(:ok)
|
16
|
+
else
|
17
|
+
report_template(:fail, error_slug: :redundant_case_insensitive_option)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def citext?
|
22
|
+
field_name = Helper.foreign_key_or_attribute(model, attribute)
|
23
|
+
|
24
|
+
field = model.columns.find { |column| column.name.to_s == field_name.to_s }
|
25
|
+
|
26
|
+
field&.type == :citext
|
27
|
+
end
|
28
|
+
|
29
|
+
def sorted_uniqueness_validator_columns
|
30
|
+
@sorted_uniqueness_validator_columns ||= Helper.sorted_uniqueness_validator_columns(attribute, validator, model)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -5,7 +5,8 @@ module DatabaseConsistency
|
|
5
5
|
# The class to process validators
|
6
6
|
class ValidatorsProcessor < BaseProcessor
|
7
7
|
CHECKERS = [
|
8
|
-
Checkers::MissingUniqueIndexChecker
|
8
|
+
Checkers::MissingUniqueIndexChecker,
|
9
|
+
Checkers::CaseSensitiveUniqueValidationChecker
|
9
10
|
].freeze
|
10
11
|
|
11
12
|
private
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DatabaseConsistency
|
4
|
+
module Writers
|
5
|
+
module Simple
|
6
|
+
class EnumValuesInconsistentWithArEnum < Base # :nodoc:
|
7
|
+
private
|
8
|
+
|
9
|
+
def template
|
10
|
+
'enum has [%<enum_values>s] values but ActiveRecord enum has [%<declared_values>s] values'
|
11
|
+
end
|
12
|
+
|
13
|
+
def attributes
|
14
|
+
{
|
15
|
+
enum_values: report.enum_values.join(', '),
|
16
|
+
declared_values: report.declared_values.join(', ')
|
17
|
+
}
|
18
|
+
end
|
19
|
+
|
20
|
+
def unique_attributes
|
21
|
+
{
|
22
|
+
table_or_model_name: report.table_or_model_name,
|
23
|
+
column_or_attribute_name: report.column_or_attribute_name,
|
24
|
+
ar_enum: true
|
25
|
+
}
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DatabaseConsistency
|
4
|
+
module Writers
|
5
|
+
module Simple
|
6
|
+
class EnumValuesInconsistentWithInclusion < Base # :nodoc:
|
7
|
+
private
|
8
|
+
|
9
|
+
def template
|
10
|
+
'enum has [%<enum_values>s] values but ActiveRecord inclusion validation has [%<declared_values>s] values'
|
11
|
+
end
|
12
|
+
|
13
|
+
def attributes
|
14
|
+
{
|
15
|
+
enum_values: report.enum_values.join(', '),
|
16
|
+
declared_values: report.declared_values.join(', ')
|
17
|
+
}
|
18
|
+
end
|
19
|
+
|
20
|
+
def unique_attributes
|
21
|
+
{
|
22
|
+
table_or_model_name: report.table_or_model_name,
|
23
|
+
column_or_attribute_name: report.column_or_attribute_name,
|
24
|
+
inclusion: true
|
25
|
+
}
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DatabaseConsistency
|
4
|
+
module Writers
|
5
|
+
module Simple
|
6
|
+
class RedundantCaseInsensitiveOption < Base # :nodoc:
|
7
|
+
private
|
8
|
+
|
9
|
+
def template
|
10
|
+
"has case insensitive type and doesn't require case_sensitive: false option"
|
11
|
+
end
|
12
|
+
|
13
|
+
def unique_attributes
|
14
|
+
{
|
15
|
+
table_or_model_name: report.table_or_model_name,
|
16
|
+
column_or_attribute_name: report.column_or_attribute_name
|
17
|
+
}
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -25,7 +25,10 @@ module DatabaseConsistency
|
|
25
25
|
redundant_unique_index: Simple::RedundantUniqueIndex,
|
26
26
|
small_primary_key: Simple::SmallPrimaryKey,
|
27
27
|
inconsistent_enum_type: Simple::InconsistentEnumType,
|
28
|
-
missing_foreign_key_cascade: Simple::MissingForeignKeyCascade
|
28
|
+
missing_foreign_key_cascade: Simple::MissingForeignKeyCascade,
|
29
|
+
enum_values_inconsistent_with_ar_enum: Simple::EnumValuesInconsistentWithArEnum,
|
30
|
+
enum_values_inconsistent_with_inclusion: Simple::EnumValuesInconsistentWithInclusion,
|
31
|
+
redundant_case_insensitive_option: Simple::RedundantCaseInsensitiveOption
|
29
32
|
}.freeze
|
30
33
|
|
31
34
|
def write
|
data/lib/database_consistency.rb
CHANGED
@@ -35,6 +35,9 @@ require 'database_consistency/writers/simple/null_constraint_missing'
|
|
35
35
|
require 'database_consistency/writers/simple/possible_null'
|
36
36
|
require 'database_consistency/writers/simple/small_primary_key'
|
37
37
|
require 'database_consistency/writers/simple/inconsistent_enum_type'
|
38
|
+
require 'database_consistency/writers/simple/enum_values_inconsistent_with_ar_enum'
|
39
|
+
require 'database_consistency/writers/simple/enum_values_inconsistent_with_inclusion'
|
40
|
+
require 'database_consistency/writers/simple/redundant_case_insensitive_option'
|
38
41
|
require 'database_consistency/writers/simple_writer'
|
39
42
|
|
40
43
|
require 'database_consistency/writers/autofix/helpers/migration'
|
@@ -67,9 +70,11 @@ require 'database_consistency/checkers/column_checkers/column_checker'
|
|
67
70
|
require 'database_consistency/checkers/column_checkers/null_constraint_checker'
|
68
71
|
require 'database_consistency/checkers/column_checkers/length_constraint_checker'
|
69
72
|
require 'database_consistency/checkers/column_checkers/primary_key_type_checker'
|
73
|
+
require 'database_consistency/checkers/column_checkers/enum_value_checker'
|
70
74
|
|
71
75
|
require 'database_consistency/checkers/validator_checkers/validator_checker'
|
72
76
|
require 'database_consistency/checkers/validator_checkers/missing_unique_index_checker'
|
77
|
+
require 'database_consistency/checkers/validator_checkers/case_sensitive_unique_validation_checker'
|
73
78
|
|
74
79
|
require 'database_consistency/checkers/validators_fraction_checkers/validators_fraction_checker'
|
75
80
|
require 'database_consistency/checkers/validators_fraction_checkers/column_presence_checker'
|
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.
|
4
|
+
version: 1.7.0
|
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-12-
|
11
|
+
date: 2022-12-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -153,6 +153,7 @@ files:
|
|
153
153
|
- lib/database_consistency/checkers/association_checkers/missing_index_checker.rb
|
154
154
|
- lib/database_consistency/checkers/base_checker.rb
|
155
155
|
- lib/database_consistency/checkers/column_checkers/column_checker.rb
|
156
|
+
- lib/database_consistency/checkers/column_checkers/enum_value_checker.rb
|
156
157
|
- lib/database_consistency/checkers/column_checkers/length_constraint_checker.rb
|
157
158
|
- lib/database_consistency/checkers/column_checkers/null_constraint_checker.rb
|
158
159
|
- lib/database_consistency/checkers/column_checkers/primary_key_type_checker.rb
|
@@ -162,6 +163,7 @@ files:
|
|
162
163
|
- lib/database_consistency/checkers/index_checkers/redundant_index_checker.rb
|
163
164
|
- lib/database_consistency/checkers/index_checkers/redundant_unique_index_checker.rb
|
164
165
|
- lib/database_consistency/checkers/index_checkers/unique_index_checker.rb
|
166
|
+
- lib/database_consistency/checkers/validator_checkers/case_sensitive_unique_validation_checker.rb
|
165
167
|
- lib/database_consistency/checkers/validator_checkers/missing_unique_index_checker.rb
|
166
168
|
- lib/database_consistency/checkers/validator_checkers/validator_checker.rb
|
167
169
|
- lib/database_consistency/checkers/validators_fraction_checkers/column_presence_checker.rb
|
@@ -206,6 +208,8 @@ files:
|
|
206
208
|
- lib/database_consistency/writers/simple/association_missing_null_constraint.rb
|
207
209
|
- lib/database_consistency/writers/simple/base.rb
|
208
210
|
- lib/database_consistency/writers/simple/default_message.rb
|
211
|
+
- lib/database_consistency/writers/simple/enum_values_inconsistent_with_ar_enum.rb
|
212
|
+
- lib/database_consistency/writers/simple/enum_values_inconsistent_with_inclusion.rb
|
209
213
|
- lib/database_consistency/writers/simple/has_one_missing_unique_index.rb
|
210
214
|
- lib/database_consistency/writers/simple/inconsistent_enum_type.rb
|
211
215
|
- lib/database_consistency/writers/simple/inconsistent_types.rb
|
@@ -220,6 +224,7 @@ files:
|
|
220
224
|
- lib/database_consistency/writers/simple/null_constraint_misses_validator.rb
|
221
225
|
- lib/database_consistency/writers/simple/null_constraint_missing.rb
|
222
226
|
- lib/database_consistency/writers/simple/possible_null.rb
|
227
|
+
- lib/database_consistency/writers/simple/redundant_case_insensitive_option.rb
|
223
228
|
- lib/database_consistency/writers/simple/redundant_index.rb
|
224
229
|
- lib/database_consistency/writers/simple/redundant_unique_index.rb
|
225
230
|
- lib/database_consistency/writers/simple/small_primary_key.rb
|