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 +4 -4
- data/lib/database_consistency/checkers/base_checker.rb +1 -1
- data/lib/database_consistency/checkers/belongs_to_presence_checker.rb +1 -1
- data/lib/database_consistency/checkers/column_presence_checker.rb +3 -3
- data/lib/database_consistency/checkers/missing_unique_index_checker.rb +49 -0
- data/lib/database_consistency/checkers/null_constraint_checker.rb +1 -1
- data/lib/database_consistency/processors/models_processor.rb +2 -1
- data/lib/database_consistency/version.rb +1 -1
- data/lib/database_consistency/writers/simple_writer.rb +3 -3
- data/lib/database_consistency.rb +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5feffbf59ee35344cc1f96e796cd63adea0ca6aa6b01eb81cacbb5c166bd082b
|
4
|
+
data.tar.gz: ac0bb8dbb583b572b785591f59bce8bebb307b267e07960707e72861e7d6b6ff
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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?(
|
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
|
-
# |
|
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
|
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
|
27
|
-
# | all
|
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
|
-
# |
|
25
|
+
# | missing | fail |
|
26
26
|
#
|
27
27
|
# We consider PresenceValidation, InclusionValidation or BelongsTo reflection using this column
|
28
28
|
def check
|
@@ -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}"
|
22
|
-
|
23
|
-
|
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
|
data/lib/database_consistency.rb
CHANGED
@@ -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
|
+
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-
|
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
|