database_consistency 3.0.5 → 3.0.6
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 +4 -4
- data/lib/database_consistency/checkers/association_checkers/polymorphic_association_nullability_checker.rb +47 -0
- data/lib/database_consistency/checkers/index_checkers/redundant_unique_index_checker.rb +1 -1
- data/lib/database_consistency/helper.rb +10 -4
- data/lib/database_consistency/version.rb +1 -1
- data/lib/database_consistency/writers/simple/polymorphic_association_nullability_mismatch.rb +31 -0
- data/lib/database_consistency.rb +2 -0
- metadata +4 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 41244c7508ff0579de421a22e770767de915176ef2e43ed9371daadf6cdd9243
|
|
4
|
+
data.tar.gz: bb38bf3073e135b1e6c039a451237a5bcd71a4c03484e4926ddc00cc41faa7b3
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 5b26db588fda6d08012fd845ad04141d6a03bb26c058c6cf9042c344c6a6cf6304c98efc89d9ae89c0ee58d8eb4a918e2d1a725578aa60782df036b665f2d638
|
|
7
|
+
data.tar.gz: c1b6d1abd2eab007e48ad83e157feaee26ea2b2485c243c8bde0e44685f862b01386d9ef5295693883ae3decc85274f27c6a33b9392c799108ee80e8f0bd5f1b
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module DatabaseConsistency
|
|
4
|
+
module Checkers
|
|
5
|
+
# This class checks that polymorphic association columns have matching null constraints
|
|
6
|
+
class PolymorphicAssociationNullabilityChecker < AssociationChecker
|
|
7
|
+
Report = ReportBuilder.define(
|
|
8
|
+
DatabaseConsistency::Report,
|
|
9
|
+
:foreign_key,
|
|
10
|
+
:foreign_type
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
private
|
|
14
|
+
|
|
15
|
+
def preconditions
|
|
16
|
+
association.belongs_to? && association.polymorphic? && foreign_key_column && foreign_type_column
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def check
|
|
20
|
+
if foreign_key_column.null == foreign_type_column.null
|
|
21
|
+
report_template(:ok)
|
|
22
|
+
else
|
|
23
|
+
report_template(:fail, error_slug: :polymorphic_association_nullability_mismatch)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def report_template(status, error_slug: nil)
|
|
28
|
+
Report.new(
|
|
29
|
+
status: status,
|
|
30
|
+
error_slug: error_slug,
|
|
31
|
+
error_message: nil,
|
|
32
|
+
foreign_key: association.foreign_key.to_s,
|
|
33
|
+
foreign_type: association.foreign_type.to_s,
|
|
34
|
+
**report_attributes
|
|
35
|
+
)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def foreign_key_column
|
|
39
|
+
@foreign_key_column ||= model.columns_hash[association.foreign_key.to_s]
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def foreign_type_column
|
|
43
|
+
@foreign_type_column ||= model.columns_hash[association.foreign_type.to_s]
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
@@ -60,7 +60,7 @@ module DatabaseConsistency
|
|
|
60
60
|
|
|
61
61
|
def contain_index?(another_index)
|
|
62
62
|
another_index_columns = Helper.extract_index_columns(another_index.columns)
|
|
63
|
-
|
|
63
|
+
(another_index_columns - index_columns).empty?
|
|
64
64
|
end
|
|
65
65
|
|
|
66
66
|
def index_columns
|
|
@@ -156,12 +156,18 @@ module DatabaseConsistency
|
|
|
156
156
|
nil
|
|
157
157
|
end
|
|
158
158
|
|
|
159
|
-
# Builds the effective uniqueness constraint enforced by a validator
|
|
160
|
-
#
|
|
161
|
-
# `
|
|
159
|
+
# Builds the effective uniqueness constraint enforced by a validator.
|
|
160
|
+
#
|
|
161
|
+
# When the validator carries an explicit `conditions` proc, that proc is the
|
|
162
|
+
# authoritative partial predicate. The implicit `allow_nil` / `allow_blank`
|
|
163
|
+
# guard on the validated attribute is redundant against a unique index (which
|
|
164
|
+
# already treats NULLs as distinct), so it is only used as a fallback when no
|
|
165
|
+
# explicit conditions are present. Otherwise gems that always set `allow_nil`
|
|
166
|
+
# (e.g. database_validations) would append a duplicate or extra
|
|
167
|
+
# `attribute IS NOT NULL` clause and never match the partial index.
|
|
162
168
|
def uniqueness_validator_where_sql(model, attribute, validator)
|
|
163
169
|
conditions_sql = conditions_where_sql(model, validator.options[:conditions])
|
|
164
|
-
guard_sql = uniqueness_validator_guard_sql(model, attribute, validator)
|
|
170
|
+
guard_sql = conditions_sql ? nil : uniqueness_validator_guard_sql(model, attribute, validator)
|
|
165
171
|
|
|
166
172
|
sql_parts = [conditions_sql, guard_sql].reject { |part| part.nil? || part == '' }
|
|
167
173
|
return nil if sql_parts.empty?
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module DatabaseConsistency
|
|
4
|
+
module Writers
|
|
5
|
+
module Simple
|
|
6
|
+
class PolymorphicAssociationNullabilityMismatch < Base # :nodoc:
|
|
7
|
+
private
|
|
8
|
+
|
|
9
|
+
def template
|
|
10
|
+
'polymorphic association columns (%<foreign_key>s and %<foreign_type>s) should have matching null constraints'
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def attributes
|
|
14
|
+
{
|
|
15
|
+
foreign_key: report.foreign_key,
|
|
16
|
+
foreign_type: report.foreign_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
|
+
foreign_key: report.foreign_key,
|
|
25
|
+
foreign_type: report.foreign_type
|
|
26
|
+
}
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
data/lib/database_consistency.rb
CHANGED
|
@@ -46,6 +46,7 @@ require 'database_consistency/writers/simple/missing_association_class'
|
|
|
46
46
|
require 'database_consistency/writers/simple/missing_table'
|
|
47
47
|
require 'database_consistency/writers/simple/implicit_order_column_missing'
|
|
48
48
|
require 'database_consistency/writers/simple/missing_dependent_destroy'
|
|
49
|
+
require 'database_consistency/writers/simple/polymorphic_association_nullability_mismatch'
|
|
49
50
|
require 'database_consistency/writers/simple/missing_index_find_by'
|
|
50
51
|
require 'database_consistency/writers/simple/view_missing_primary_key'
|
|
51
52
|
require 'database_consistency/writers/simple/view_primary_key_column_missing'
|
|
@@ -91,6 +92,7 @@ require 'database_consistency/checkers/association_checkers/foreign_key_type_che
|
|
|
91
92
|
require 'database_consistency/checkers/association_checkers/foreign_key_cascade_checker'
|
|
92
93
|
require 'database_consistency/checkers/association_checkers/missing_association_class_checker'
|
|
93
94
|
require 'database_consistency/checkers/association_checkers/missing_dependent_destroy_checker'
|
|
95
|
+
require 'database_consistency/checkers/association_checkers/polymorphic_association_nullability_checker'
|
|
94
96
|
|
|
95
97
|
require 'database_consistency/checkers/column_checkers/column_checker'
|
|
96
98
|
require 'database_consistency/checkers/column_checkers/null_constraint_checker'
|
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: 3.0.
|
|
4
|
+
version: 3.0.6
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Evgeniy Demin
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-
|
|
11
|
+
date: 2026-07-19 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: activerecord
|
|
@@ -153,6 +153,7 @@ files:
|
|
|
153
153
|
- lib/database_consistency/checkers/association_checkers/missing_association_class_checker.rb
|
|
154
154
|
- lib/database_consistency/checkers/association_checkers/missing_dependent_destroy_checker.rb
|
|
155
155
|
- lib/database_consistency/checkers/association_checkers/missing_index_checker.rb
|
|
156
|
+
- lib/database_consistency/checkers/association_checkers/polymorphic_association_nullability_checker.rb
|
|
156
157
|
- lib/database_consistency/checkers/base_checker.rb
|
|
157
158
|
- lib/database_consistency/checkers/column_checkers/column_checker.rb
|
|
158
159
|
- lib/database_consistency/checkers/column_checkers/enum_value_checker.rb
|
|
@@ -240,6 +241,7 @@ files:
|
|
|
240
241
|
- lib/database_consistency/writers/simple/null_constraint_association_misses_validator.rb
|
|
241
242
|
- lib/database_consistency/writers/simple/null_constraint_misses_validator.rb
|
|
242
243
|
- lib/database_consistency/writers/simple/null_constraint_missing.rb
|
|
244
|
+
- lib/database_consistency/writers/simple/polymorphic_association_nullability_mismatch.rb
|
|
243
245
|
- lib/database_consistency/writers/simple/possible_null.rb
|
|
244
246
|
- lib/database_consistency/writers/simple/redundant_case_insensitive_option.rb
|
|
245
247
|
- lib/database_consistency/writers/simple/redundant_index.rb
|