database_consistency 1.5.1 → 1.5.3

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: 074a7027af87351fbb41c7e7d730568771d9399929c1bcd3794b2c4b2af92c29
4
- data.tar.gz: a2d0fcce0d3dc47adf0a7e6081e7ad4489d5770ee0d4c55661e596235425d957
3
+ metadata.gz: f06f5f350f8f4a0dc3eab32d48b01f61da8fde59ebcbc0fcd35296a5a0a2ecd5
4
+ data.tar.gz: 13d02eb9836ef51570c33b721909517e1abbbb2d7406e5236a718d040e997f0d
5
5
  SHA512:
6
- metadata.gz: ccf20c7f82e25eb2e9cd6bc42b87c42cb413d09a368b514cf4f34809884ee015a408e927ddcf3817e94a57f15540744e590839961df9ddb95d0a7ce6b857823c
7
- data.tar.gz: 82b30815054c3987d13ea769b0a2b2c6ae7e72a70aa23a39d108e9369b5d8fbc4b864d7275798e4e9f3090b1a8b267b9100c73ee70a5482c94fe0a465edf9350
6
+ metadata.gz: be22b9c36f696268721fdad8fdb8cb5379cb739261d40577754ede248c4cdfd7cf1858f03a6609c762fa500ce87424b3cfc2ed60c93a437bcb2af15dc92f4f8d
7
+ data.tar.gz: cdfdaa8f8ba2d0e8af3e510407dcfffb36a1568c380a8be115aa87369bf3f97f45d3c2ebb6587f30a0b2048a576193bf90a168e924f3387f6b05e34f1f4d6ced
@@ -39,7 +39,7 @@ opt_parser = OptionParser.new do |opts|
39
39
  options[:todo] = true
40
40
  end
41
41
 
42
- opts.on('-fix', '--autofix', 'Automatically fixes issues by adjusting the code or generating missing migrations.') do
42
+ opts.on('-f', '--autofix', 'Automatically fixes issues by adjusting the code or generating missing migrations.') do
43
43
  options[:autofix] = true
44
44
  end
45
45
 
@@ -21,7 +21,7 @@ module DatabaseConsistency
21
21
  has_one
22
22
  has_many
23
23
  ].include?(association.macro) && association.through_reflection.nil? && association.klass
24
- rescue NameError
24
+ rescue StandardError
25
25
  false
26
26
  end
27
27
 
@@ -18,22 +18,17 @@ module DatabaseConsistency
18
18
  validator.kind == :presence
19
19
  end
20
20
 
21
- # We skip the check when there are no presence validators
22
21
  def preconditions
23
- column && validators.any? && !association?
22
+ (regular_column || association) && validators.any?
24
23
  end
25
24
 
26
- def association?
27
- model._reflect_on_association(attribute)&.macro == :has_one
28
- end
29
-
30
- def report_template(status, error_message: nil, error_slug: nil)
25
+ def report_template(status, column_name:, error_slug: nil)
31
26
  Report.new(
32
27
  status: status,
33
28
  error_slug: error_slug,
34
- error_message: error_message,
29
+ error_message: nil,
35
30
  table_name: model.table_name.to_s,
36
- column_name: attribute.to_s,
31
+ column_name: column_name,
37
32
  **report_attributes
38
33
  )
39
34
  end
@@ -42,56 +37,42 @@ module DatabaseConsistency
42
37
  validators.all? { |validator| validator.options.slice(*WEAK_OPTIONS).any? }
43
38
  end
44
39
 
45
- def check # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
46
- can_be_null = column.null
47
- has_weak_option = weak_option?
40
+ def check
41
+ return analyse(attribute.to_s, type: :null_constraint_missing) if regular_column
48
42
 
