database_consistency 1.7.5 → 1.7.7

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: c2d7b47374ed7eb17980a23c56cda142a19c6d3e796f70d9c0fbc737da3e7b6d
4
- data.tar.gz: 369df7a14b6f5550b598162177c8915a1615e0df63eed590d74be43eb3145f7d
3
+ metadata.gz: f17a39d98d4045434101b952706e15a54da6c1bc8a46860e7daa56932b9569a8
4
+ data.tar.gz: 73a0950925d6158c7e9be1dfa7e9d2c706bed88590884c7ab9784e3434e044e0
5
5
  SHA512:
6
- metadata.gz: a7b0139cf11940e9ce879fe06d788e15e5c6a7c8f3bcd8a9483267a656e3f14977f7cf1e1f8586ca8ce70482d5a2f325214c457b2c055e5d8a590c0e9ea66031
7
- data.tar.gz: 103e78babcc811497dcfa70cac0f3285e430e79611efa44dec94070c9b5601c9dbf461e560002f723bde1c7e26ae2d7e2641840f1ecca23dc6c8e7e70f153e75
6
+ metadata.gz: cd3677888dabe3342854f3ad10e2591cfb79027fd6f210099c4be014f21e02b3317dec9070a35b41876088fbae5bcd5d4faacdb452bbd6e98cfaf7808f3f3294
7
+ data.tar.gz: a1bdfe2c29b6a873bec6f9866eb11b9a2ac90fe4117df6586f9e3ab3687cb20f29f387b23a34b011f5f2c6a52e52ee881b8b3682ee2e527c1673e2249a4f74fa
@@ -2,7 +2,7 @@
2
2
 
3
3
  module DatabaseConsistency
4
4
  module Checkers
5
- # This class checks if non polymorphic +belongs_to+ association has foreign key constraint
5
+ # This class checks that foreign key has a cascade option matching dependent option
6
6
  class ForeignKeyCascadeChecker < AssociationChecker
7
7
  Report = ReportBuilder.define(
8
8
  DatabaseConsistency::Report,
@@ -14,11 +14,11 @@ module DatabaseConsistency
14
14
  )
15
15
 
16
16
  OPTION_TO_CASCADE = {
17
- delete: :cascade,
18
- delete_all: :cascade,
19
- nullify: :nullify,
20
- restrict_with_exception: :restrict,
21
- restrict_with_error: :restrict
17
+ delete: [:cascade],
18
+ delete_all: [:cascade],
19
+ nullify: [:nullify],
20
+ restrict_with_exception: [nil, :restrict],
21
+ restrict_with_error: [nil, :restrict]
22
22
  }.freeze
23
23
 
24
24
  DEPENDENT_OPTIONS = OPTION_TO_CASCADE.keys.freeze
@@ -52,7 +52,7 @@ module DatabaseConsistency
52
52
  end
53
53
 
54
54
  def correlated_cascade_constraint?
55
- required_foreign_key_cascade == foreign_key_on_delete_option
55
+ required_foreign_key_cascade.include?(foreign_key_on_delete_option)
56
56
  end
57
57
 
58
58
  def dependent_option
@@ -2,7 +2,7 @@
2
2
 
3
3
  module DatabaseConsistency
4
4
  module Checkers
5
- # The base class for table checkers
5
+ # The base class for column checkers
6
6
  class ColumnChecker < BaseChecker
7
7
  attr_reader :model, :column
8
8
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  module DatabaseConsistency
4
4
  module Checkers
5
- # This class checks missing presence validator
5
+ # This class checks the database enum values and ActiveRecord's enum values/inclusion validations are aligned
6
6
  class EnumValueChecker < ColumnChecker
