database_consistency 0.8.11 → 0.8.12

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: 964b3ab9787dba65ec8981c690f93ec0b0c5a7848e7e0b835ef2aa9a9a415025
4
- data.tar.gz: '09d5264224cf230a7d08157101c16d5fe8fcb180684876265dcb16c6c3ecc3f1'
3
+ metadata.gz: ba8c6b25dd4a98dcc7de0dfad22a1c33e4c3a325ab999aedd037fe743c108bdd
4
+ data.tar.gz: 9c563d73dec087a460e10b9acedc365bbc45059028df5571d59ec6dbfa11f1b1
5
5
  SHA512:
6
- metadata.gz: 661de8be036f7cd5640e6240e3abd605a013bda1fe2b7fcd52243c88d0b0251f9a57ba852117ec75827d125873f982c9c060d991ace7fff296312f7a3bf390c6
7
- data.tar.gz: e87befd21bc84b26608b1ded54a6e0199958e4fe9993071db93d511b6ab223c0cb468851f472ab927f7a46f4c2b9d4ce15bb6649a01e64641a1551d05eb6385c
6
+ metadata.gz: f51d16f3806889640ad04fca4cddda635c708063d0c6554a6f0589678ca8cfe290db7a9709df5232fe0bbfb14fd8532b8498bb6204e2a341473dd27ae3c40400
7
+ data.tar.gz: '09097fa4bfec79f64a87df086601253594ce409b4d11b736df0127b3b9af10145f721d761ab90c8a0865cd12c48ecb9429211299d1244e02ddf2a88e11c971a9'
@@ -33,11 +33,15 @@ require 'database_consistency/checkers/validator_checkers/missing_unique_index_c
33
33
  require 'database_consistency/checkers/validators_fraction_checkers/validators_fraction_checker'
34
34
  require 'database_consistency/checkers/validators_fraction_checkers/column_presence_checker'
35
35
 
36
+ require 'database_consistency/checkers/index_checkers/index_checker'
37
+ require 'database_consistency/checkers/index_checkers/unique_index_checker'
38
+
36
39
  require 'database_consistency/processors/base_processor'
37
40
  require 'database_consistency/processors/associations_processor'
38
41
  require 'database_consistency/processors/validators_processor'
39
42
  require 'database_consistency/processors/columns_processor'
40
43
  require 'database_consistency/processors/validators_fractions_processor'
44
+ require 'database_consistency/processors/indexes_processor'
41
45
 
42
46
  # The root module
43
47
  module DatabaseConsistency
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DatabaseConsistency
4
+ module Checkers
5
+ # The base class for table checkers
6
+ class IndexChecker < BaseChecker
7
+ attr_reader :model, :index
8
+
9
+ def initialize(model, index)
10
+ @model = model
11
+ @index = index
12
+ end
13
+
14
+ def column_or_attribute_name
15
+ @column_or_attribute_name ||= index.name.to_s
16
+ end
17
+
18
+ def table_or_model_name
19
+ @table_or_model_name ||= model.name.to_s
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DatabaseConsistency
4
+ module Checkers
5
+ # This class checks missing uniqueness validator
6
+ class UniqueIndexChecker < IndexChecker
7
+ # Message templates
8
+ VALIDATOR_MISSING = 'index is unique in the database but do not have uniqueness validator'
9
+
10
+ private
11
+
12
+ # We skip check when:
13
+ # - index is not unique
14
+ def preconditions
15
+ index.unique
16
+ end
17
+
18
+ # Table of possible statuses
19
+ # | validation | status |
20
+ # | ---------- | ------ |
21
+ # | provided | ok |
22
+ # | missing | fail |
23
+ #
24
+ def check
25
+ if valid?
26
+ report_template(:ok)
27
+ else
28
+ report_template(:fail, VALIDATOR_MISSING)
29
+ end
30
+ end
31
+
32
+ def valid?
33
+ uniqueness_validators = model.validators.select { |validator| validator.kind == :uniqueness }
34
+
35
+ uniqueness_validators.any? do |validator|
36
+ validator.attributes.any? do |attribute|
37
+ sorted_index_columns == Helper.sorted_uniqueness_validator_columns(attribute, validator, model)
38
+ end
39
+ end
40
+ end
41
+
42
+ def sorted_index_columns
43
+ @sorted_index_columns ||= Helper.extract_index_columns(index.columns).sort
44
+ end
45
+ end
46
+ end
47
+ end
@@ -7,7 +7,7 @@ module DatabaseConsistency
7
7
  MISSING_INDEX = 'model should have proper unique index in the database'
8
8
 
9
9
  def column_or_attribute_name
10
- @column_or_attribute_name ||= index_columns.join('+')
10
+ @column_or_attribute_name ||= Helper.uniqueness_validator_columns(attribute, validator, model).join('+')
11
11
  end
12
12
 
13
13
  private
@@ -33,42 +33,12 @@ module DatabaseConsistency
33
33
 
34
34
  def unique_index
35
35
  @unique_index ||= model.connection.indexes(model.table_name).find do |index|
36
- index.unique && extract_index_columns(index.columns).sort == sorted_index_columns
36
+ index.unique && Helper.extract_index_columns(index.columns).sort == sorted_uniqueness_validator_columns
37
37
  end
38
38
  end
39
39
 
