database_consistency 1.4.1 → 1.5.1

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: 5c6d7fe7d9a50ab2233cca7b018d096f9d47b850e2fb92b2724fecba15eb78e6
4
- data.tar.gz: 5c98abb3adb05dfddb3c586e3eae4a87d35069d07332c516154791514cc9c70f
3
+ metadata.gz: 074a7027af87351fbb41c7e7d730568771d9399929c1bcd3794b2c4b2af92c29
4
+ data.tar.gz: a2d0fcce0d3dc47adf0a7e6081e7ad4489d5770ee0d4c55661e596235425d957
5
5
  SHA512:
6
- metadata.gz: 32ee0e823c4c893f5e062f847f33dca5fe6094bfc98d12786f879cbe6a2d2182a44f5d9756271550b524093c56030d38f72ce4ff58de6175b2936852e3e7d3d0
7
- data.tar.gz: 210a2a55056fee4992ad8e7b719aa42604b03944891e2061976b27972ad8fd5c7b8ac920fb5b888f193a0dd2fbb27e2a205ffba1252a83c736a5926a471641db
6
+ metadata.gz: ccf20c7f82e25eb2e9cd6bc42b87c42cb413d09a368b514cf4f34809884ee015a408e927ddcf3817e94a57f15540744e590839961df9ddb95d0a7ce6b857823c
7
+ data.tar.gz: 82b30815054c3987d13ea769b0a2b2c6ae7e72a70aa23a39d108e9369b5d8fbc4b864d7275798e4e9f3090b1a8b267b9100c73ee70a5482c94fe0a465edf9350
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DatabaseConsistency
4
+ module Checkers
5
+ # This class checks if non polymorphic +belongs_to+ association has foreign key constraint
6
+ class ForeignKeyCascadeChecker < AssociationChecker
7
+ Report = ReportBuilder.define(
8
+ DatabaseConsistency::Report,
9
+ :cascade_option,
10
+ :primary_table,
11
+ :foreign_table,
12
+ :primary_key,
13
+ :foreign_key
14
+ )
15
+
16
+ OPTION_TO_CASCADE = {
17
+ delete: :cascade,
18
+ delete_all: :cascade,
19
+ nullify: :nullify,
20
+ restrict_with_exception: :restrict,
21
+ restrict_with_error: :restrict
22
+ }.freeze
23
+
24
+ DEPENDENT_OPTIONS = OPTION_TO_CASCADE.keys.freeze
25
+
26
+ private
27
+
28
+ def preconditions
29
+ !association.polymorphic? &&
30
+ !association.belongs_to? &&
31
+ foreign_key &&
32
+ DEPENDENT_OPTIONS.include?(dependent_option)
33
+ rescue StandardError
34
+ false
35
+ end
36
+
37
+ # Table of possible statuses
38
+ # | foreign key | status |
39
+ # | ----------- | ------ |
40
+ # | persisted | ok |
41
+ # | missing | fail |
42
+ def check
43
+ if correlated_cascade_constraint?
44
+ report_template(:ok)
45
+ else
46
+ report_template(:fail, error_slug: :missing_foreign_key_cascade)
47
+ end
48
+ end
49
+
50
+ def required_foreign_key_cascade
51
+ OPTION_TO_CASCADE[dependent_option]
52
+ end
53
+
54
+ def correlated_cascade_constraint?
55
+ required_foreign_key_cascade == foreign_key_on_delete_option
56
+ end
57
+
58
+ def dependent_option
59
+ association.options[:dependent]
60
+ end
61
+
62
+ def foreign_key_on_delete_option
63
+ foreign_key.options[:on_delete]
64
+ end
65
+
66
+ def foreign_key
67
+ @foreign_key ||=
68
+ association.klass
69
+ .connection
70
+ .foreign_keys(association.klass.table_name)
71
+ .find { |fk| fk.column == association.foreign_key.to_s }
72
+ end
73
+
74
+ def report_template(status, error_slug: nil)
75
+ Report.new(
76
+ status: status,
77
+ error_message: nil,
78
+ error_slug: error_slug,
79
+ primary_table: association.table_name.to_s,
80
+ primary_key: association.association_primary_key.to_s,
81
+ foreign_table: association.active_record.table_name.to_s,
82
+ foreign_key: association.foreign_key.to_s,
83
+ cascade_option: required_foreign_key_cascade,
84
+ **report_attributes
85
+ )
86
+ end
87
+ end
88
+ end
89
+ end
@@ -37,7 +37,7 @@ module DatabaseConsistency
37
37
 
38
38
  def column_type_converter(type)
39
39
  case type
