database_consistency 1.4.0 → 1.4.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: 9bc1994c7c24ab9a38d97c764fb0d9ce55e4fa71934df313f2cc693c466cecb7
4
- data.tar.gz: 04110bdba61254592c2b22323555948ad06a78b8a63ce78a40175005096acb54
3
+ metadata.gz: 5c6d7fe7d9a50ab2233cca7b018d096f9d47b850e2fb92b2724fecba15eb78e6
4
+ data.tar.gz: 5c98abb3adb05dfddb3c586e3eae4a87d35069d07332c516154791514cc9c70f
5
5
  SHA512:
6
- metadata.gz: 581c8660605072f0cd877310813d71781b50de0af57aa5d85b216b57dd3ed040dc90944e165e51ff9b4728d180474157281ce1e028d9295b7137657367a4fa80
7
- data.tar.gz: 0c9d42d9902485c01d208ac38110aa4c7742b4ceca00c369c4fc4c014e84df038b708f17b084f9cee7da8eef28a1f31b52d4a29fa2f597f7cf454af1cefc828c
6
+ metadata.gz: 32ee0e823c4c893f5e062f847f33dca5fe6094bfc98d12786f879cbe6a2d2182a44f5d9756271550b524093c56030d38f72ce4ff58de6175b2936852e3e7d3d0
7
+ data.tar.gz: 210a2a55056fee4992ad8e7b719aa42604b03944891e2061976b27972ad8fd5c7b8ac920fb5b888f193a0dd2fbb27e2a205ffba1252a83c736a5926a471641db
@@ -4,8 +4,6 @@ module DatabaseConsistency
4
4
  module Checkers
5
5
  # This class checks enum types consistency
6
6
  class EnumTypeChecker < EnumChecker
7
- MissingColumn = Class.new(StandardError)
8
-
9
7
  Report = DatabaseConsistency::ReportBuilder.define(
10
8
  DatabaseConsistency::Report,
11
9
  :column_type,
@@ -15,7 +13,7 @@ module DatabaseConsistency
15
13
  private
16
14
 
17
15
  def preconditions
18
- true
16
+ column.present?
19
17
  end
20
18
 
21
19
  def check
@@ -53,10 +51,11 @@ module DatabaseConsistency
53
51
  model.defined_enums[enum].values.map(&:class).uniq
54
52
  end
55
53
 
56
- def column_type
57
- column = model.columns.find { |c| c.name.to_s == enum.to_s }
58
- raise MissingColumn unless column
54
+ def column
55
+ @column ||= model.columns.find { |c| c.name.to_s == enum.to_s }
56
+ end
59
57
 
58
+ def column_type
60
59
  column.type.to_s
61
60
  end
62
61
 
@@ -20,26 +20,13 @@ module DatabaseConsistency
20
20
 
21
21
  # We skip the check when there are no presence validators
22
22
  def preconditions
23
- validators.any? && !association?
23
+ column && validators.any? && !association?
24
24
  end
25
25
 
26
26
  def association?
27
27
  model._reflect_on_association(attribute)&.macro == :has_one
28
28
  end
29
29
 
30
- # Table of possible statuses
31
- # | allow_nil/allow_blank/if/unless | database | status |
32
- # | ------------------------------- | -------- | ------ |
33
- # | at least one provided | required | fail |
34
- # | at least one provided | optional | ok |
35
- # | all missing | required | ok |
36
- # | all missing | optional | fail |
37
- def check
38
- report_message
39
- rescue Errors::MissingField => e
40
- report_template(:fail, error_message: e.message)
41
- end
42
-
43
30
  def report_template(status, error_message: nil, error_slug: nil)
44
31
  Report.new(
45
32
  status: status,
@@ -55,7 +42,7 @@ module DatabaseConsistency
55
42
  validators.all? { |validator| validator.options.slice(*WEAK_OPTIONS).any? }
56
43
  end
57
44
 
58
- def report_message # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
45
+ def check # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
59
46
  can_be_null = column.null
60
47
  has_weak_option = weak_option?
61
48
 
@@ -84,9 +71,7 @@ module DatabaseConsistency
84
71
  end
85
72
 
86
73
  def column
87
- @column ||= regular_column ||
88
- association_reference_column ||
89
- (raise Errors::MissingField, "column (#{attribute}) is missing in table (#{model.table_name}) but used for presence validation") # rubocop:disable Layout/LineLength
74
+ @column ||= (regular_column || association_reference_column)
90
75
  end
91
76
 
92
77
  def regular_column
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DatabaseConsistency
4
- VERSION = '1.4.0'
4
+ VERSION = '1.4.1'
5
5
  end
@@ -17,7 +17,9 @@ module DatabaseConsistency
17
17
  private
18
18
 
19
19
  def migration
20
- File.read(template_path) % attributes.merge(migration_configuration(migration_name))
20
+ attributes.merge(migration_configuration(migration_name)).reduce(File.read(template_path)) do |str, (k, v)|
21
+ str.gsub("%<#{k}>s", v.to_s)
22
+ end
21
23
  end
22
24
  end
23
25
  end
@@ -47,7 +47,9 @@ module DatabaseConsistency
47
47
  end
48
48
 
49
49
  def message_text
50
- template % attributes
50
+ attributes.reduce(template) do |str, (k, v)|
51
+ str.gsub("%<#{k}>s", v.to_s)
52
+ end
51
53
  end
52
54
 
53
55
  def attributes
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: database_consistency
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evgeniy Demin