database_consistency 0.8.5 → 0.8.10

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: ed9a2103f6e588628f52a00ddd933b0dc899fac0d6e8752a3bf1111a3894fb17
4
- data.tar.gz: d8bf6a551c55fc3e4a33d837354f1b3d777c6171b48aa84d2cce5bbba478e0d5
3
+ metadata.gz: 3790aaad0e110f0764989efae93b0f0e12e8fd3a915fe8d6ed52fb2dec09646b
4
+ data.tar.gz: 7d20a3901b932cc31b5c49d4e02dc78725835f13c2d5c7f147a0fe137af35cd1
5
5
  SHA512:
6
- metadata.gz: a1c59e4de257fdaa7aae63725abcc82f7da1f4ea98f51fc804162b8c5488021c3c665f7e7298b135eaddd50560415b563f0cd608fdaaee81519e44af74966bb2
7
- data.tar.gz: 516f5102afb3234eb88d8a8039b584c9210b33d78401290423032cbd017df5218aa6a455ae5faeffb54dd9bad7ea82a17a40f1729255c7ea2ed988924bad3ea9
6
+ metadata.gz: b6f565eaf656ee1d0b63944ab11ebfec0352286bb0d404070eaab4e58e2ad4e9b810ed95292a0162c8df0119ad75861fd462da188a73b239a89293ac044cfd91
7
+ data.tar.gz: d66b391ad1b3e84d1e4ad1b839732ea8a695f335f9bb180cea18c7447981cdd3ae947e1df010d5d7c9de0f209176053eba7ccd6a2fb5c32425fe57e8d7ec038d
@@ -6,6 +6,7 @@ require 'database_consistency/version'
6
6
  require 'database_consistency/helper'
7
7
  require 'database_consistency/configuration'
8
8
  require 'database_consistency/rescue_error'
9
+ require 'database_consistency/errors'
9
10
 
10
11
  require 'database_consistency/writers/base_writer'
11
12
  require 'database_consistency/writers/simple_writer'
@@ -17,8 +17,16 @@ module DatabaseConsistency
17
17
 
18
18
  private
19
19
 
20
+ # We skip check when:
21
+ # - association is polymorphic association
22
+ # - association is has_and_belongs_to_many
23
+ # - association has `through` option
24
+ # - associated class doesn't exist
20
25
  def preconditions
21
- !association.polymorphic? && association.klass.present?
26
+ !association.polymorphic? &&
27
+ association.through_reflection.nil? &&
28
+ association.klass.present? &&
29
+ association.macro != :has_and_belongs_to_many
22
30
  rescue NameError
23
31
  false
24
32
  end
@@ -47,12 +55,24 @@ module DatabaseConsistency
47
55
 
48
56
  # @return [String]
49
57
  def base_key
50
- @base_key ||= belongs_to_association? ? association.foreign_key : association.active_record_primary_key
58
+ @base_key ||= (
59
+ if belongs_to_association?
60
+ association.foreign_key
61
+ else
62
+ association.association_primary_key
63
+ end
64
+ ).to_s
51
65
  end
52
66
 
53
67
  # @return [String]
54
68
  def associated_key
55
- @associated_key ||= belongs_to_association? ? association.active_record_primary_key : association.foreign_key
69
+ @associated_key ||= (
70
+ if belongs_to_association?
71
+ association.association_primary_key
72
+ else
73
+ association.foreign_key
74
+ end
75
+ ).to_s
56
76
  end
57
77
 
58
78
  # @return [ActiveRecord::ConnectionAdapters::Column]
@@ -75,7 +95,14 @@ module DatabaseConsistency
75
95
  #
76
96
  # @return [ActiveRecord::ConnectionAdapters::Column]
77
97
  def column(model, column_name)