7
7
  Report = ReportBuilder.define(
8
8
  DatabaseConsistency::Report,
@@ -2,7 +2,7 @@
2
2
 
3
3
  module DatabaseConsistency
4
4
  module Checkers
5
- # This class checks missing presence validator
5
+ # This checker checks that column has a length constraint if there is a length validation
6
6
  class LengthConstraintChecker < ColumnChecker
7
7
  VALIDATOR_CLASS =
8
8
  if defined?(ActiveRecord::Validations::LengthValidator)
@@ -2,7 +2,7 @@
2
2
 
3
3
  module DatabaseConsistency
4
4
  module Checkers
5
- # This class checks missing presence validator
5
+ # This class checks that primary key is bigserial, bigint, or uuid
6
6
  class PrimaryKeyTypeChecker < ColumnChecker
7
7
  Report = ReportBuilder.define(
8
8
  DatabaseConsistency::Report,
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DatabaseConsistency
4
+ module Checkers
5
+ # This class checks missing NOT NULL constraint for boolean columns
6
+ class ThreeStateBooleanChecker < ColumnChecker
7
+ Report = ReportBuilder.define(
8
+ DatabaseConsistency::Report,
9
+ :table_name,
10
+ :column_name
11
+ )
12
+
13
+ private
14
+
15
+ def preconditions
16
+ column.type == :boolean
17
+ end
18
+
19
+ def check
20
+ if valid?
21
+ report_template(:ok)
22
+ else
23
+ report_template(:fail, error_slug: :three_state_boolean)
24
+ end
25
+ end
26
+
27
+ def report_template(status, error_slug: nil)
28
+ Report.new(
29
+ status: status,
30
+ error_slug: error_slug,
31
+ error_message: nil,
32
+ table_name: model.table_name,
33
+ column_name: column.name,
34
+ **report_attributes
35
+ )
36
+ end
37
+
38
+ # @return [Boolean]
39
+ def valid?
40
+ !column.null
41
+ end
42
+ end
43
+ end
44
+ end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module DatabaseConsistency
4
4
  module Checkers
5
- # The base class for table checkers
5
+ # The base class for index checkers
6
6
  class IndexChecker < BaseChecker
7
7
  attr_reader :model, :index
8
8
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  module DatabaseConsistency
4
4
  module Checkers
5
- # This class checks redundant database indexes
5
+ # This class checks redundant database unique indexes
6
6
  class RedundantUniqueIndexChecker < IndexChecker
7
7
  Report = ReportBuilder.define(
8
8
  DatabaseConsistency::Report,
@@ -2,7 +2,7 @@
2
2
 
3
3
  module DatabaseConsistency
4
4
  module Checkers
5
- # This class checks if uniqueness validator has unique index in the database
5
+ # This class checks redundant case_sensitive option for uniqueness validation
6
6
  class CaseSensitiveUniqueValidationChecker < ValidatorChecker
7
7
  private
8
8
 
@@ -38,18 +38,25 @@ module DatabaseConsistency
38
38
  end
39
39
 
40
40
  def check
41
- return analyse(attribute.to_s, type: :null_constraint_missing) if regular_column
41
+ if regular_column
42
+ analyse(attribute.to_s, type: :null_constraint_missing)
43
+ else
44
+ analyse_association
45
+ end
46
+ end
42
47
 
48
+ def analyse_association
43
49
  reports = [analyse(association.foreign_key.to_s, type: :association_missing_null_constraint)]
44
50
  if association.polymorphic?
45
51
  reports << analyse(association.foreign_type.to_s, type: :association_foreign_type_missing_null_constraint)
46
52
  end
47
-
48
- reports
53
+ reports.compact.presence
49
54
  end
50
55
 
51
56
  def analyse(column_name, type:)
52
57
  field = column(column_name)
58
+ # If the column is missing there is nothing we can do.
59
+ return if field.nil?
53
60
 
54
61
  can_be_null = field.null
55
62
  has_weak_option = weak_option?
@@ -2,7 +2,7 @@
2
2
 
3
3
  module DatabaseConsistency
4
4
  module Checkers
5
- # The base class for validator checker
5
+ # The base class for validator fraction checkers
6
6
  class ValidatorsFractionChecker < BaseChecker
7
7
  attr_reader :model, :attribute, :validators
8
8
 
@@ -8,7 +8,8 @@ module DatabaseConsistency
8
8
  Checkers::NullConstraintChecker,
9
9
  Checkers::LengthConstraintChecker,
10
10
  Checkers::PrimaryKeyTypeChecker,
11
- Checkers::EnumValueChecker
11
+ Checkers::EnumValueChecker,
12
+ Checkers::ThreeStateBooleanChecker
12
13
  ].freeze
13
14
 
14
15
  private
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DatabaseConsistency
4
- VERSION = '1.7.5'
4
+ VERSION = '1.7.7'
5
5
  end
@@ -16,7 +16,8 @@ module DatabaseConsistency
16
16
  null_constraint_missing: Autofix::NullConstraintMissing,
17
17
  redundant_index: Autofix::RedundantIndex,
18
18
  redundant_unique_index: Autofix::RedundantIndex,
19
- small_primary_key: Autofix::InconsistentTypes
19
+ small_primary_key: Autofix::InconsistentTypes,
20
+ three_state_boolean: Autofix::NullConstraintMissing
20
21
  }.freeze
21
22
 
22
23
  def write
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DatabaseConsistency
4
+ module Writers
5
+ module Simple
6
+ class ThreeStateBoolean < Base # :nodoc:
7
+ private
8
+
9
+ def template
10
+ 'boolean column should have NOT NULL constraint'
11
+ end
12
+
13
+ def unique_attributes
14
+ {
15
+ table_name: report.table_name,
16
+ column_name: report.column_name
17
+ }
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -28,7 +28,8 @@ module DatabaseConsistency
28
28
  missing_foreign_key_cascade: Simple::MissingForeignKeyCascade,
29
29
  enum_values_inconsistent_with_ar_enum: Simple::EnumValuesInconsistentWithArEnum,
30
30
  enum_values_inconsistent_with_inclusion: Simple::EnumValuesInconsistentWithInclusion,
31
- redundant_case_insensitive_option: Simple::RedundantCaseInsensitiveOption
31
+ redundant_case_insensitive_option: Simple::RedundantCaseInsensitiveOption,
32
+ three_state_boolean: Simple::ThreeStateBoolean
32
33
  }.freeze
