database_consistency 1.3.9 → 1.4.1

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: c589e055624919d847ef24864b767c55ebb9bb37d18da70b0558e73c2336815c
4
- data.tar.gz: 53bd691bf67c2a014d8c96cfd1e93f59699ace4c6a1ba41befeca8557c05fda6
3
+ metadata.gz: 5c6d7fe7d9a50ab2233cca7b018d096f9d47b850e2fb92b2724fecba15eb78e6
4
+ data.tar.gz: 5c98abb3adb05dfddb3c586e3eae4a87d35069d07332c516154791514cc9c70f
5
5
  SHA512:
6
- metadata.gz: f15b6e520813a6cc98105f250bdfa4397f12c1fff90357680681bafdba028a748bb74de0e43ac22fd377fcdf9569e619d0bd09e14095301c7ea353c45dc71146
7
- data.tar.gz: 27e3773b5458c410b80c6640e5e1fc0e863faf568a7b26cf0bddbf8441411971e79ffe8ede2d77fae11c1cf590e7fe13439f0c32ee423305ad0d3d77c77b98f1
6
+ metadata.gz: 32ee0e823c4c893f5e062f847f33dca5fe6094bfc98d12786f879cbe6a2d2182a44f5d9756271550b524093c56030d38f72ce4ff58de6175b2936852e3e7d3d0
7
+ data.tar.gz: 210a2a55056fee4992ad8e7b719aa42604b03944891e2061976b27972ad8fd5c7b8ac920fb5b888f193a0dd2fbb27e2a205ffba1252a83c736a5926a471641db
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DatabaseConsistency
4
+ module Checkers
5
+ # The base class for enum checkers
6
+ class EnumChecker < BaseChecker
7
+ attr_reader :model, :enum
8
+
9
+ def initialize(model, enum)
10
+ super()
11
+ @model = model
12
+ @enum = enum
13
+ end
14
+
15
+ def column_or_attribute_name
16
+ @column_or_attribute_name ||= enum.to_s
17
+ end
18
+
19
+ def table_or_model_name
20
+ @table_or_model_name ||= model.name.to_s
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DatabaseConsistency
4
+ module Checkers
5
+ # This class checks enum types consistency
6
+ class EnumTypeChecker < EnumChecker
7
+ Report = DatabaseConsistency::ReportBuilder.define(
8
+ DatabaseConsistency::Report,
9
+ :column_type,
10
+ :values_types
11
+ )
12
+
13
+ private
14
+
15
+ def preconditions
16
+ column.present?
17
+ end
18
+
19
+ def check
20
+ if valid?
21
+ report_template(:ok)
22
+ else
23
+ report_template(:fail, error_slug: :inconsistent_enum_type)
24
+ end
25
+ end
26
+
27
+ def report_template(status, error_slug: nil)
28
+ Report.new(
29
+ status: status,
30
+ error_slug: error_slug,
31
+ error_message: nil,
32
+ column_type: column_type,
33
+ values_types: values_types,
34
+ **report_attributes
35
+ )
36
+ end
37
+
38
+ def column_type_converter(type)
39
+ case type
40
+ when 'string' then String
41
+ when 'integer' then Integer
42
+ when 'decimal' then BigDecimal
43
+ when 'date' then Date
44
+ when 'datetime' then DateTime
45
+ when 'float' then Float
46
+ else type
47
+ end
48
+ end
49
+
50
+ def values_types
51
+ model.defined_enums[enum].values.map(&:class).uniq
52
+ end
53
+
54
+ def column
55
+ @column ||= model.columns.find { |c| c.name.to_s == enum.to_s }
56
+ end
57
+
58
+ def column_type
59
+ column.type.to_s
60
+ end
61
+
62
+ # @return [Boolean]
63
+ def valid?
64
+ values_types.all? { |type| type == column_type_converter(column_type) }
65
+ end
66
+ end
67
+ end
68
+ end
@@ -20,26 +20,13 @@ module DatabaseConsistency
20
20
 
21
21
  # We skip the check when there are no presence validators
22
22
  def preconditions
23
- validators.any? && !association?
23
+ column && validators.any? && !association?
24
24
  end
