database_consistency 1.1.2 → 1.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9e4c24aeb235c8283ce761d7b4d1e3511246eae28c761a55b4f8ef180a090b22
|
4
|
+
data.tar.gz: d8e3b783eae3e68e88126c7f9d77de062748201652ec46492522eda6af0722bf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 005c1fa13f5aab3d833c9fd0690907f5df81a0d34ee2dcb5ea19ace51ed41f1438c803d56b95d3573eaa0938b8d9295a04dd5e2dafb6a6ab6ca48c6fcba3af4e
|
7
|
+
data.tar.gz: fab16372c3782df28f84aac35b997c31ccbb68d3be8ee0b8442e09061c8a85797da584153fc3de151ff1761227348b17041302260daede249b3b10753a1d610b
|
data/lib/database_consistency/checkers/validators_fraction_checkers/column_presence_checker.rb
CHANGED
@@ -7,6 +7,7 @@ module DatabaseConsistency
|
|
7
7
|
WEAK_OPTIONS = %i[allow_nil allow_blank if unless on].freeze
|
8
8
|
# Message templates
|
9
9
|
CONSTRAINT_MISSING = 'column should be required in the database'
|
10
|
+
ASSOCIATION_FOREIGN_KEY_CONSTRAINT_MISSING = 'association foreign key column should be required in the database'
|
10
11
|
POSSIBLE_NULL = 'column is required but there is possible null value insert'
|
11
12
|
|
12
13
|
private
|
@@ -15,11 +16,9 @@ module DatabaseConsistency
|
|
15
16
|
validator.kind == :presence
|
16
17
|
end
|
17
18
|
|
18
|
-
# We skip check when
|
19
|
-
# - there is no presence validators
|
20
|
-
# - there is no column in the database with given name
|
19
|
+
# We skip the check when there are no presence validators
|
21
20
|
def preconditions
|
22
|
-
validators.any?
|
21
|
+
validators.any?
|
23
22
|
end
|
24
23
|
|
25
24
|
# Table of possible statuses
|
@@ -33,17 +32,39 @@ module DatabaseConsistency
|
|
33
32
|
can_be_null = column.null
|
34
33
|
has_weak_option = validators.all? { |validator| validator.options.slice(*WEAK_OPTIONS).any? }
|
35
34
|
|
36
|
-
if can_be_null == has_weak_option
|
37
|
-
|
38
|
-
|
35
|
+
return report_template(:ok) if can_be_null == has_weak_option
|
36
|
+
return report_template(:fail, POSSIBLE_NULL) unless can_be_null
|
37
|
+
|
38
|
+
if regular_column
|
39
39
|
report_template(:fail, CONSTRAINT_MISSING)
|
40
40
|
else
|
41
|
-
report_template(:fail,
|
41
|
+
report_template(:fail, ASSOCIATION_FOREIGN_KEY_CONSTRAINT_MISSING)
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
45
45
|
def column
|
46
|
-
@column ||=
|
46
|
+
@column ||= regular_column || association_reference_column ||
|
47
|
+
(raise Errors::MissingField, "Missing column in #{model.table_name} for #{attribute}")
|
48
|
+
end
|
49
|
+
|
50
|
+
def regular_column
|
51
|
+
@regular_column ||= column_for_name(attribute.to_s)
|
52
|
+
end
|
53
|
+
|
54
|
+
def column_for_name(name)
|
55
|
+
model.columns.find { |field| field.name == name.to_s }
|
56
|
+
end
|
57
|
+
|
58
|
+
def association_reference_column
|
59
|
+
return unless association_reflection
|
60
|
+
|
61
|
+
column_for_name(association_reflection.foreign_key)
|
62
|
+
end
|
63
|
+
|
64
|
+
def association_reflection
|
65
|
+
model
|
66
|
+
.reflect_on_all_associations
|
67
|
+
.find { |reflection| reflection.belongs_to? && reflection.name == attribute }
|
47
68
|
end
|
48
69
|
end
|
49
70
|
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: 1.1.
|
4
|
+
version: 1.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Evgeniy Demin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-10-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|