49
- return report_template(:ok) if can_be_null == has_weak_option
50
- return report_template(:fail, error_slug: :possible_null) unless can_be_null
51
-
52
- if regular_column
53
- Report.new(
54
- status: :fail,
55
- error_slug: :null_constraint_missing,
56
- error_message: nil,
57
- table_name: model.table_name.to_s,
58
- column_name: attribute.to_s,
59
- **report_attributes
60
- )
61
- else
62
- Report.new(
63
- status: :fail,
64
- error_slug: :association_missing_null_constraint,
65
- error_message: nil,
66
- table_name: model.table_name.to_s,
67
- column_name: association_reflection.foreign_key.to_s,
68
- **report_attributes
69
- )
43
+ reports = [analyse(association.foreign_key.to_s, type: :association_missing_null_constraint)]
44
+ if association.polymorphic?
45
+ reports << analyse(association.foreign_type.to_s, type: :association_foreign_type_missing_null_constraint)
70
46
  end
71
- end
72
47
 
73
- def column
74
- @column ||= (regular_column || association_reference_column)
48
+ reports
75
49
  end
76
50
 
77
- def regular_column
78
- @regular_column ||= column_for_name(attribute.to_s)
79
- end
51
+ def analyse(column_name, type:)
52
+ field = column(column_name)
80
53
 
81
- def column_for_name(name)
82
- model.columns.find { |field| field.name == name.to_s }
83
- end
54
+ can_be_null = field.null
55
+ has_weak_option = weak_option?
56
+
57
+ return report_template(:ok, column_name: column_name) if can_be_null == has_weak_option
58
+ return report_template(:fail, error_slug: :possible_null, column_name: column_name) unless can_be_null
84
59
 
85
- def association_reference_column
86
- return unless association_reflection
60
+ report_template(:fail, error_slug: type, column_name: column_name)
61
+ end
87
62
 
88
- column_for_name(association_reflection.foreign_key)
63
+ def regular_column
64
+ @regular_column ||= column(attribute.to_s)
89
65
  end
90
66
 
91
- def association_reflection
92
- model
67
+ def association
68
+ @association ||=
69
+ model
93
70
  .reflect_on_all_associations
94
- .find { |reflection| reflection.belongs_to? && reflection.name == attribute }
71
+ .find { |reflection| reflection.belongs_to? && reflection.name.to_s == attribute.to_s }
72
+ end
73
+
74
+ def column(name)
75
+ model.columns.find { |field| field.name == name.to_s }
95
76
  end
96
77
  end
97
78
  end
@@ -18,7 +18,7 @@ module DatabaseConsistency
18
18
  next unless configuration.enabled?(model.name.to_s)
19
19
 
20
20
  Helper.first_level_associations(model).flat_map do |association|
21
- enabled_checkers.map do |checker_class|
21
+ enabled_checkers.flat_map do |checker_class|
22
22
  checker = checker_class.new(model, association)
23
23
  checker.report_if_enabled?(configuration)
24
24
  end
@@ -17,7 +17,7 @@ module DatabaseConsistency
17
17
  next unless configuration.enabled?(model.name.to_s)
18
18
 
19
19
  model.columns.flat_map do |column|
20
- enabled_checkers.map do |checker_class|
20
+ enabled_checkers.flat_map do |checker_class|
21
21
  checker = checker_class.new(model, column)
22
22
  checker.report_if_enabled?(configuration)
23
23
  end
@@ -15,7 +15,7 @@ module DatabaseConsistency
15
15
  next unless configuration.enabled?(model.name.to_s)
16
16
 
17
17
  model.defined_enums.keys.flat_map do |enum|
18
- enabled_checkers.map do |checker_class|
18
+ enabled_checkers.flat_map do |checker_class|
19
19
  checker = checker_class.new(model, enum)
20
20
  checker.report_if_enabled?(configuration)
21
21
  end
@@ -19,7 +19,7 @@ module DatabaseConsistency
19
19
  indexes = model.connection.indexes(model.table_name)
20
20
 
21
21
  indexes.flat_map do |index|
22
- enabled_checkers.map do |checker_class|
22
+ enabled_checkers.flat_map do |checker_class|
23
23
  checker = checker_class.new(model, index)