25
25
 
26
26
  def association?
27
27
  model._reflect_on_association(attribute)&.macro == :has_one
28
28
  end
29
29
 
30
- # Table of possible statuses
31
- # | allow_nil/allow_blank/if/unless | database | status |
32
- # | ------------------------------- | -------- | ------ |
33
- # | at least one provided | required | fail |
34
- # | at least one provided | optional | ok |
35
- # | all missing | required | ok |
36
- # | all missing | optional | fail |
37
- def check
38
- report_message
39
- rescue Errors::MissingField => e
40
- report_template(:fail, error_message: e.message)
41
- end
42
-
43
30
  def report_template(status, error_message: nil, error_slug: nil)
44
31
  Report.new(
45
32
  status: status,
@@ -55,7 +42,7 @@ module DatabaseConsistency
55
42
  validators.all? { |validator| validator.options.slice(*WEAK_OPTIONS).any? }
56
43
  end
57
44
 
58
- def report_message # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
45
+ def check # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
59
46
  can_be_null = column.null
60
47
  has_weak_option = weak_option?
61
48
 
@@ -84,9 +71,7 @@ module DatabaseConsistency
84
71
  end
85
72
 
86
73
  def column
87
- @column ||= regular_column ||
88
- association_reference_column ||
89
- (raise Errors::MissingField, "column (#{attribute}) is missing in table (#{model.table_name}) but used for presence validation") # rubocop:disable Layout/LineLength
74
+ @column ||= (regular_column || association_reference_column)
90
75
  end
91
76
 
92
77
  def regular_column
@@ -9,7 +9,8 @@ module DatabaseConsistency
9
9
  ValidatorsProcessor,
10
10
  AssociationsProcessor,
11
11
  ValidatorsFractionsProcessor,
12
- IndexesProcessor
12
+ IndexesProcessor,
13
+ EnumsProcessor
13
14
  ].flat_map do |processor|
14
15
  processor.new(configuration).reports
15
16
  end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DatabaseConsistency
4
+ module Processors
5
+ # The class to process enums
6
+ class EnumsProcessor < BaseProcessor
7
+ CHECKERS = [
8
+ Checkers::EnumTypeChecker
9
+ ].freeze
10
+
11
+ private
12
+
13
+ def check
14
+ Helper.models.flat_map do |model|
15
+ next unless configuration.enabled?(model.name.to_s)
16
+
17
+ model.defined_enums.keys.flat_map do |enum|
18
+ enabled_checkers.map do |checker_class|
19
+ checker = checker_class.new(model, enum)
20
+ checker.report_if_enabled?(configuration)
21
+ end
22
+ end
23
+ end.compact
24
+ end
25
+ end
26
+ end
27
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DatabaseConsistency
4
- VERSION = '1.3.9'
4
+ VERSION = '1.4.1'
5
5
  end
@@ -17,7 +17,9 @@ module DatabaseConsistency
17
17
  private
18
18
 
19
19
  def migration
20
- File.read(template_path) % attributes.merge(migration_configuration(migration_name))
20
+ attributes.merge(migration_configuration(migration_name)).reduce(File.read(template_path)) do |str, (k, v)|
21
+ str.gsub("%<#{k}>s", v.to_s)
22
+ end
21
23
  end
22
24
  end
23
25
  end
@@ -47,7 +47,9 @@ module DatabaseConsistency
47
47
  end
48
48
 
49
49
  def message_text
50
- template % attributes
50
+ attributes.reduce(template) do |str, (k, v)|
51
+ str.gsub("%<#{k}>s", v.to_s)
52
+ end
51
53
  end
52
54
 
53
55
  def attributes
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DatabaseConsistency
4
+ module Writers
5
+ module Simple
6
+ class InconsistentEnumType < Base # :nodoc:
7
+ private
8
+
9
+ def template
10
+ 'enum has %<values_types>s types but column has %<column_type>s type'
11
+ end
12
+
13
+ def attributes
14
+ {
15
+ values_types: report.values_types.join(', '),
16
+ column_type: report.column_type
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
+ }
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -22,7 +22,8 @@ module DatabaseConsistency
22
22
  possible_null: Simple::PossibleNull,
