database_consistency 0.2.1 → 0.2.2

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: 86853e78b9e1bb68f9e34dbea3b23de33890760a3598b954cd8e89f3ddf88193
4
- data.tar.gz: '082c7135a3057bbe59b78d463b62fd522cfcfc3bd0c8b452bb591375d72d962d'
3
+ metadata.gz: 93c3eb656f1f4ca77b34a647a612db82f713d624c672e7205a79ffc2c4715608
4
+ data.tar.gz: 879befe9d7b24ae68f1ef492b9f32ae1e166c9d32ff9706bc0aebd68007d0511
5
5
  SHA512:
6
- metadata.gz: a82a054afba3dac8b0c75bbfaeab357f4c9f9c20d50f854cf0ae30442dbcae1dd18d4223c6e5d31545aa8927c0cd16c243bdc45aeca25102e2a74bee5da097b7
7
- data.tar.gz: e0a898d8f10bbf2c47ec4fd397f1b2f01f4696f0ee23bcce47c6ee0d2efa4a2aa0d5caefc219f621fa14701d86fb3aecad3ff414190efbf4542360c5a7f2085c
6
+ metadata.gz: bcb8edf4ae4fd959295dbdb1782c1f099c540b1c30d7d317da5f8164928502cd27666da9097059cbbfdc1f0ac060ed1ac90cab7aaf4cd6b2be683aad08c8010b
7
+ data.tar.gz: a3ee3f2e85349980cad0d06e273ba77c889e5bd262b17df3dfb2fda5440b3aac97acf4e69e9287f5ea98b7368535de882031aeef73a3f2c98f54dd5294ba8c20
@@ -5,7 +5,9 @@ module DatabaseConsistency
5
5
  VALIDATOR_MISSING = 'is required but do not have presence validator'.freeze
6
6
 
7
7
  def verify
8
- result(:fail, Helper.message(model, column, VALIDATOR_MISSING)) unless skip? || validator?
8
+ return if skip? || presence_validator? || inclusion_validator? || belongs_to_reflection?
9
+
10
+ result(:fail, Helper.message(model, column, VALIDATOR_MISSING))
9
11
  end
10
12
 
11
13
  private
@@ -16,9 +18,21 @@ module DatabaseConsistency
16
18
  (model.record_timestamps? && %w[created_at updated_at].include?(column.name))
17
19
  end
18
20
 
19
- def validator?
21
+ def presence_validator?
20
22
  model.validators.grep(ActiveModel::Validations::PresenceValidator).any? do |validator|
21
- validator.attributes.include?(column.name) || validator.attributes.include?(column.name.to_sym)
23
+ Helper.check_inclusion?(validator.attributes, column.name)
24
+ end
25
+ end
26
+
27
+ def inclusion_validator?
28
+ model.validators.grep(ActiveModel::Validations::InclusionValidator).any? do |validator|
29
+ Helper.check_inclusion?(validator.attributes, column.name)
30
+ end
31
+ end
32
+
33
+ def belongs_to_reflection?
34
+ model.reflect_on_all_associations.grep(ActiveRecord::Reflection::BelongsToReflection).any? do |reflection|
35
+ Helper.check_inclusion?([reflection.foreign_key, reflection.foreign_type], column.name)
22
36
  end
23
37
  end
24
38
  end
@@ -6,7 +6,7 @@ module DatabaseConsistency
6
6
  ].freeze
7
7
 
8
8
  def reports
9
- Helper.models.flat_map do |model|
9
+ Helper.parent_models.flat_map do |model|
10
10
  model.columns.flat_map do |column|
11
11
  VERIFIERS.map do |verifier|
12
12
  verifier.verify(model, column)
@@ -5,7 +5,14 @@ module DatabaseConsistency
5
5
 
6
6
  # Returns list of models to check
7
7
  def models
8
- ActiveRecord::Base.descendants.select { |model| model.validators.any? }
8
+ ActiveRecord::Base.descendants.delete_if(&:abstract_class?)
9
+ end
10
+
11
+ # Return list of not inherited models
12
+ def parent_models
13
+ models.group_by(&:table_name).each_value.map do |models|
14
+ models.min_by { |model| models.include?(model.superclass) ? 1 : 0 }
15
+ end
9
16
  end
10
17
 
11
18
  # Loads all models
@@ -20,9 +27,14 @@ module DatabaseConsistency
20
27
 
21
28
  # @return [String]
22
29
  def message(model, column, template = nil)
23
- str = "column #{column.name} of table #{model.table_name}"
30
+ str = "column #{column.name} of table #{model.table_name} of model #{model.name}"
24
31
  str += " #{template}" if template
25
32
  str
26
33
  end
34
+
35
+ # @return [Boolean]
36
+ def check_inclusion?(array, element)
37
+ array.include?(element.to_s) || array.include?(element.to_sym)
38
+ end
27
39
  end
28
40
  end
@@ -9,7 +9,7 @@ module DatabaseConsistency
9
9
 
10
10
  def result(status, message)
11
11
  {
12
- status: status,
12
+ status: status,
13
13
  message: message
14
14
  }.tap { |hash| hash[:opts] = opts unless opts.empty? }
15
15
  end
@@ -1,3 +1,3 @@
1
1
  module DatabaseConsistency
2
- VERSION = '0.2.1'.freeze
2
+ VERSION = '0.2.2'.freeze
3
3
  end
@@ -10,6 +10,7 @@ module DatabaseConsistency
10
10
  def format
11
11
  results.map do |result|
12
12
  next unless write?(result[:status])
13
+
13
14
  line(result)
14
15
  end.tap(&:compact!).map(&:lstrip).delete_if(&:empty?).join(delimiter)
15
16
  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: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evgeniy Demin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-10-31 00:00:00.000000000 Z
11
+ date: 2018-11-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord