database_consistency 1.3.1 → 1.3.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9c6abf040aee5da4569c129d448c0b9dcc2344ace31f64ffad808f1f225f7328
4
- data.tar.gz: ccc59e66ac21cee7e6f71cf1d994dc65c09c5706343396af90af7a4b157a6ac8
3
+ metadata.gz: 857156332ffa36d32216953b8bd1f798ae27dcb2037e873c93d08fa55a01bd47
4
+ data.tar.gz: f1aa19a34377de90e606b1e8272d9444992e44f6487c0ac28d913298d5ef6df9
5
5
  SHA512:
6
- metadata.gz: 1617cef7111b2d5e3cca1f003176b1477b27529c1b433dc04a6131c1efff1b2ba8c1291637cd9fa4e169736c10807fa21b12f4c48197175596226d73814e64eb
7
- data.tar.gz: 87aa6bf2daaf34aec66256af9b999cc20e09587a71441b151e88a0616fba0c8c907e34ea4497b89c432538d8d0c59b78d27c8dda4f5222f3ca5ec6e759276951
6
+ metadata.gz: 635ac9cfa1426f77ec59647e7282887fcfc81ec2db8217a545f791383bc1e3538f8f2e5bcb55c1aedee194b27287063a0ee330ffafe0068a7c219f2350a421b9
7
+ data.tar.gz: f72cf3ef8ac71296984002d798b895740fc15ec91e582503fde012d8b58986956e36bd41bfae5453abb374da27a20f92bbcf96fbfe2abdddf3fe8f2569be3f48
@@ -5,15 +5,16 @@ module DatabaseConsistency
5
5
  # This class checks redundant database indexes
6
6
  class RedundantIndexChecker < IndexChecker
7
7
  class Report < DatabaseConsistency::Report # :nodoc:
8
- attr_reader :index_name
8
+ attr_reader :covered_index_name, :index_name
9
9
 
10
- def initialize(index_name:, **args)
10
+ def initialize(covered_index_name:, index_name:, **args)
11
11
  super(**args)
12
+ @covered_index_name = covered_index_name
12
13
  @index_name = index_name
13
14
  end
14
15
 
15
16
  def attributes
16
- super.merge(index_name: index_name)
17
+ super.merge(covered_index_name: covered_index_name, index_name: index_name)
17
18
  end
18
19
  end
19
20
 
@@ -31,13 +32,14 @@ module DatabaseConsistency
31
32
  # | provided | ok |
32
33
  # | redundant | fail |
33
34
  #
34
- def check
35
+ def check # rubocop:disable Metrics/MethodLength
35
36
  if covered_by_index
36
37
  Report.new(
37
38
  status: :fail,
38
39
  error_slug: :redundant_index,
39
40
  error_message: nil,
40
- index_name: covered_by_index.name,
41
+ covered_index_name: covered_by_index.name,
42
+ index_name: index.name,
41
43
  **report_attributes
42
44
  )
43
45
  else
@@ -78,17 +78,23 @@ module DatabaseConsistency
78
78
  end
79
79
 
80
80
  def uniqueness_validator_columns(attribute, validator, model)
81
- ([wrapped_attribute_name(attribute, validator)] + scope_columns(validator, model)).map(&:to_s)
81
+ ([wrapped_attribute_name(attribute, validator, model)] + scope_columns(validator, model)).map(&:to_s)
82
82
  end
83
83
 
84
84
  def scope_columns(validator, model)
85
85
  Array.wrap(validator.options[:scope]).map do |scope_item|
86
- model._reflect_on_association(scope_item)&.foreign_key || scope_item
86
+ foreign_key_or_attribute(model, scope_item)
87
87
  end
88
88
  end
89
89
 
90
+ def foreign_key_or_attribute(model, attribute)
91
+ model._reflect_on_association(attribute)&.foreign_key || attribute
92
+ end
93
+
90
94
  # @return [String]