33
34
 
34
35
  def write
@@ -38,6 +38,7 @@ require 'database_consistency/writers/simple/inconsistent_enum_type'
38
38
  require 'database_consistency/writers/simple/enum_values_inconsistent_with_ar_enum'
39
39
  require 'database_consistency/writers/simple/enum_values_inconsistent_with_inclusion'
40
40
  require 'database_consistency/writers/simple/redundant_case_insensitive_option'
41
+ require 'database_consistency/writers/simple/three_state_boolean'
41
42
  require 'database_consistency/writers/simple_writer'
42
43
 
43
44
  require 'database_consistency/writers/autofix/helpers/migration'
@@ -71,6 +72,7 @@ require 'database_consistency/checkers/column_checkers/null_constraint_checker'
71
72
  require 'database_consistency/checkers/column_checkers/length_constraint_checker'
72
73
  require 'database_consistency/checkers/column_checkers/primary_key_type_checker'
73
74
  require 'database_consistency/checkers/column_checkers/enum_value_checker'
75
+ require 'database_consistency/checkers/column_checkers/three_state_boolean_checker'
74
76
 
75
77
  require 'database_consistency/checkers/validator_checkers/validator_checker'
76
78
  require 'database_consistency/checkers/validator_checkers/missing_unique_index_checker'
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.7.5
4
+ version: 1.7.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evgeniy Demin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-03-05 00:00:00.000000000 Z
11
+ date: 2023-05-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -157,6 +157,7 @@ files:
157
157
  - lib/database_consistency/checkers/column_checkers/length_constraint_checker.rb
158
158
  - lib/database_consistency/checkers/column_checkers/null_constraint_checker.rb
159
159
  - lib/database_consistency/checkers/column_checkers/primary_key_type_checker.rb
160
+ - lib/database_consistency/checkers/column_checkers/three_state_boolean_checker.rb
160
161
  - lib/database_consistency/checkers/enum_checkers/enum_checker.rb
161
162
  - lib/database_consistency/checkers/enum_checkers/enum_type_checker.rb
162
163
  - lib/database_consistency/checkers/index_checkers/index_checker.rb
@@ -228,6 +229,7 @@ files:
228
229
  - lib/database_consistency/writers/simple/redundant_index.rb
229
230
  - lib/database_consistency/writers/simple/redundant_unique_index.rb
230
231
  - lib/database_consistency/writers/simple/small_primary_key.rb
232
+ - lib/database_consistency/writers/simple/three_state_boolean.rb
231
233
  - lib/database_consistency/writers/simple_writer.rb
232
234
  - lib/database_consistency/writers/todo_writer.rb
233
235
  homepage: https://github.com/djezzzl/database_consistency