24
24
  checker.report_if_enabled?(configuration)
25
25
  end
@@ -18,7 +18,7 @@ module DatabaseConsistency
18
18
  model._validators.flat_map do |attribute, validators|
19
19
  next unless attribute
20
20
 
21
- enabled_checkers.map do |checker_class|
21
+ enabled_checkers.flat_map do |checker_class|
22
22
  checker = checker_class.new(model, attribute, validators)
23
23
  checker.report_if_enabled?(configuration)
24
24
  end
@@ -19,7 +19,7 @@ module DatabaseConsistency
19
19
  next unless validator.respond_to?(:attributes)
20
20
 
21
21
  validator.attributes.flat_map do |attribute|
22
- enabled_checkers.map do |checker_class|
22
+ enabled_checkers.flat_map do |checker_class|
23
23
  checker = checker_class.new(model, attribute, validator)
24
24
  checker.report_if_enabled?(configuration)
25
25
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DatabaseConsistency
4
- VERSION = '1.5.1'
4
+ VERSION = '1.5.3'
5
5
  end
@@ -8,6 +8,7 @@ module DatabaseConsistency
8
8
  SLUG_TO_GENERATOR = {
9
9
  association_missing_index: Autofix::AssociationMissingIndex,
10
10
  association_missing_null_constraint: Autofix::NullConstraintMissing,
11
+ association_foreign_type_missing_null_constraint: Autofix::NullConstraintMissing,
11
12
  has_one_missing_unique_index: Autofix::HasOneMissingUniqueIndex,
12
13
  inconsistent_types: Autofix::InconsistentTypes,
13
14
  missing_foreign_key: Autofix::MissingForeignKey,
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DatabaseConsistency
4
+ module Writers
5
+ module Simple
6
+ class AssociationForeignTypeMissingNullConstraint < Base # :nodoc:
7
+ private
8
+
9
+ def template
10
+ 'association foreign type column should be required in the database'
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
@@ -8,6 +8,7 @@ module DatabaseConsistency
8
8
  SLUG_TO_WRITER = {
9
9
  association_missing_index: Simple::AssociationMissingIndex,
10
10
  association_missing_null_constraint: Simple::AssociationMissingNullConstraint,
11
+ association_foreign_type_missing_null_constraint: Simple::AssociationForeignTypeMissingNullConstraint,
11
12
  has_one_missing_unique_index: Simple::HasOneMissingUniqueIndex,
12
13
  inconsistent_types: Simple::InconsistentTypes,
13
14
  length_validator_greater_limit: Simple::LengthValidatorGreaterLimit,
@@ -22,6 +22,7 @@ require 'database_consistency/writers/simple/missing_foreign_key_cascade'
22
22
  require 'database_consistency/writers/simple/redundant_unique_index'
23
23
  require 'database_consistency/writers/simple/association_missing_index'
24
24
  require 'database_consistency/writers/simple/association_missing_null_constraint'
25
+ require 'database_consistency/writers/simple/association_foreign_type_missing_null_constraint'
25
26
  require 'database_consistency/writers/simple/has_one_missing_unique_index'
26
27
  require 'database_consistency/writers/simple/length_validator_lower_limit'
27
28
  require 'database_consistency/writers/simple/length_validator_greater_limit'
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.5.1
4
+ version: 1.5.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-29 00:00:00.000000000 Z
11
+ date: 2022-12-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -201,6 +201,7 @@ files:
201
201
  - lib/database_consistency/writers/autofix/templates/redundant_index.tt
202
202
  - lib/database_consistency/writers/autofix_writer.rb
203
203
  - lib/database_consistency/writers/base_writer.rb
204
+ - lib/database_consistency/writers/simple/association_foreign_type_missing_null_constraint.rb
204
205
  - lib/database_consistency/writers/simple/association_missing_index.rb
205
206
  - lib/database_consistency/writers/simple/association_missing_null_constraint.rb
206
207
  - lib/database_consistency/writers/simple/base.rb