database_validations 0.9.3 → 0.9.4
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_validations/lib/adapters/base_adapter.rb +13 -6
- data/lib/database_validations/lib/checkers/db_uniqueness_validator.rb +2 -2
- data/lib/database_validations/lib/uniqueness_key_extractor.rb +1 -1
- data/lib/database_validations/lib/validations.rb +13 -3
- data/lib/database_validations/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5cea34b87885accbde526d9a36bcfcda30b0ae23dd474ee9d3ccbd19b729cdb5
|
4
|
+
data.tar.gz: bf86e7012afed03710c09ab205388d08a6c0c3b34a8cce77423fca40916ae03e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
13
|
-
|
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
|
19
|
-
|
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
|
23
|
-
model.connection
|
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.
|
42
|
-
raise Errors::IndexNotFound.new(columns, validator.where, validator.index_name, adapter.
|
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.
|
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
|
-
|
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
|
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.
|
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-
|
11
|
+
date: 2020-09-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|