database_consistency 3.0.6 → 3.0.7
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/validators_fraction_checkers/numericality_constraint_checker.rb +70 -0
- data/lib/database_consistency/version.rb +1 -1
- data/lib/database_consistency/writers/simple/numericality_check_constraint_missing.rb +22 -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: 3cf17a396eca47c805aaa0bb4da89132a7c890b122a44793678ea316cd4f627c
|
|
4
|
+
data.tar.gz: 84a0f6ce121e286275a17683259db3531bdef446e21fa7b2459b77db8566bfd4
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 5796a54dd3a57c34e5743b919b5407c0642235e0bc39c1c6da63a6bf960ca39df350c3e37031229f396194f4abe8607850680459b3846a87a6e5f1f4180d6282
|
|
7
|
+
data.tar.gz: e5e448dcaae9070e70f6a30000dad04a4bea1f3f481c1dc2212adc71196c3170da7d75fdf111bba55d7de026f714b368445bcbdb32a513e5d6fe9b4535a9847d
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module DatabaseConsistency
|
|
4
|
+
module Checkers
|
|
5
|
+
# This class checks if numericality validator has check constraint in the database
|
|
6
|
+
class NumericalityConstraintChecker < ValidatorsFractionChecker
|
|
7
|
+
SQL_KEYWORDS = %w[
|
|
8
|
+
and or not null is true false in like between case when then else end check
|
|
9
|
+
exists select where having order group limit offset distinct all any some
|
|
10
|
+
from join on using as by union into values set update delete insert with
|
|
11
|
+
].freeze
|
|
12
|
+
PLAIN_IDENTIFIER_PATTERN =
|
|
13
|
+
/\b([a-zA-Z_][a-zA-Z0-9_]*(?:\.[a-zA-Z_][a-zA-Z0-9_]*)?)\b(?!\s*\()/.freeze
|
|
14
|
+
|
|
15
|
+
private
|
|
16
|
+
|
|
17
|
+
def filter(validator)
|
|
18
|
+
validator.kind == :numericality
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def preconditions
|
|
22
|
+
model.connection.respond_to?(:check_constraints) &&
|
|
23
|
+
model.connection.table_exists?(model.table_name) &&
|
|
24
|
+
column
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def check
|
|
28
|
+
if check_constraint_for_column?
|
|
29
|
+
report_template(:ok)
|
|
30
|
+
else
|
|
31
|
+
report_template(:fail, error_slug: :numericality_check_constraint_missing)
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def check_constraint_for_column?
|
|
36
|
+
check_constraints.any? do |constraint|
|
|
37
|
+
constraint_columns(constraint.expression).include?(column.name.downcase)
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def check_constraints
|
|
42
|
+
@check_constraints ||= model.connection.check_constraints(model.table_name)
|
|
43
|
+
rescue StandardError
|
|
44
|
+
[]
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def constraint_columns(expression)
|
|
48
|
+
expression_sql = expression.to_s
|
|
49
|
+
# Captures identifiers in three forms:
|
|
50
|
+
# 1) "double quoted", 2) `backtick quoted`, 3) plain SQL identifiers.
|
|
51
|
+
# Plain identifiers followed by `(` are excluded to skip SQL function names.
|
|
52
|
+
quoted_identifiers = expression_sql.scan(/"([^"]+)"|`([^`]+)`/).flatten.compact
|
|
53
|
+
# Only the column part is needed for comparison, so `table.column` is
|
|
54
|
+
# reduced to `column`.
|
|
55
|
+
# `(?!\s*\()` excludes function names such as `ABS(`.
|
|
56
|
+
plain_identifiers = expression_sql.scan(PLAIN_IDENTIFIER_PATTERN)
|
|
57
|
+
.flatten
|
|
58
|
+
.map { |identifier| identifier.split('.').last }
|
|
59
|
+
|
|
60
|
+
(quoted_identifiers + plain_identifiers).map(&:downcase).reject do |identifier|
|
|
61
|
+
SQL_KEYWORDS.include?(identifier)
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def column
|
|
66
|
+
@column ||= model.columns.find { |field| field.name == attribute.to_s }
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module DatabaseConsistency
|
|
4
|
+
module Writers
|
|
5
|
+
module Simple
|
|
6
|
+
class NumericalityCheckConstraintMissing < Base # :nodoc:
|
|
7
|
+
private
|
|
8
|
+
|
|
9
|
+
def template
|
|
10
|
+
'column has a numericality validator but does not have a CHECK constraint'
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def unique_attributes
|
|
14
|
+
{
|
|
15
|
+
table_or_model_name: report.table_or_model_name,
|
|
16
|
+
column_or_attribute_name: report.column_or_attribute_name
|
|
17
|
+
}
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
data/lib/database_consistency.rb
CHANGED
|
@@ -36,6 +36,7 @@ require 'database_consistency/writers/simple/missing_uniqueness_validation'
|
|
|
36
36
|
require 'database_consistency/writers/simple/null_constraint_misses_validator'
|
|
37
37
|
require 'database_consistency/writers/simple/null_constraint_missing'
|
|
38
38
|
require 'database_consistency/writers/simple/possible_null'
|
|
39
|
+
require 'database_consistency/writers/simple/numericality_check_constraint_missing'
|
|
39
40
|
require 'database_consistency/writers/simple/small_primary_key'
|
|
40
41
|
require 'database_consistency/writers/simple/inconsistent_enum_type'
|
|
41
42
|
require 'database_consistency/writers/simple/enum_values_inconsistent_with_ar_enum'
|
|
@@ -109,6 +110,7 @@ require 'database_consistency/checkers/validator_checkers/case_sensitive_unique_
|
|
|
109
110
|
|
|
110
111
|
require 'database_consistency/checkers/validators_fraction_checkers/validators_fraction_checker'
|
|
111
112
|
require 'database_consistency/checkers/validators_fraction_checkers/column_presence_checker'
|
|
113
|
+
require 'database_consistency/checkers/validators_fraction_checkers/numericality_constraint_checker'
|
|
112
114
|
|
|
113
115
|
require 'database_consistency/checkers/index_checkers/index_checker'
|
|
114
116
|
require 'database_consistency/checkers/index_checkers/unique_index_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.7
|
|
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-07-
|
|
11
|
+
date: 2026-07-26 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: activerecord
|
|
@@ -176,6 +176,7 @@ files:
|
|
|
176
176
|
- lib/database_consistency/checkers/validator_checkers/missing_unique_index_checker.rb
|
|
177
177
|
- lib/database_consistency/checkers/validator_checkers/validator_checker.rb
|
|
178
178
|
- lib/database_consistency/checkers/validators_fraction_checkers/column_presence_checker.rb
|
|
179
|
+
- lib/database_consistency/checkers/validators_fraction_checkers/numericality_constraint_checker.rb
|
|
179
180
|
- lib/database_consistency/checkers/validators_fraction_checkers/validators_fraction_checker.rb
|
|
180
181
|
- lib/database_consistency/configuration.rb
|
|
181
182
|
- lib/database_consistency/databases/factory.rb
|
|
@@ -241,6 +242,7 @@ files:
|
|
|
241
242
|
- lib/database_consistency/writers/simple/null_constraint_association_misses_validator.rb
|
|
242
243
|
- lib/database_consistency/writers/simple/null_constraint_misses_validator.rb
|
|
243
244
|
- lib/database_consistency/writers/simple/null_constraint_missing.rb
|
|
245
|
+
- lib/database_consistency/writers/simple/numericality_check_constraint_missing.rb
|
|
244
246
|
- lib/database_consistency/writers/simple/polymorphic_association_nullability_mismatch.rb
|
|
245
247
|
- lib/database_consistency/writers/simple/possible_null.rb
|
|
246
248
|
- lib/database_consistency/writers/simple/redundant_case_insensitive_option.rb
|