generated_schema_validations 0.2.3 → 0.3.0
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: 2d7371cf78c33a1ff16867c837724f1dc51bb7145a83b8d6cf90a8f160382fb3
|
4
|
+
data.tar.gz: 7544fd35cf7dd4daef98a4223c2208bca9a4c74482bbc503290122e216bada3c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 752b409572e541277f7c4313e1c4168223eb740cd4c5a5cdbef4245d1b66953f9f8c34303f25dba4754f00d198d78b59dd2fc1b2704d8d968a1b877cdcbcd295
|
7
|
+
data.tar.gz: 6495630671eb4abb5e8a2f439f0780a27251b1538b092694593ffecba75a5183bd3934ef65615e4b087098eb311f1fce36e339bb920b601a2d5d235a9dbbd12e
|
data/README.md
CHANGED
@@ -95,6 +95,11 @@ You can watch changes on `schema_validations.rb` to understand the generated val
|
|
95
95
|
|
96
96
|
## Changelog
|
97
97
|
|
98
|
+
### 0.3.0
|
99
|
+
|
100
|
+
* Check usage of schema_validations (see #3)
|
101
|
+
* Force excluding unique validations with where clause (see #2)
|
102
|
+
|
98
103
|
### 0.2.3
|
99
104
|
|
100
105
|
* Add `jsonb` and `xml` as possible field type
|
@@ -13,6 +13,7 @@ class GeneratedSchemaValidations::Table
|
|
13
13
|
@table_name = table_name
|
14
14
|
@column_names = []
|
15
15
|
@possible_belongs_to_not_null_columns = []
|
16
|
+
@bad_indexes = []
|
16
17
|
@unique_indexes = []
|
17
18
|
@validations = []
|
18
19
|
|
@@ -25,6 +26,7 @@ class GeneratedSchemaValidations::Table
|
|
25
26
|
if @possible_belongs_to_not_null_columns.present?
|
26
27
|
string += " belongs_to_presence_validations_for(#{@possible_belongs_to_not_null_columns.inspect})\n"
|
27
28
|
end
|
29
|
+
string += " bad_uniqueness_validations_for(#{@bad_indexes.inspect})\n" if @bad_indexes.present?
|
28
30
|
string += " belongs_to_uniqueness_validations_for(#{@unique_indexes.inspect})\n" if @unique_indexes.present?
|
29
31
|
string += " uniqueness_validations_for(#{@unique_indexes.inspect})\n" if @unique_indexes.present?
|
30
32
|
string += @validations.uniq.map { |v| " #{v}\n" }.join
|
@@ -137,6 +139,10 @@ class GeneratedSchemaValidations::Table
|
|
137
139
|
return unless index_options[:unique]
|
138
140
|
return unless names.all? { |name| name.to_s.in?(@column_names) }
|
139
141
|
|
140
|
-
|
142
|
+
if defined?(Rails::Railtie) && (Rails.env.development? || Rails.env.test?) && index_options[:where]
|
143
|
+
@bad_indexes.push(names.map(&:to_s))
|
144
|
+
else
|
145
|
+
@unique_indexes.push(names.map(&:to_s))
|
146
|
+
end
|
141
147
|
end
|
142
148
|
end
|
@@ -11,14 +11,29 @@ module SchemaValidations
|
|
11
11
|
|
12
12
|
included do
|
13
13
|
class_attribute :schema_validations_excluded_columns, default: %i[id created_at updated_at type]
|
14
|
+
class_attribute :schema_validations_called, default: false
|
15
|
+
|
16
|
+
if defined?(Rails::Railtie) && (Rails.env.development? || Rails.env.test?)
|
17
|
+
TracePoint.trace(:end) do |t|
|
18
|
+
if t.self.respond_to?(:schema_validations_called) && t.self < ApplicationRecord &&
|
19
|
+
!t.self.schema_validations_called
|
20
|
+
raise "#{t.self}: schema_validations or skip_schema_validations missing"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
14
24
|
end
|
15
25
|
|
16
26
|
class_methods do
|
17
27
|
def schema_validations(exclude: [], schema_table_name: table_name)
|
18
|
-
self.
|
28
|
+
self.schema_validations_called = true
|
29
|
+
self.schema_validations_excluded_columns += exclude.map(&:to_sym)
|
19
30
|
send("dbv_#{schema_table_name}_validations")
|
20
31
|
end
|
21
32
|
|
33
|
+
def skip_schema_validations
|
34
|
+
self.schema_validations_called = true
|
35
|
+
end
|
36
|
+
|
22
37
|
TABLE_VALIDATIONS
|
23
38
|
|
24
39
|
def validates_with_filter(attribute, options)
|
@@ -36,6 +51,17 @@ module SchemaValidations
|
|
36
51
|
end
|
37
52
|
end
|
38
53
|
|
54
|
+
def bad_uniqueness_validations_for(unique_indexes)
|
55
|
+
unique_indexes.each do |names|
|
56
|
+
names.each do |name|
|
57
|
+
next if name.to_sym.in?(schema_validations_excluded_columns)
|
58
|
+
|
59
|
+
raise "Unique index with where clause is outside the scope of this gem.\n\n" \
|
60
|
+
"You can exclude this column: `schema_validations exclude: [:#{name}]`"
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
39
65
|
def belongs_to_uniqueness_validations_for(unique_indexes)
|
40
66
|
reflect_on_all_associations(:belongs_to).each do |association|
|
41
67
|
dbv_uniqueness_validations_for(unique_indexes, foreign_key: association.foreign_key.to_s,
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: generated_schema_validations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Georg Limbach
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-04-20 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: After each migration it generates a file with some validations. Each
|
14
14
|
active record should include this file and can uns generated validations.
|