database_consistency 1.3.8 → 1.4.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: ad78f367e1642231f8f17aa787385cd154dec73b9fd68ee0f506311335128d0e
4
- data.tar.gz: cd0d30bf84e70dffb926a9cae47542853ea59d68ec4e268318af1bee0e6094e4
3
+ metadata.gz: 9bc1994c7c24ab9a38d97c764fb0d9ce55e4fa71934df313f2cc693c466cecb7
4
+ data.tar.gz: 04110bdba61254592c2b22323555948ad06a78b8a63ce78a40175005096acb54
5
5
  SHA512:
6
- metadata.gz: 221df64634adcf4c025ed9937b38f4fed127639b39bb944c9b31b616917916e1cbf01165c934c35c8e5b2f338d6512ab5a371d9d0bcf43ffe25fa3258f73aa47
7
- data.tar.gz: bee363244d2240f94225429684e66a8d1dbdd545a22a03f8fdd70f3b40baea91d94be51aadc4822f0a19921541e472c668aaaea22504e7a1c5ff5de10014d005
6
+ metadata.gz: 581c8660605072f0cd877310813d71781b50de0af57aa5d85b216b57dd3ed040dc90944e165e51ff9b4728d180474157281ce1e028d9295b7137657367a4fa80
7
+ data.tar.gz: 0c9d42d9902485c01d208ac38110aa4c7742b4ceca00c369c4fc4c014e84df038b708f17b084f9cee7da8eef28a1f31b52d4a29fa2f597f7cf454af1cefc828c
@@ -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,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DatabaseConsistency
4
+ module Checkers
5
+ # This class checks enum types consistency
6
+ class EnumTypeChecker < EnumChecker
7
+ MissingColumn = Class.new(StandardError)
8
+
9
+ Report = DatabaseConsistency::ReportBuilder.define(
10
+ DatabaseConsistency::Report,
11
+ :column_type,
12
+ :values_types
13
+ )
14
+
15
+ private
16
+
17
+ def preconditions
18
+ true
19
+ end
20
+
21
+ def check
22
+ if valid?
23
+ report_template(:ok)
24
+ else
25
+ report_template(:fail, error_slug: :inconsistent_enum_type)
26
+ end
27
+ end
28
+
29
+ def report_template(status, error_slug: nil)
30
+ Report.new(
31
+ status: status,
32
+ error_slug: error_slug,
33
+ error_message: nil,
34
+ column_type: column_type,
35
+ values_types: values_types,
36
+ **report_attributes
37
+ )
38
+ end
39
+
40
+ def column_type_converter(type)
41
+ case type
42
+ when 'string' then String
43
+ when 'integer' then Integer
44
+ when 'decimal' then BigDecimal
45
+ when 'date' then Date
46
+ when 'datetime' then DateTime
47
+ when 'float' then Float
48
+ else type
49
+ end
50
+ end
51
+
52
+ def values_types
53
+ model.defined_enums[enum].values.map(&:class).uniq
54
+ end
55
+
56
+ def column_type
57
+ column = model.columns.find { |c| c.name.to_s == enum.to_s }
58
+ raise MissingColumn unless column
59
+
60
+ column.type.to_s
61
+ end
62
+
63
+ # @return [Boolean]
64
+ def valid?
65
+ values_types.all? { |type| type == column_type_converter(column_type) }
66
+ end
67
+ end
68
+ end
69
+ end
@@ -7,17 +7,10 @@ module DatabaseConsistency
7
7
  class Configuration
8
8
  DEFAULT_PATH = '.database_consistency.yml'
9
9
 
10
- def initialize(filepaths = DEFAULT_PATH)
11
- @configuration = Array(filepaths).each_with_object({}) do |filepath, result|
12
- content =
13
- if filepath && File.exist?(filepath)
14
- data = load_yaml_config_file(filepath)
15
- data.is_a?(Hash) ? data : {}
16
- else
17
- {}
18
- end
19
-
20
- combine_configs!(result, content)
10
+ def initialize(file_paths = DEFAULT_PATH)
11
+ @configuration = existing_configurations(file_paths).then do |existing_paths|
12
+ puts "Loaded configurations: #{existing_paths.join(', ')}"
13
+ extract_configurations(existing_paths)
21
14
  end
22
15
  end
23
16
 
@@ -55,6 +48,21 @@ module DatabaseConsistency
55
48
 
56
49
  attr_reader :configuration
57
50
 
51
+ def existing_configurations(paths)
52
+ Array(paths).select do |filepath|
53
+ filepath && File.exist?(filepath)
54
+ end
55
+ end
56
+
57
+ def extract_configurations(paths)
58
+ Array(paths).each_with_object({}) do |filepath, result|
59
+ data = load_yaml_config_file(filepath)
60
+ content = data.is_a?(Hash) ? data : {}
61
+
62
+ combine_configs!(result, content)
63
+ end
64
+ end
65
+
58
66
  def global_enabling
59
67
  value = configuration.dig('DatabaseConsistencyCheckers', 'All', 'enabled')
60
68
 
@@ -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.8'
4
+ VERSION = '1.4.0'
5
5
  end
@@ -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.8
4
+ version: 1.4.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-11-23 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