database_consistency 0.8.13 → 1.1.2
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_consistency.rb +2 -0
- data/lib/database_consistency/checkers/index_checkers/redundant_index_checker.rb +59 -0
- data/lib/database_consistency/checkers/index_checkers/redundant_unique_index_checker.rb +59 -0
- data/lib/database_consistency/processors/indexes_processor.rb +3 -1
- data/lib/database_consistency/version.rb +1 -1
- data/lib/database_consistency/writers/simple_writer.rb +1 -3
- metadata +8 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dc9590f6144cef94d2046971c259258a8428cddd9b7083f7225a04d3c5c19933
|
4
|
+
data.tar.gz: 6a272364d20b0fdfb9be5155dc0e56c3becc4fc98ef0dc777537ae5a5f1c5f0f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dcb1f1eae53a0d783c6e83a6f3ca77ae4f5ab4c3086291b268e0e3745abce10d760214c313687db98086ccee9322fe88cbcaade5def8290dea17939447534868
|
7
|
+
data.tar.gz: 90fcc238b355768e1f2e7b11909b7eb5f2d679b724be2ef3264a158fcdeb916aa751b97e6e14ecfae39f5de985e83887f862565e86c716bb3e43391a463b3a8a
|
data/lib/database_consistency.rb
CHANGED
@@ -35,6 +35,8 @@ require 'database_consistency/checkers/validators_fraction_checkers/column_prese
|
|
35
35
|
|
36
36
|
require 'database_consistency/checkers/index_checkers/index_checker'
|
37
37
|
require 'database_consistency/checkers/index_checkers/unique_index_checker'
|
38
|
+
require 'database_consistency/checkers/index_checkers/redundant_index_checker'
|
39
|
+
require 'database_consistency/checkers/index_checkers/redundant_unique_index_checker'
|
38
40
|
|
39
41
|
require 'database_consistency/processors/base_processor'
|
40
42
|
require 'database_consistency/processors/associations_processor'
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DatabaseConsistency
|
4
|
+
module Checkers
|
5
|
+
# This class checks redundant database indexes
|
6
|
+
class RedundantIndexChecker < IndexChecker
|
7
|
+
# Message templates
|
8
|
+
REDUNDANT_INDEX = 'index is redundant as (%index) covers it'
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
# We skip check when:
|
13
|
+
# - index is 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_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
|
+
clause_equals?(another_index) && include_index_as_prefix?(another_index)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def clause_equals?(another_index)
|
46
|
+
another_index.where == index.where
|
47
|
+
end
|
48
|
+
|
49
|
+
def include_index_as_prefix?(another_index)
|
50
|
+
another_index_columns = Helper.extract_index_columns(another_index.columns)
|
51
|
+
index_columns == another_index_columns.first(index_columns.size)
|
52
|
+
end
|
53
|
+
|
54
|
+
def index_columns
|
55
|
+
@index_columns ||= Helper.extract_index_columns(index.columns)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,59 @@
|
|
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 && clause_equals?(another_index) && contain_index?(another_index)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def clause_equals?(another_index)
|
46
|
+
another_index.where == index.where
|
47
|
+
end
|
48
|
+
|
49
|
+
def contain_index?(another_index)
|
50
|
+
another_index_columns = Helper.extract_index_columns(another_index.columns)
|
51
|
+
index_columns & another_index_columns == another_index_columns
|
52
|
+
end
|
53
|
+
|
54
|
+
def index_columns
|
55
|
+
@index_columns ||= Helper.extract_index_columns(index.columns)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -5,7 +5,9 @@ module DatabaseConsistency
|
|
5
5
|
# The class to process indexes
|
6
6
|
class IndexesProcessor < BaseProcessor
|
7
7
|
CHECKERS = [
|
8
|
-
Checkers::UniqueIndexChecker
|
8
|
+
Checkers::UniqueIndexChecker,
|
9
|
+
Checkers::RedundantIndexChecker,
|
10
|
+
Checkers::RedundantUniqueIndexChecker
|
9
11
|
].freeze
|
10
12
|
|
11
13
|
private
|
@@ -21,9 +21,7 @@ module DatabaseConsistency
|
|
21
21
|
end
|
22
22
|
|
23
23
|
def msg(result)
|
24
|
-
|
25
|
-
msg += " (checker: #{result.checker_name})" if config.debug?
|
26
|
-
msg
|
24
|
+
"#{result.checker_name} #{status_text(result)} #{key_text(result)} #{result.message}"
|
27
25
|
end
|
28
26
|
|
29
27
|
private
|
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:
|
4
|
+
version: 1.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Evgeniy Demin
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-08-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -122,7 +122,7 @@ dependencies:
|
|
122
122
|
- - "~>"
|
123
123
|
- !ruby/object:Gem::Version
|
124
124
|
version: '1.3'
|
125
|
-
description:
|
125
|
+
description:
|
126
126
|
email:
|
127
127
|
- lawliet.djez@gmail.com
|
128
128
|
executables:
|
@@ -141,6 +141,8 @@ files:
|
|
141
141
|
- lib/database_consistency/checkers/column_checkers/null_constraint_checker.rb
|
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
|
+
- lib/database_consistency/checkers/index_checkers/redundant_index_checker.rb
|
145
|
+
- lib/database_consistency/checkers/index_checkers/redundant_unique_index_checker.rb
|
144
146
|
- lib/database_consistency/checkers/index_checkers/unique_index_checker.rb
|
145
147
|
- lib/database_consistency/checkers/validator_checkers/belongs_to_presence_checker.rb
|
146
148
|
- lib/database_consistency/checkers/validator_checkers/missing_unique_index_checker.rb
|
@@ -168,7 +170,7 @@ homepage: https://github.com/djezzzl/database_consistency
|
|
168
170
|
licenses:
|
169
171
|
- MIT
|
170
172
|
metadata: {}
|
171
|
-
post_install_message:
|
173
|
+
post_install_message:
|
172
174
|
rdoc_options: []
|
173
175
|
require_paths:
|
174
176
|
- lib
|
@@ -184,7 +186,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
184
186
|
version: '0'
|
185
187
|
requirements: []
|
186
188
|
rubygems_version: 3.0.8
|
187
|
-
signing_key:
|
189
|
+
signing_key:
|
188
190
|
specification_version: 4
|
189
191
|
summary: Provide an easy way to check the consistency of the database constraints
|
190
192
|
with the application validations.
|