database_consistency 0.4.0 → 0.5.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: f0980b34790693d62df725d9e527a8694480d343c44c5061250abe3900802b32
4
- data.tar.gz: 514a6e6590244f3c31b43d7c997fc90d4f3eae5e4748930e4d28eefb6945c01a
3
+ metadata.gz: 5feffbf59ee35344cc1f96e796cd63adea0ca6aa6b01eb81cacbb5c166bd082b
4
+ data.tar.gz: ac0bb8dbb583b572b785591f59bce8bebb307b267e07960707e72861e7d6b6ff
5
5
  SHA512:
6
- metadata.gz: f79aa59df5649f0313cb78023781c9ce59e73e2add191268606353530a957a299bed6b6834352b2fbb694cdef90c072ae1c5281cdf15e3187f65da04a758e874
7
- data.tar.gz: d1b45c15a664d2832bc3cde9a511a779fa1bd9ca39b2c7c990cdfdedeced6a6f9fcf8a50b526f07eb79b8664ea2183fab4445465d1507e9aea8770394933560b
6
+ metadata.gz: 189c7967c87ebac15dfe0e68a98041423148a37596508360f87cbba295790754575aabef0cd4740e4d3c5b0bb6766eb62675920fcb01972f723d7ccfa166ce4f
7
+ data.tar.gz: beddfa2491ec4ec8d22bac9b4b081933d2751dd9931f849f4713e9a3e2f41aaae8850a88ee3041adb154b1ed960a65949e25e3754d5706b079d1b612182ed77b
@@ -13,7 +13,7 @@ module DatabaseConsistency
13
13
 
14
14
  # @param [DatabaseConsistency::Configuration] configuration
15
15
  def enabled?(configuration)
16
- configuration.enabled?(checker_name, table_or_model_name, column_or_attribute_name)
16
+ configuration.enabled?(table_or_model_name, column_or_attribute_name, checker_name)
17
17
  end
18
18
 
19
19
  private
@@ -20,7 +20,7 @@ module DatabaseConsistency
20
20
  # | foreign key | status |
21
21
  # | ----------- | ------ |
22
22
  # | persisted | ok |
23
- # | missed | fail |
23
+ # | missing | fail |
24
24
  def check
25
25
  if model.connection.foreign_keys(model.table_name).find { |fk| fk.column == reflection.foreign_key.to_s }
26
26
  report_template(:ok)
@@ -2,7 +2,7 @@
2
2
 
3
3
  module DatabaseConsistency
4
4
  module Checkers
5
- # This class checks PresenceValidator
5
+ # This class checks if presence validator has non-null constraint in the database
6
6
  class ColumnPresenceChecker < ValidatorChecker
7
7
  WEAK_OPTIONS = %i[allow_nil allow_blank if unless].freeze
8
8
  # Message templates
@@ -23,8 +23,8 @@ module DatabaseConsistency
23
23
  # | ------------------------------- | -------- | ------ |
24
24
  # | at least one provided | required | fail |
25
25
  # | at least one provided | optional | ok |
26
- # | all missed | required | ok |
27
- # | all missed | optional | fail |
26
+ # | all missing | required | ok |
27
+ # | all missing | optional | fail |
28
28
  def check
29
29
  can_be_null = column.null
30
30
  has_weak_option = validator.options.slice(*WEAK_OPTIONS).any?
@@ -0,0 +1,49 @@
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 MissingUniqueIndexChecker < ValidatorChecker
7
+ MISSING_INDEX = 'should have unique index in the database'
8
+
9
+ def column_or_attribute_name
10
+ @column_or_attribute_name ||= index_columns.join('+')
11
+ end
12
+
13
+ private
14
+
15
+ # We skip check when:
16
+ # - validator is not a uniqueness validator
17
+ def preconditions
18
+ validator.kind == :uniqueness
19
+ end
20
+
21
+ # Table of possible statuses
22
+ # | unique index | status |
23
+ # | ------------ | ------ |
24
+ # | persisted | ok |
25
+ # | missing | fail |
26
+ def check
27
+ if unique_index
28
+ report_template(:ok)
29
+ else
30
+ report_template(:fail, MISSING_INDEX)
31
+ end
32
+ end
33
+
34
+ def unique_index
35
+ @unique_index ||= model.connection.indexes(model.table_name).find do |index|
36
+ index.unique && index.columns.sort == sorted_index_columns
37
+ end
38
+ end
39
+
40
+ def index_columns
41
+ @index_columns ||= ([attribute] + Array.wrap(validator.options[:scope])).map(&:to_s)
42
+ end
43
+
44
+ def sorted_index_columns
45
+ @sorted_index_columns ||= index_columns.sort
46
+ end
47
+ end
48
+ end
49
+ end
@@ -22,7 +22,7 @@ module DatabaseConsistency
22
22
  # | validation | status |
23
23
  # | ---------- | ------ |
24
24
  # | provided | ok |
25
- # | missed | fail |
25
+ # | missing | fail |
26
26
  #
27
27
  # We consider PresenceValidation, InclusionValidation or BelongsTo reflection using this column
28
28
  def check
@@ -6,7 +6,8 @@ module DatabaseConsistency
6
6
  class ModelsProcessor < BaseProcessor
7
7
  CHECKERS = [
8
8
  Checkers::ColumnPresenceChecker,
9
- Checkers::BelongsToPresenceChecker
9
+ Checkers::BelongsToPresenceChecker,
10
+ Checkers::MissingUniqueIndexChecker
10
11
  ].freeze
11
12
 
12
13
  private
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DatabaseConsistency
4
- VERSION = '0.4.0'
4
+ VERSION = '0.5.0'
5
5
  end
@@ -18,9 +18,9 @@ module DatabaseConsistency
18
18
  end
19
19
 
20
20
  def line(result)
21
- "#{result.status} #{result.table_or_model_name} #{result.column_or_attribute_name} #{result.message}".tap do |s|
22
- s.concat " (checker: #{result.checker_name})" if debug?
23
- end
21
+ s = "#{result.status} #{result.table_or_model_name} #{result.column_or_attribute_name} #{result.message}"
22
+ s += " (checker: #{result.checker_name})" if debug?
23
+ s
24
24
  end
25
25
  end
26
26
  end
@@ -15,6 +15,7 @@ require 'database_consistency/checkers/validator_checker'
15
15
  require 'database_consistency/checkers/column_presence_checker'
16
16
  require 'database_consistency/checkers/null_constraint_checker'
17
17
  require 'database_consistency/checkers/belongs_to_presence_checker'
18
+ require 'database_consistency/checkers/missing_unique_index_checker'
18
19
 
19
20
  require 'database_consistency/processors/base_processor'
20
21
  require 'database_consistency/processors/models_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: 0.4.0
4
+ version: 0.5.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: 2019-01-10 00:00:00.000000000 Z
11
+ date: 2019-01-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -113,6 +113,7 @@ files:
113
113
  - lib/database_consistency/checkers/base_checker.rb
114
114
  - lib/database_consistency/checkers/belongs_to_presence_checker.rb
115
115
  - lib/database_consistency/checkers/column_presence_checker.rb
116
+ - lib/database_consistency/checkers/missing_unique_index_checker.rb
116
117
  - lib/database_consistency/checkers/null_constraint_checker.rb
117
118
  - lib/database_consistency/checkers/table_checker.rb
118
119
  - lib/database_consistency/checkers/validator_checker.rb