23
23
  redundant_index: Simple::RedundantIndex,
24
24
  redundant_unique_index: Simple::RedundantUniqueIndex,
25
- small_primary_key: Simple::SmallPrimaryKey
25
+ small_primary_key: Simple::SmallPrimaryKey,
26
+ inconsistent_enum_type: Simple::InconsistentEnumType
26
27
  }.freeze
27
28
 
28
29
  def write
@@ -32,6 +32,7 @@ require 'database_consistency/writers/simple/null_constraint_misses_validator'
32
32
  require 'database_consistency/writers/simple/null_constraint_missing'
33
33
  require 'database_consistency/writers/simple/possible_null'
34
34
  require 'database_consistency/writers/simple/small_primary_key'
35
+ require 'database_consistency/writers/simple/inconsistent_enum_type'
35
36
  require 'database_consistency/writers/simple_writer'
36
37
 
37
38
  require 'database_consistency/writers/autofix/helpers/migration'
@@ -51,6 +52,9 @@ require 'database_consistency/databases/types/sqlite'
51
52
 
52
53
  require 'database_consistency/checkers/base_checker'
53
54
 
55
+ require 'database_consistency/checkers/enum_checkers/enum_checker'
56
+ require 'database_consistency/checkers/enum_checkers/enum_type_checker'
57
+
54
58
  require 'database_consistency/checkers/association_checkers/association_checker'
55
59
  require 'database_consistency/checkers/association_checkers/missing_index_checker'
56
60
  require 'database_consistency/checkers/association_checkers/foreign_key_checker'
@@ -73,6 +77,7 @@ require 'database_consistency/checkers/index_checkers/redundant_index_checker'
73
77
  require 'database_consistency/checkers/index_checkers/redundant_unique_index_checker'
74
78
 
75
79
  require 'database_consistency/processors/base_processor'
80
+ require 'database_consistency/processors/enums_processor'
76
81
  require 'database_consistency/processors/associations_processor'
77
82
  require 'database_consistency/processors/validators_processor'
78
83
  require 'database_consistency/processors/columns_processor'
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.3.9
4
+ version: 1.4.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: 2022-11-26 00:00:00.000000000 Z
11
+ date: 2022-11-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -155,6 +155,8 @@ files:
155
155
  - lib/database_consistency/checkers/column_checkers/length_constraint_checker.rb
156
156
  - lib/database_consistency/checkers/column_checkers/null_constraint_checker.rb
157
157
  - lib/database_consistency/checkers/column_checkers/primary_key_type_checker.rb
158
+ - lib/database_consistency/checkers/enum_checkers/enum_checker.rb
159
+ - lib/database_consistency/checkers/enum_checkers/enum_type_checker.rb
158
160
  - lib/database_consistency/checkers/index_checkers/index_checker.rb
159
161
  - lib/database_consistency/checkers/index_checkers/redundant_index_checker.rb
160
162
  - lib/database_consistency/checkers/index_checkers/redundant_unique_index_checker.rb
@@ -172,6 +174,7 @@ files:
172
174
  - lib/database_consistency/processors/associations_processor.rb
173
175
  - lib/database_consistency/processors/base_processor.rb
174
176
  - lib/database_consistency/processors/columns_processor.rb
177
+ - lib/database_consistency/processors/enums_processor.rb
175
178
  - lib/database_consistency/processors/indexes_processor.rb
176
179
  - lib/database_consistency/processors/validators_fractions_processor.rb
177
180
  - lib/database_consistency/processors/validators_processor.rb
@@ -202,6 +205,7 @@ files:
202
205
  - lib/database_consistency/writers/simple/base.rb
203
206
  - lib/database_consistency/writers/simple/default_message.rb
204
207
  - lib/database_consistency/writers/simple/has_one_missing_unique_index.rb
208
+ - lib/database_consistency/writers/simple/inconsistent_enum_type.rb
205
209
  - lib/database_consistency/writers/simple/inconsistent_types.rb
206
210
  - lib/database_consistency/writers/simple/length_validator_greater_limit.rb
207
211
  - lib/database_consistency/writers/simple/length_validator_lower_limit.rb