database_consistency 1.1.12 → 1.1.14

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: 908211c725197dadbe72f2f7c8f2f47d48a55ed546c57d2f512b9b2978fb325a
4
- data.tar.gz: 4b18e433573c598b25906dce98554516f3c54bacbd897b59c8f72c6d9577a53d
3
+ metadata.gz: 86037a5780e8f5f906a5232a9925b5e250177b6ebe653a81509167814dc87a2e
4
+ data.tar.gz: 91cafa728a48dd765a2b66fc41c9a2a4f2952d72737472c6f1f88af28d9053b8
5
5
  SHA512:
6
- metadata.gz: 43ea449cc58c970f99deb81936461d2f267a107934ad93cfb1f73ef0e0a8e46c97d9ba5085a54538828d162ca06638ad35afb907fa51de661df30bad9527cefc
7
- data.tar.gz: 799f1975507dababf597ca140655377e90a1480af4422080349027b03ef7ef9dbade4125e95f317ef3d873706e82f1c24991c9c9600a08cb3dca3e6ff2bbc8e0
6
+ metadata.gz: d6ac7dbf5de3cc1cbfaa7aef08e86aad7c88b2e5734283ef130ab60582945312faf87828b949d6f2f2e551b2770faca8498ad70e6438bcc6416977df20441aa3
7
+ data.tar.gz: f31f3a0ad600d45e7fcfe6f9d9aad07a267dfc96de990e03f7e725e9644f09d95ce8b6f0c69de920676e7023a5cf2dc7058dce2506f0ead2f23ee543b2596e8f
@@ -6,6 +6,7 @@ module DatabaseConsistency
6
6
  class MissingIndexChecker < AssociationChecker
7
7
  # Message templates
8
8
  MISSING_INDEX = 'associated model should have proper index in the database'
9
+ MISSING_UNIQUE_INDEX = 'associated model should have proper unique index in the database'
9
10
 
10
11
  private
11
12
 
@@ -28,6 +29,22 @@ module DatabaseConsistency
28
29
  # | persisted | ok |
29
30
  # | missing | fail |
30
31
  def check
32
+ if unique_has_one_association?
33
+ check_unique_has_one
34
+ else
35
+ check_remaining
36
+ end
37
+ end
38
+
39
+ def check_unique_has_one
40
+ if unique_index
41
+ report_template(:ok)
42
+ else
43
+ report_template(:fail, MISSING_UNIQUE_INDEX)
44
+ end
45
+ end
46
+
47
+ def check_remaining
31
48
  if index
32
49
  report_template(:ok)
33
50
  else
@@ -35,9 +52,19 @@ module DatabaseConsistency
35
52
  end
36
53
  end
37
54
 
55
+ def unique_has_one_association?
56
+ association.scope.nil? && association.macro == :has_one && !association.options[:as].present?
57
+ end
58
+
59
+ def unique_index
60
+ @unique_index ||= association.klass.connection.indexes(association.klass.table_name).find do |index|
61
+ index_keys(index) == association_keys && index.unique
62
+ end
63
+ end
64
+
38
65
  def index
39
66
  @index ||= association.klass.connection.indexes(association.klass.table_name).find do |index|
40
- index_keys(index) == association_keys
67
+ index_keys(index, limit: association_keys.size) == association_keys
41
68
  end
42
69
  end
43
70
 
@@ -45,10 +72,16 @@ module DatabaseConsistency
45
72
  @association_keys ||= [association.foreign_key, association.type].compact.map(&:to_s).sort
46
73
  end
47
74
 
48
- def index_keys(index)
75
+ def index_keys(index, limit: nil)
49
76
  return unless index.columns.is_a?(Array)
50
77
 
51
- index.columns[0...association_keys.size].sort
78
+ columns = index.columns
79
+
80
+ if limit
81
+ columns.first(limit).sort
82
+ else
83
+ columns
84
+ end
52
85
  end
53
86
  end
54
87
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DatabaseConsistency
4
- VERSION = '1.1.12'
4
+ VERSION = '1.1.14'
5
5
  end
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: 1.1.12
4
+ version: 1.1.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evgeniy Demin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-01-23 00:00:00.000000000 Z
11
+ date: 2022-04-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -199,7 +199,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
199
199
  - !ruby/object:Gem::Version
200
200
  version: '0'
201
201
  requirements: []
202
- rubygems_version: 3.2.22
202
+ rubygems_version: 3.0.3.1
203
203
  signing_key:
204
204
  specification_version: 4
205
205
  summary: Provide an easy way to check the consistency of the database constraints