database_consistency 1.0.0 → 1.1.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: b074df0ae4a1316afdb9874c811a02189fd664e00b85220730dd0ae985a36c76
4
- data.tar.gz: f870f9f74117a6ea1b5592e7610becc5f854c375ee4a82f9720b287ae9288462
3
+ metadata.gz: f37408420931f459ea476d095f441e40256ea2279e07c21ad9d477c0781d5944
4
+ data.tar.gz: b054877a7819333fab57dbe22786456d4a0328b462f9808561e470290b3ae6e7
5
5
  SHA512:
6
- metadata.gz: 99e298ec2e58c3bfcc5c10a9e294891b14eee23b1a4c9ccbf3e78f5e500480baf1c3ec397d0a19f5af6e83f05dbe9a297e062358eab9699d324b6b06a1255889
7
- data.tar.gz: 7d742b1d5985792e2fa3b3a6d5195ccb91c4b49cdbcbce800de6db29a16ef1045f4bf786b3d88e981d23b573282d4ddb821ede90951890ab45682c16b37ede7b
6
+ metadata.gz: c2e7a18dabde150f3f1f2ee144b6076e2c0fb944c9c128851379e86007f0aed056f1fb550cc19319b777767ffc165534939a680e248213473032054dfa8e2924
7
+ data.tar.gz: f047ff2a3e5e41137a93f5c64e4a0288e74b0d687670e7eabe5b3656a675abf87df75586fd5a0988f25f26f993d20542addfd2d84b430dac7dd30025f2016415
@@ -36,6 +36,7 @@ require 'database_consistency/checkers/validators_fraction_checkers/column_prese
36
36
  require 'database_consistency/checkers/index_checkers/index_checker'
37
37
  require 'database_consistency/checkers/index_checkers/unique_index_checker'
38
38
  require 'database_consistency/checkers/index_checkers/redundant_index_checker'
39
+ require 'database_consistency/checkers/index_checkers/redundant_unique_index_checker'
39
40
 
40
41
  require 'database_consistency/processors/base_processor'
41
42
  require 'database_consistency/processors/associations_processor'
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DatabaseConsistency
4
+ module Checkers
5
+ # This class checks redundant database indexes
6
+ class RedundantUniqueIndexChecker < IndexChecker
7
+ # Message templates
8
+ REDUNDANT_UNIQUE_INDEX = 'index uniqueness is redundant as (%index) covers it'
9
+
10
+ private
11
+
12
+ # We skip check when:
13
+ # - index is not unique
14
+ def preconditions
15
+ index.unique
16
+ end
17
+
18
+ # Table of possible statuses
19
+ # | validation | status |
20
+ # | ---------- | ------ |
21
+ # | provided | ok |
22
+ # | redundant | fail |
23
+ #
24
+ def check
25
+ if covered_by_index
26
+ report_template(:fail, render_message)
27
+ else
28
+ report_template(:ok)
29
+ end
30
+ end
31
+
32
+ def render_message
33
+ REDUNDANT_UNIQUE_INDEX.sub('%index', covered_by_index.name)
34
+ end
35
+
36
+ def covered_by_index
37
+ @covered_by_index ||=
38
+ model.connection.indexes(model.table_name).find do |another_index|
39
+ next if index.name == another_index.name
40
+
41
+ another_index.unique && contain_index?(another_index)
42
+ end
43
+ end
44
+
45
+ def contain_index?(another_index)
46
+ another_index_columns = Helper.extract_index_columns(another_index.columns)
47
+ index_columns & another_index_columns == another_index_columns
48
+ end
49
+
50
+ def index_columns
51
+ @index_columns ||= Helper.extract_index_columns(index.columns)
52
+ end
53
+ end
54
+ end
55
+ end
@@ -6,7 +6,8 @@ module DatabaseConsistency
6
6
  class IndexesProcessor < BaseProcessor
7
7
  CHECKERS = [
8
8
  Checkers::UniqueIndexChecker,
9
- Checkers::RedundantIndexChecker
9
+ Checkers::RedundantIndexChecker,
10
+ Checkers::RedundantUniqueIndexChecker
10
11
  ].freeze
11
12
 
12
13
  private
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DatabaseConsistency
4
- VERSION = '1.0.0'
4
+ VERSION = '1.1.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: database_consistency
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evgeniy Demin
@@ -142,6 +142,7 @@ files:
142
142
  - lib/database_consistency/checkers/column_checkers/primary_key_type_checker.rb
143
143
  - lib/database_consistency/checkers/index_checkers/index_checker.rb
144
144
  - lib/database_consistency/checkers/index_checkers/redundant_index_checker.rb
145
+ - lib/database_consistency/checkers/index_checkers/redundant_unique_index_checker.rb
145
146
  - lib/database_consistency/checkers/index_checkers/unique_index_checker.rb
146
147
  - lib/database_consistency/checkers/validator_checkers/belongs_to_presence_checker.rb
147
148
  - lib/database_consistency/checkers/validator_checkers/missing_unique_index_checker.rb