91
- def wrapped_attribute_name(attribute, validator)
95
+ def wrapped_attribute_name(attribute, validator, model)
96
+ attribute = foreign_key_or_attribute(model, attribute)
97
+
92
98
  if validator.options[:case_sensitive].nil? || validator.options[:case_sensitive]
93
99
  attribute
94
100
  else
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DatabaseConsistency
4
- VERSION = '1.3.1'
4
+ VERSION = '1.3.3'
5
5
  end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DatabaseConsistency
4
+ module Writers
5
+ module Autofix
6
+ class RedundantIndex < MigrationBase # :nodoc:
7
+ private
8
+
9
+ def migration_name
10
+ "remove_#{report.index_name}_index"
11
+ end
12
+
13
+ def template_path
14
+ File.join(__dir__, 'templates', 'redundant_index.tt')
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,5 @@
1
+ class %<migration_name>s < ActiveRecord::Migration[%<migration_version>s]
2
+ def change
3
+ remove_index nil, name: '%<index_name>s'
4
+ end
5
+ end
@@ -8,7 +8,8 @@ module DatabaseConsistency
8
8
  SLUG_TO_GENERATOR = {
9
9
  missing_foreign_key: Autofix::MissingForeignKey,
10
10
  null_constraint_missing: Autofix::NullConstraintMissing,
11
- association_missing_null_constraint: Autofix::NullConstraintMissing
11
+ association_missing_null_constraint: Autofix::NullConstraintMissing,
12
+ redundant_index: Autofix::RedundantIndex
12
13
  }.freeze
13
14
 
14
15
  def write
@@ -29,7 +29,7 @@ module DatabaseConsistency
29
29
  null_constraint_association_misses_validator: 'column is required in the database but do not have presence validator for association %<association_name>s', # rubocop:disable Layout/LineLength
30
30
  null_constraint_misses_validator: 'column is required in the database but do not have presence validator',
31
31
  small_primary_key: 'column has int/serial type but recommended to have bigint/bigserial/uuid',
32
- redundant_index: 'index is redundant as %<index_name>s covers it',
32
+ redundant_index: 'index is redundant as %<covered_index_name>s covers it',
33
33
  redundant_unique_index: 'index uniqueness is redundant as %<index_name>s covers it',
34
34
  missing_uniqueness_validation: 'index is unique in the database but do not have uniqueness validator',
35
35
  missing_unique_index: 'model should have proper unique index in the database',
@@ -19,6 +19,7 @@ require 'database_consistency/writers/autofix/helpers/migration'
19
19
  require 'database_consistency/writers/autofix/base'
20
20
  require 'database_consistency/writers/autofix/migration_base'
21
21
  require 'database_consistency/writers/autofix/missing_foreign_key'
22
+ require 'database_consistency/writers/autofix/redundant_index'
22
23
  require 'database_consistency/writers/autofix/null_constraint_missing'
23
24
  require 'database_consistency/writers/autofix_writer'
24
25
 
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.3.1
4
+ version: 1.3.3
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-11-07 00:00:00.000000000 Z
11
+ date: 2022-11-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -184,8 +184,10 @@ files:
184
184
  - lib/database_consistency/writers/autofix/migration_base.rb
185
185
  - lib/database_consistency/writers/autofix/missing_foreign_key.rb
186
186
  - lib/database_consistency/writers/autofix/null_constraint_missing.rb
187
+ - lib/database_consistency/writers/autofix/redundant_index.rb
187
188
  - lib/database_consistency/writers/autofix/templates/missing_foreign_key.tt
188
189
  - lib/database_consistency/writers/autofix/templates/null_constraint_missing.tt
190
+ - lib/database_consistency/writers/autofix/templates/redundant_index.tt
189
191
  - lib/database_consistency/writers/autofix_writer.rb
190
192
  - lib/database_consistency/writers/base_writer.rb
191
193
  - lib/database_consistency/writers/helpers/pipes.rb