40
- when 'string' then String
40
+ when 'string', 'enum' then String
41
41
  when 'integer' then Integer
42
42
  when 'decimal' then BigDecimal
43
43
  when 'date' then Date
@@ -7,7 +7,8 @@ module DatabaseConsistency
7
7
  CHECKERS = [
8
8
  Checkers::MissingIndexChecker,
9
9
  Checkers::ForeignKeyChecker,
10
- Checkers::ForeignKeyTypeChecker
10
+ Checkers::ForeignKeyTypeChecker,
11
+ Checkers::ForeignKeyCascadeChecker
11
12
  ].freeze
12
13
 
13
14
  private
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DatabaseConsistency
4
- VERSION = '1.4.1'
4
+ VERSION = '1.5.1'
5
5
  end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DatabaseConsistency
4
+ module Writers
5
+ module Simple
6
+ class MissingForeignKeyCascade < Base # :nodoc:
7
+ private
8
+
9
+ def template
10
+ 'should have foreign key with on_delete: :%<cascade_option>s in the database'
11
+ end
12
+
13
+ def attributes
14
+ {
15
+ cascade_option: report.cascade_option
16
+ }
17
+ end
18
+
19
+ def unique_attributes
20
+ {
21
+ foreign_table: report.foreign_table,
22
+ foreign_key: report.foreign_key,
23
+ primary_table: report.primary_table,
24
+ primary_key: report.primary_key,
25
+ cascade_option: report.cascade_option
26
+ }
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -23,7 +23,8 @@ module DatabaseConsistency
23
23
  redundant_index: Simple::RedundantIndex,
24
24
  redundant_unique_index: Simple::RedundantUniqueIndex,
25
25
  small_primary_key: Simple::SmallPrimaryKey,
26
- inconsistent_enum_type: Simple::InconsistentEnumType
26
+ inconsistent_enum_type: Simple::InconsistentEnumType,
27
+ missing_foreign_key_cascade: Simple::MissingForeignKeyCascade
27
28
  }.freeze
28
29
 
29
30
  def write
@@ -18,6 +18,7 @@ require 'database_consistency/writers/simple/default_message'
18
18
  require 'database_consistency/writers/simple/inconsistent_types'
19
19
  require 'database_consistency/writers/simple/null_constraint_association_misses_validator'
20
20
  require 'database_consistency/writers/simple/redundant_index'
21
+ require 'database_consistency/writers/simple/missing_foreign_key_cascade'
21
22
  require 'database_consistency/writers/simple/redundant_unique_index'
22
23
  require 'database_consistency/writers/simple/association_missing_index'
23
24
  require 'database_consistency/writers/simple/association_missing_null_constraint'
@@ -59,6 +60,7 @@ require 'database_consistency/checkers/association_checkers/association_checker'
59
60
  require 'database_consistency/checkers/association_checkers/missing_index_checker'
60
61
  require 'database_consistency/checkers/association_checkers/foreign_key_checker'
61
62
  require 'database_consistency/checkers/association_checkers/foreign_key_type_checker'
63
+ require 'database_consistency/checkers/association_checkers/foreign_key_cascade_checker'
62
64
 
63
65
  require 'database_consistency/checkers/column_checkers/column_checker'
64
66
  require 'database_consistency/checkers/column_checkers/null_constraint_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.4.1
4
+ version: 1.5.1
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-27 00:00:00.000000000 Z
11
+ date: 2022-11-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -147,6 +147,7 @@ files:
147
147
  - bin/database_consistency
148
148
  - lib/database_consistency.rb
149
149
  - lib/database_consistency/checkers/association_checkers/association_checker.rb
150
+ - lib/database_consistency/checkers/association_checkers/foreign_key_cascade_checker.rb
150
151
  - lib/database_consistency/checkers/association_checkers/foreign_key_checker.rb
151
152
  - lib/database_consistency/checkers/association_checkers/foreign_key_type_checker.rb
152
153
  - lib/database_consistency/checkers/association_checkers/missing_index_checker.rb
@@ -211,6 +212,7 @@ files:
211
212
  - lib/database_consistency/writers/simple/length_validator_lower_limit.rb
212
213
  - lib/database_consistency/writers/simple/length_validator_missing.rb
213
214
  - lib/database_consistency/writers/simple/missing_foreign_key.rb
215
+ - lib/database_consistency/writers/simple/missing_foreign_key_cascade.rb
214
216
  - lib/database_consistency/writers/simple/missing_unique_index.rb
215
217
  - lib/database_consistency/writers/simple/missing_uniqueness_validation.rb
216
218
  - lib/database_consistency/writers/simple/null_constraint_association_misses_validator.rb