78
- model.connection.columns(model.table_name).find { |column| column.name == column_name }
98
+ model.connection.columns(model.table_name).find { |column| column.name == column_name } ||
99
+ (raise Errors::MissingField, missing_field_error(model.table_name, column_name))
100
+ end
101
+
102
+ # @return [String]
103
+ def missing_field_error(table_name, column_name)
104
+ "Association (#{association.name}) of class (#{association.active_record}) relies on "\
105
+ "field (#{column_name}) of table (#{table_name}) but it is missing."
79
106
  end
80
107
 
81
108
  # @param [ActiveRecord::ConnectionAdapters::Column] column
@@ -92,11 +119,6 @@ module DatabaseConsistency
92
119
  database_factory.type(type(column)).convert
93
120
  end
94
121
 
95
- # @return [Boolean]
96
- def supported_type?(column)
97
- database_factory.type(type(column)).supported?
98
- end
99
-
100
122
  # @return [Boolean]
101
123
  def belongs_to_association?
102
124
  association.macro == :belongs_to
@@ -16,16 +16,20 @@ module DatabaseConsistency
16
16
  @checker_name ||= name.split('::').last
17
17
  end
18
18
 
19
- # @return [Hash, nil]
20
- def report
19
+ # @param [Boolean] catch_errors
20
+ #
21
+ # @return [Hash, File, nil]
22
+ def report(catch_errors = true)
21
23
  return unless preconditions
22
24
 
23
25
  @report ||= check
24
26
  rescue StandardError => e
27
+ raise e unless catch_errors
28
+
25
29
  RescueError.call(e)
26
30
  end
27
31
 
28
- # @return [Hash, nil]
32
+ # @return [Hash, File, nil]
29
33
  def report_if_enabled?(configuration)
30
34
  report if enabled?(configuration)
31
35
  end
@@ -62,7 +66,7 @@ module DatabaseConsistency
62
66
  raise NotImplementedError
63
67
  end
64
68
 
65
- # @return [Hash]
69
+ # @return [OpenStruct]
66
70
  def report_template(status, message = nil)
67
71
  OpenStruct.new(
68
72
  checker_name: checker_name,
@@ -45,6 +45,7 @@ module DatabaseConsistency
45
45
  .map(&:strip)
46
46
  .map { |str| str.gsub(/lower\(/i, 'lower(') }
47
47
  .map { |str| str.gsub(/\(([^)]+)\)::\w+/, '\1') }
48
+ .map { |str| str.gsub(/'([^)]+)'::\w+/, '\1') }
48
49
  end
49
50
 
50
51
  def index_columns
@@ -14,7 +14,7 @@ module DatabaseConsistency
14
14
 
15
15
  # @return [String]
16
16
  def convert
17
- TYPES[type]
17
+ TYPES[type] || type
18
18
  end
19
19
  end
20
20
  end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DatabaseConsistency
4
+ module Errors
5
+ # The base error class
6
+ class Base < StandardError; end
7
+
8
+ # The error class for missing field
9
+ class MissingField < Base; end
10
+ end
11
+ end
@@ -8,7 +8,7 @@ module DatabaseConsistency
8
8
  # Returns list of models to check
9
9
  def models
10
10
  ActiveRecord::Base.descendants.delete_if(&:abstract_class?).delete_if do |klass|
11
- !klass.connection.table_exists?(klass.table_name)
11
+ !klass.connection.table_exists?(klass.table_name) || klass.name.include?('HABTM_')
12
12
  end
13
13
  end
14
14
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DatabaseConsistency
4
- VERSION = '0.8.5'
4
+ VERSION = '0.8.10'
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: 0.8.5
4
+ version: 0.8.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evgeniy Demin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-09-21 00:00:00.000000000 Z
11
+ date: 2020-11-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -149,6 +149,7 @@ files:
149
149
  - lib/database_consistency/databases/factory.rb
150
150
  - lib/database_consistency/databases/types/base.rb
151
151
  - lib/database_consistency/databases/types/sqlite.rb
152
+ - lib/database_consistency/errors.rb
152
153
  - lib/database_consistency/helper.rb
153
154
  - lib/database_consistency/processors/associations_processor.rb
154
155
  - lib/database_consistency/processors/base_processor.rb