40
- # @return [Array<String>]
41
- def extract_index_columns(index_columns)
42
- return index_columns unless index_columns.is_a?(String)
43
-
44
- index_columns.split(',')
45
- .map(&:strip)
46
- .map { |str| str.gsub(/lower\(/i, 'lower(') }
47
- .map { |str| str.gsub(/\(([^)]+)\)::\w+/, '\1') }
48
- .map { |str| str.gsub(/'([^)]+)'::\w+/, '\1') }
49
- end
50
-
51
- def index_columns
52
- @index_columns ||= ([wrapped_attribute_name] + scope_columns).map(&:to_s)
53
- end
54
-
55
- def scope_columns
56
- @scope_columns ||= Array.wrap(validator.options[:scope]).map do |scope_item|
57
- model._reflect_on_association(scope_item)&.foreign_key || scope_item
58
- end
59
- end
60
-
61
- def sorted_index_columns
62
- @sorted_index_columns ||= index_columns.sort
63
- end
64
-
65
- # @return [String]
66
- def wrapped_attribute_name
67
- if validator.options[:case_sensitive].nil? || validator.options[:case_sensitive]
68
- attribute
69
- else
70
- "lower(#{attribute})"
71
- end
40
+ def sorted_uniqueness_validator_columns
41
+ @sorted_uniqueness_validator_columns ||= Helper.sorted_uniqueness_validator_columns(attribute, validator, model)
72
42
  end
73
43
  end
74
44
  end
@@ -34,5 +34,39 @@ module DatabaseConsistency
34
34
 
35
35
  associations
36
36
  end
37
+
38
+ # @return [Array<String>]
39
+ def extract_index_columns(index_columns)
40
+ return index_columns unless index_columns.is_a?(String)
41
+
42
+ index_columns.split(',')
43
+ .map(&:strip)
44
+ .map { |str| str.gsub(/lower\(/i, 'lower(') }
45
+ .map { |str| str.gsub(/\(([^)]+)\)::\w+/, '\1') }
46
+ .map { |str| str.gsub(/'([^)]+)'::\w+/, '\1') }
47
+ end
48
+
49
+ def sorted_uniqueness_validator_columns(attribute, validator, model)
50
+ uniqueness_validator_columns(attribute, validator, model).sort
51
+ end
52
+
53
+ def uniqueness_validator_columns(attribute, validator, model)
54
+ ([wrapped_attribute_name(attribute, validator)] + scope_columns(validator, model)).map(&:to_s)
55
+ end
56
+
57
+ def scope_columns(validator, model)
58
+ Array.wrap(validator.options[:scope]).map do |scope_item|
59
+ model._reflect_on_association(scope_item)&.foreign_key || scope_item
60
+ end
61
+ end
62
+
63
+ # @return [String]
64
+ def wrapped_attribute_name(attribute, validator)
65
+ if validator.options[:case_sensitive].nil? || validator.options[:case_sensitive]
66
+ attribute
67
+ else
68
+ "lower(#{attribute})"
69
+ end
70
+ end
37
71
  end
38
72
  end
@@ -8,7 +8,8 @@ module DatabaseConsistency
8
8
  ColumnsProcessor,
9
9
  ValidatorsProcessor,
10
10
  AssociationsProcessor,
11
- ValidatorsFractionsProcessor
11
+ ValidatorsFractionsProcessor,
12
+ IndexesProcessor
12
13
  ].flat_map do |processor|
13
14
  processor.new(configuration).reports
14
15
  end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DatabaseConsistency
4
+ module Processors
5
+ # The class to process indexes
6
+ class IndexesProcessor < BaseProcessor
7
+ CHECKERS = [
8
+ Checkers::UniqueIndexChecker
9
+ ].freeze
10
+
11
+ private
12
+
13
+ def check # rubocop:disable Metrics/AbcSize
14
+ Helper.parent_models.flat_map do |model|
15
+ next unless configuration.enabled?(model.name.to_s)
16
+
17
+ indexes = ActiveRecord::Base.connection.indexes(model.table_name)
18
+
19
+ indexes.flat_map do |index|
20
+ enabled_checkers.map do |checker_class|
21
+ checker = checker_class.new(model, index)
22
+ checker.report_if_enabled?(configuration)
23
+ end
24
+ end
25
+ end.compact
26
+ end
27
+ end
28
+ end
29
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DatabaseConsistency
4
- VERSION = '0.8.11'
4
+ VERSION = '0.8.12'
5
5
  end
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: 0.8.11
4
+ version: 0.8.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evgeniy Demin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-11-19 00:00:00.000000000 Z
11
+ date: 2020-12-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -140,6 +140,8 @@ files:
140
140
  - lib/database_consistency/checkers/column_checkers/length_constraint_checker.rb
141
141
  - lib/database_consistency/checkers/column_checkers/null_constraint_checker.rb
142
142
  - lib/database_consistency/checkers/column_checkers/primary_key_type_checker.rb
143
+ - lib/database_consistency/checkers/index_checkers/index_checker.rb
144
+ - lib/database_consistency/checkers/index_checkers/unique_index_checker.rb
143
145
  - lib/database_consistency/checkers/validator_checkers/belongs_to_presence_checker.rb
144
146
  - lib/database_consistency/checkers/validator_checkers/missing_unique_index_checker.rb
145
147
  - lib/database_consistency/checkers/validator_checkers/validator_checker.rb
@@ -154,6 +156,7 @@ files:
154
156
  - lib/database_consistency/processors/associations_processor.rb
155
157
  - lib/database_consistency/processors/base_processor.rb
156
158
  - lib/database_consistency/processors/columns_processor.rb
159
+ - lib/database_consistency/processors/indexes_processor.rb
157
160
  - lib/database_consistency/processors/validators_fractions_processor.rb
158
161
  - lib/database_consistency/processors/validators_processor.rb
159
162
  - lib/database_consistency/rescue_error.rb