database_consistency 1.1.2 → 1.1.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: dc9590f6144cef94d2046971c259258a8428cddd9b7083f7225a04d3c5c19933
4
- data.tar.gz: 6a272364d20b0fdfb9be5155dc0e56c3becc4fc98ef0dc777537ae5a5f1c5f0f
3
+ metadata.gz: 9e4c24aeb235c8283ce761d7b4d1e3511246eae28c761a55b4f8ef180a090b22
4
+ data.tar.gz: d8e3b783eae3e68e88126c7f9d77de062748201652ec46492522eda6af0722bf
5
5
  SHA512:
6
- metadata.gz: dcb1f1eae53a0d783c6e83a6f3ca77ae4f5ab4c3086291b268e0e3745abce10d760214c313687db98086ccee9322fe88cbcaade5def8290dea17939447534868
7
- data.tar.gz: 90fcc238b355768e1f2e7b11909b7eb5f2d679b724be2ef3264a158fcdeb916aa751b97e6e14ecfae39f5de985e83887f862565e86c716bb3e43391a463b3a8a
6
+ metadata.gz: 005c1fa13f5aab3d833c9fd0690907f5df81a0d34ee2dcb5ea19ace51ed41f1438c803d56b95d3573eaa0938b8d9295a04dd5e2dafb6a6ab6ca48c6fcba3af4e
7
+ data.tar.gz: fab16372c3782df28f84aac35b997c31ccbb68d3be8ee0b8442e09061c8a85797da584153fc3de151ff1761227348b17041302260daede249b3b10753a1d610b
@@ -7,6 +7,7 @@ module DatabaseConsistency
7
7
  WEAK_OPTIONS = %i[allow_nil allow_blank if unless on].freeze
8
8
  # Message templates
9
9
  CONSTRAINT_MISSING = 'column should be required in the database'
10
+ ASSOCIATION_FOREIGN_KEY_CONSTRAINT_MISSING = 'association foreign key column should be required in the database'
10
11
  POSSIBLE_NULL = 'column is required but there is possible null value insert'
11
12
 
12
13
  private
@@ -15,11 +16,9 @@ module DatabaseConsistency
15
16
  validator.kind == :presence
16
17
  end
17
18
 
18
- # We skip check when:
19
- # - there is no presence validators
20
- # - there is no column in the database with given name
19
+ # We skip the check when there are no presence validators
21
20
  def preconditions
22
- validators.any? && column
21
+ validators.any?
23
22
  end
24
23
 
25
24
  # Table of possible statuses
@@ -33,17 +32,39 @@ module DatabaseConsistency
33
32
  can_be_null = column.null
34
33
  has_weak_option = validators.all? { |validator| validator.options.slice(*WEAK_OPTIONS).any? }
35
34
 
36
- if can_be_null == has_weak_option
37
- report_template(:ok)
38
- elsif can_be_null
35
+ return report_template(:ok) if can_be_null == has_weak_option
36
+ return report_template(:fail, POSSIBLE_NULL) unless can_be_null
37
+
38
+ if regular_column
39
39
  report_template(:fail, CONSTRAINT_MISSING)
40
40
  else
41
- report_template(:fail, POSSIBLE_NULL)
41
+ report_template(:fail, ASSOCIATION_FOREIGN_KEY_CONSTRAINT_MISSING)
42
42
  end
43
43
  end
44
44
 
45
45
  def column
46
- @column ||= model.columns.select.find { |field| field.name == attribute.to_s }
46
+ @column ||= regular_column || association_reference_column ||
47
+ (raise Errors::MissingField, "Missing column in #{model.table_name} for #{attribute}")
48
+ end
49
+
50
+ def regular_column
51
+ @regular_column ||= column_for_name(attribute.to_s)
52
+ end
53
+
54
+ def column_for_name(name)
55
+ model.columns.find { |field| field.name == name.to_s }
56
+ end
57
+
58
+ def association_reference_column
59
+ return unless association_reflection
60
+
61
+ column_for_name(association_reflection.foreign_key)
62
+ end
63
+
64
+ def association_reflection
65
+ model
66
+ .reflect_on_all_associations
67
+ .find { |reflection| reflection.belongs_to? && reflection.name == attribute }
47
68
  end
48
69
  end
49
70
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DatabaseConsistency
4
- VERSION = '1.1.2'
4
+ VERSION = '1.1.3'
5
5
  end
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.1.2
4
+ version: 1.1.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: 2021-08-03 00:00:00.000000000 Z
11
+ date: 2021-10-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord