database_consistency 1.5.3 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f06f5f350f8f4a0dc3eab32d48b01f61da8fde59ebcbc0fcd35296a5a0a2ecd5
4
- data.tar.gz: 13d02eb9836ef51570c33b721909517e1abbbb2d7406e5236a718d040e997f0d
3
+ metadata.gz: de35bff4be332ceca4a982d5cde674db49db44a53d3fce22e177cc561cfe6253
4
+ data.tar.gz: 9f519c9b7aef026da3033790b47690c513871b725b5271e77afa55f29014480f
5
5
  SHA512:
6
- metadata.gz: be22b9c36f696268721fdad8fdb8cb5379cb739261d40577754ede248c4cdfd7cf1858f03a6609c762fa500ce87424b3cfc2ed60c93a437bcb2af15dc92f4f8d
7
- data.tar.gz: cdfdaa8f8ba2d0e8af3e510407dcfffb36a1568c380a8be115aa87369bf3f97f45d3c2ebb6587f30a0b2048a576193bf90a168e924f3387f6b05e34f1f4d6ced
6
+ metadata.gz: bdbd9750c5a9f8470a0c73fbae78a50193b423367b934f244fe0132e0dc9af29011cf55b8ef67a6dbe22f7fee3b4bbc012069dac2ae329ccad6f43452f83c80d
7
+ data.tar.gz: 133abb119e8a80c25b6c4fe0936273dd328adbd6df7bc290ea16f97e695b6468f99a0b9c10bea0420dbc1788de88c73948ea429981b30812002a7b103b2f9ddf
@@ -0,0 +1,91 @@
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
+ postgresql? && column.type == :enum && (enum || inclusion_validator)
28
+ end
29
+
30
+ def postgresql?
31
+ Helper.adapter == 'postgresql'
32
+ end
33
+
34
+ def check
35
+ [
36
+ (verify_enum if enum),
37
+ (verify_inclusion_validator if inclusion_validator)
38
+ ].compact
39
+ end
40
+
41
+ def enum_column_values
42
+ @enum_column_values ||= begin
43
+ _, values = model.connection.enum_types.find { |(enum, _)| enum == column.sql_type }
44
+ values.split(',').map(&:strip)
45
+ end
46
+ end
47
+
48
+ def verify_enum
49
+ values = enum.values.uniq
50
+
51
+ if enum_column_values == values
52
+ report_template(:ok, values)
53
+ else
54
+ report_template(:fail, values, error_slug: :enum_values_inconsistent_with_ar_enum)
55
+ end
56
+ end
57
+
58
+ def verify_inclusion_validator
59
+ values = validator_values(inclusion_validator).uniq
60
+
61
+ if enum_column_values == values
62
+ report_template(:ok, values)
63
+ else
64
+ report_template(:fail, values, error_slug: :enum_values_inconsistent_with_inclusion)
65
+ end
66
+ end
67
+
68
+ def validator_values(validator)
69
+ validator.options[:in] || validator.options[:within]
70
+ end
71
+
72
+ def simple_values_validator?(validator)
73
+ values = validator_values(validator)
74
+
75
+ values.is_a?(Array) && values.all? { |val| val.is_a?(String) }
76
+ end
77
+
78
+ def inclusion_validator
79
+ @inclusion_validator ||= model.validators.find do |validator|
80
+ validator.kind == :inclusion &&
81
+ Helper.check_inclusion?(validator.attributes, column.name) &&
82
+ simple_values_validator?(validator)
83
+ end
84
+ end
85
+
86
+ def enum
87
+ @enum ||= model.defined_enums[column.name]
88
+ end
89
+ end
90
+ end
91
+ end
@@ -7,7 +7,8 @@ module DatabaseConsistency
7
7
  CHECKERS = [
8
8
  Checkers::NullConstraintChecker,
9
9
  Checkers::LengthConstraintChecker,
10
- Checkers::PrimaryKeyTypeChecker
10
+ Checkers::PrimaryKeyTypeChecker,
11
+ Checkers::EnumValueChecker
11
12
  ].freeze
12
13
 
13
14
  private
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DatabaseConsistency
4
- VERSION = '1.5.3'
4
+ VERSION = '1.6.0'
5
5
  end
@@ -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
@@ -25,7 +25,9 @@ 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
29
31
  }.freeze
30
32
 
31
33
  def write
@@ -35,6 +35,8 @@ 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'
38
40
  require 'database_consistency/writers/simple_writer'
39
41
 
40
42
  require 'database_consistency/writers/autofix/helpers/migration'
@@ -67,6 +69,7 @@ require 'database_consistency/checkers/column_checkers/column_checker'
67
69
  require 'database_consistency/checkers/column_checkers/null_constraint_checker'
68
70
  require 'database_consistency/checkers/column_checkers/length_constraint_checker'
69
71
  require 'database_consistency/checkers/column_checkers/primary_key_type_checker'
72
+ require 'database_consistency/checkers/column_checkers/enum_value_checker'
70
73
 
71
74
  require 'database_consistency/checkers/validator_checkers/validator_checker'
72
75
  require 'database_consistency/checkers/validator_checkers/missing_unique_index_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.5.3
4
+ version: 1.6.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-01 00:00:00.000000000 Z
11
+ date: 2022-12-03 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
@@ -206,6 +207,8 @@ files:
206
207
  - lib/database_consistency/writers/simple/association_missing_null_constraint.rb
207
208
  - lib/database_consistency/writers/simple/base.rb
208
209
  - lib/database_consistency/writers/simple/default_message.rb
210
+ - lib/database_consistency/writers/simple/enum_values_inconsistent_with_ar_enum.rb
211
+ - lib/database_consistency/writers/simple/enum_values_inconsistent_with_inclusion.rb
209
212
  - lib/database_consistency/writers/simple/has_one_missing_unique_index.rb
210
213
  - lib/database_consistency/writers/simple/inconsistent_enum_type.rb
211
214
  - lib/database_consistency/writers/simple/inconsistent_types.rb