generated_schema_validations 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e8a2504a7047ebe796e668a73b2920b0113e0c38b9c514a5f652f8e524f2696b
4
- data.tar.gz: 42f4ad881435d17cbad568bf4a704bdb04a3c8ab1d713752ae9401b4f19d0931
3
+ metadata.gz: 2d7371cf78c33a1ff16867c837724f1dc51bb7145a83b8d6cf90a8f160382fb3
4
+ data.tar.gz: 7544fd35cf7dd4daef98a4223c2208bca9a4c74482bbc503290122e216bada3c
5
5
  SHA512:
6
- metadata.gz: 730fb6c7de51578b874f0e22d7b02089384849a6fa22252ed105b4b3c74a3ed2bbd9c1ba2f4ed03a2f336a28c49cd5c3e784807375a317434d28b239d613d255
7
- data.tar.gz: 9e19c517659c728540736cef20a72944cb9ac568268e047888402fd6833c2281e94733b24d858dbb26ffff738eb8138f31fcb29146c0271b80c76623f4cacef9
6
+ metadata.gz: 752b409572e541277f7c4313e1c4168223eb740cd4c5a5cdbef4245d1b66953f9f8c34303f25dba4754f00d198d78b59dd2fc1b2704d8d968a1b877cdcbcd295
7
+ data.tar.gz: 6495630671eb4abb5e8a2f439f0780a27251b1538b092694593ffecba75a5183bd3934ef65615e4b087098eb311f1fce36e339bb920b601a2d5d235a9dbbd12e
data/README.md CHANGED
@@ -95,6 +95,15 @@ 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
+
103
+ ### 0.2.3
104
+
105
+ * Add `jsonb` and `xml` as possible field type
106
+
98
107
  ### 0.2.2
99
108
 
100
109
  * Enable rails 7.0 usage
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = 'generated_schema_validations'
5
- spec.version = '0.2.2'
5
+ spec.version = '0.3.0'
6
6
  spec.authors = ['Georg Limbach']
7
7
  spec.email = ['georg.limbach@lichtbit.com']
8
8
 
@@ -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
@@ -100,6 +102,14 @@ class GeneratedSchemaValidations::Table
100
102
  null_validation(:json, name, column_options)
101
103
  end
102
104
 
105
+ def jsonb(name, column_options = {})
106
+ null_validation(:jsonb, name, column_options)
107
+ end
108
+
109
+ def xml(name, column_options = {})
110
+ null_validation(:xml, name, column_options)
111
+ end
112
+
103
113
  def decimal(name, column_options = {})
104
114
  null_validation(:decimal, name, column_options)
105
115
  return if column_options[:array]
@@ -129,6 +139,10 @@ class GeneratedSchemaValidations::Table
129
139
  return unless index_options[:unique]
130
140
  return unless names.all? { |name| name.to_s.in?(@column_names) }
131
141
 
132
- @unique_indexes.push(names.map(&:to_s))
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
133
147
  end
134
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.schema_validations_excluded_columns += exclude
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.2.2
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: 2022-10-21 00:00:00.000000000 Z
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.