database_validations 0.9.3 → 0.9.4

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: 37040902b8f9dd94cbfcc1785129eff2ac2f75eb085f567299852a03f4a83cfa
4
- data.tar.gz: c6a2a3331225b8344c23244692296a982ed25405be15650c32bc0815e227c20a
3
+ metadata.gz: 5cea34b87885accbde526d9a36bcfcda30b0ae23dd474ee9d3ccbd19b729cdb5
4
+ data.tar.gz: bf86e7012afed03710c09ab205388d08a6c0c3b34a8cce77423fca40916ae03e
5
5
  SHA512:
6
- metadata.gz: 27f9f74a6f081f343bbe94da77956cd0c06419dcf321d2576a6238f0b2bddb5dd967861deaffe88ab47929e4b3837825fc9c17bf95b4fba83f5fbfd4b07d04e0
7
- data.tar.gz: 86f379332348da451c1f2d02e3e4096e528f9e00c25f10fdb8881512d97c10ffdf9ab5cffdc3f4e9ce6ea475e0d3d0a0d75b728fed700f7606ccf07cacfe9bdf
6
+ metadata.gz: 1ed0995301de6de0e5169d0e5b3e333ce9b0417014e423094aa5293055dec0ee8657f1a8661d466f8ccdce64020e33378c4c4f4623da5fa5b6755408e0253ff4
7
+ data.tar.gz: 3d08f422bff52c0e3942dbdb60ab867c891bd1802f12f90fd97305a66bc5bdf01158aa5ba94c418fab6e8ed88db41cc05cacaee47a7ddad95d77dffdfd6c799e
@@ -9,18 +9,25 @@ module DatabaseValidations
9
9
  end
10
10
 
11
11
  # @param [String] index_name
12
- def find_index_by_name(index_name)
13
- indexes.find { |index| index.name == index_name }
12
+ def find_unique_index_by_name(index_name)
13
+ unique_indexes.find { |index| index.name == index_name }
14
14
  end
15
15
 
16
16
  # @param [Array<String>] columns
17
17
  # @param [String] where
18
- def find_index(columns, where)
19
- indexes.find { |index| Array.wrap(index.columns).map(&:to_s).sort == columns && index.where == where }
18
+ def find_unique_index(columns, where)
19
+ unique_indexes.find { |index| Array.wrap(index.columns).map(&:to_s).sort == columns && index.where == where }
20
20
  end
21
21
 
22
- def indexes
23
- model.connection.indexes(model.table_name).select(&:unique)
22
+ def unique_indexes
23
+ connection = model.connection
24
+
25
+ if connection.schema_cache.respond_to?(:indexes)
26
+ # Rails 6 only
27
+ connection.schema_cache.indexes(model.table_name).select(&:unique)
28
+ else
29
+ connection.indexes(model.table_name).select(&:unique)
30
+ end
24
31
  end
25
32
 
26
33
  def foreign_keys
@@ -38,8 +38,8 @@ module DatabaseValidations
38
38
 
39
39
  validator.attributes.map do |attribute|
40
40
  columns = KeyGenerator.unify_columns(attribute, validator.options[:scope])
41
- index = validator.index_name ? adapter.find_index_by_name(validator.index_name.to_s) : adapter.find_index(columns, validator.where) # rubocop:disable Metrics/LineLength
42
- raise Errors::IndexNotFound.new(columns, validator.where, validator.index_name, adapter.indexes, adapter.table_name) unless index && valid_index?(columns, index) # rubocop:disable Metrics/LineLength
41
+ index = validator.index_name ? adapter.find_unique_index_by_name(validator.index_name.to_s) : adapter.find_unique_index(columns, validator.where) # rubocop:disable Metrics/LineLength
42
+ raise Errors::IndexNotFound.new(columns, validator.where, validator.index_name, adapter.unique_indexes, adapter.table_name) unless index && valid_index?(columns, index) # rubocop:disable Metrics/LineLength
43
43
  end
44
44
  end
45
45
  end
@@ -22,7 +22,7 @@ module DatabaseValidations
22
22
  else
23
23
  validator.attributes.map do |attribute|
24
24
  columns = KeyGenerator.unify_columns(attribute, validator.options[:scope])
25
- index = adapter.find_index(columns, validator.where)
25
+ index = adapter.find_unique_index(columns, validator.where)
26
26
  [KeyGenerator.for_unique_index(index.name), attribute]
27
27
  end.to_h
28
28
  end
@@ -14,16 +14,26 @@ module DatabaseValidations
14
14
  end
15
15
 
16
16
  def create_or_update(*args, &block)
17
+ options = args.extract_options!
18
+
19
+ if options[:validate] == false
20
+ super
21
+ else
22
+ rescue_from_database_exceptions { super }
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def rescue_from_database_exceptions(&block)
17
29
  self._database_validations_fallback = false
18
- ActiveRecord::Base.connection.transaction(requires_new: true) { super }
30
+ self.class.connection.transaction(requires_new: true, &block)
19
31
  rescue ActiveRecord::InvalidForeignKey, ActiveRecord::RecordNotUnique => e
20
32
  raise e unless Rescuer.handled?(self, e)
21
33
 
22
34
  raise ActiveRecord::RecordInvalid, self
23
35
  end
24
36
 
25
- private
26
-
27
37
  def perform_validations(options = {})
28
38
  options[:validate] == false || valid_without_database_validations?(options[:context])
29
39
  end
@@ -1,3 +1,3 @@
1
1
  module DatabaseValidations
2
- VERSION = '0.9.3'.freeze
2
+ VERSION = '0.9.4'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: database_validations
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.3
4
+ version: 0.9.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evgeniy Demin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-09-24 00:00:00.000000000 Z
11
+ date: 2020-09-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord