database_consistency 1.7.17 → 1.7.19

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: 8218744cf4be81d5348c01ffbb18c545b0c98db3a864575de8046b0c15d43002
4
- data.tar.gz: 6a311b65d72845d0bb746d6d93c5f8850e2056202b042a015c4ea900f30c70f4
3
+ metadata.gz: 43676fecc34589198204d39c8f3e1b1a46b6a616fb5c226ee368a009e84cab24
4
+ data.tar.gz: 53980d4481038860da1dd9c673d83b54b4cd5eddd2e98224a0772217dae66d10
5
5
  SHA512:
6
- metadata.gz: 85832321274f953b83db7ca119b07bd812c983adeabf3ce9be69291bd8cbb29afef6630a9e90599be82f40f68538e878f599cdfbdd19e705dd38bba0ee5fd54d
7
- data.tar.gz: a5738e4667fabca642f76d0be5dff2618bede457681d6b26cbf16f2b0fdb63fa526c068baf87026d0773c476bcd442a3de70fd119fce9f68e9c60ff2e7bb0fad
6
+ metadata.gz: ff3b416001fee6e744e60ae1a199e74ce7fc4f5e14259cfac8114dab4246d4ca869e545dfe7c3ebee31efc5346c954736fa38606035b7526d7fbe0095aeff152
7
+ data.tar.gz: 8daea1d6b71e9ec2c2a8089a22bce987b1d0e3237cc4d61130cb59fb721d3f0b39b2f8ee094f489ccab5349c6c6ffbf9832be3d40062ade2b03dce7faafd054f
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DatabaseConsistency
4
+ module Checkers
5
+ # This class checks that primary column type is "uuid" and the model class defines `self.implicit_order_column`
6
+ class ImplicitOrderingChecker < ColumnChecker
7
+ Report = ReportBuilder.define(DatabaseConsistency::Report)
8
+
9
+ private
10
+
11
+ TARGET_COLUMN_TYPE = 'uuid'
12
+
13
+ # We skip check when:
14
+ # - adapter is not PostgreSQL
15
+ # - column is not a primary key
16
+ # - column type is not "uuid"
17
+ def preconditions
18
+ ActiveRecord::VERSION::MAJOR >= 6 &&
19
+ Helper.postgresql? &&
20
+ primary_field? &&
21
+ column.sql_type.to_s.match(TARGET_COLUMN_TYPE)
22
+ end
23
+
24
+ # Table of possible statuses
25
+ # | defined `self.implicit_order_column` | status |
26
+ # | ----------------------------------- | ------ |
27
+ # | yes | ok |
28
+ # | no | fail |
29
+ def check
30
+ if implicit_order_column_defined?
31
+ report_template(:ok)
32
+ else
33
+ report_template(:fail, error_slug: :implicit_order_column_missing)
34
+ end
35
+ end
36
+
37
+ # @return [Boolean]
38
+ def primary_field?
39
+ column.name.to_s == model.primary_key.to_s
40
+ end
41
+
42
+ # @return [Boolean]
43
+ def implicit_order_column_defined?
44
+ model.implicit_order_column.present?
45
+ end
46
+ end
47
+ end
48
+ end
@@ -31,19 +31,24 @@ module DatabaseConsistency
31
31
 
32
32
  def project_models
33
33
  ActiveRecord::Base.descendants.select do |klass|
34
- project_klass?(klass)
34
+ project_klass?(klass) && connected?(klass)
35
35
  end
36
36
  end
37
37
 
38
38
  # Returns list of models to check
39
39
  def models
40
40
  project_models.select do |klass|
41
- !klass.abstract_class? &&
42
- klass.connection.table_exists?(klass.table_name) &&
43
- !klass.name.include?('HABTM_')
41
+ !klass.abstract_class? && !klass.name.include?('HABTM_')
44
42
  end
45
43
  end
46
44
 
45
+ def connected?(klass)
46
+ klass.connection
47
+ rescue ActiveRecord::ConnectionNotEstablished
48
+ puts "#{klass} doesn't have active connection: ignoring"
49
+ false
50
+ end
51
+
47
52
  # Return list of not inherited models
48
53
  def parent_models
49
54
  models.group_by(&:table_name).each_value.flat_map do |models|
@@ -9,7 +9,8 @@ module DatabaseConsistency
9
9
  Checkers::LengthConstraintChecker,
10
10
  Checkers::PrimaryKeyTypeChecker,
11
11
  Checkers::EnumValueChecker,
12
- Checkers::ThreeStateBooleanChecker
12
+ Checkers::ThreeStateBooleanChecker,
13
+ Checkers::ImplicitOrderingChecker
13
14
  ].freeze
14
15
 
15
16
  private
@@ -7,7 +7,7 @@ module DatabaseConsistency
7
7
  attr_reader(*attrs)
8
8
 
9
9
  class_eval(<<~DEF, __FILE__, __LINE__ + 1)
10
- def initialize(#{attrs.map { |attr| "#{attr}:" }.join(', ')}, **opts)
10
+ def initialize(#{attrs.map { |attr| "#{attr}:" }.join(', ')}#{"," if attrs.any?} **opts)
11
11
  super(**opts) if opts.any?
12
12
  #{attrs.map { |attr| "@#{attr} = #{attr}" }.join("\n")}
13
13
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DatabaseConsistency
4
- VERSION = '1.7.17'
4
+ VERSION = '1.7.19'
5
5
  end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DatabaseConsistency
4
+ module Writers
5
+ module Simple
6
+ class ImplicitOrderColumnMissing < Base # :nodoc:
7
+ private
8
+
9
+ def template
10
+ 'implicit_order_column is recommended when using uuid column type for primary key'
11
+ end
12
+
13
+ def unique_attributes
14
+ {
15
+ table_or_model_name: report.table_or_model_name,
16
+ column_or_attribute_name: report.column_or_attribute_name
17
+ }
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -12,6 +12,7 @@ module DatabaseConsistency
12
12
  enum_values_inconsistent_with_ar_enum: Simple::EnumValuesInconsistentWithArEnum,
13
13
  enum_values_inconsistent_with_inclusion: Simple::EnumValuesInconsistentWithInclusion,
14
14
  has_one_missing_unique_index: Simple::HasOneMissingUniqueIndex,
15
+ implicit_order_column_missing: Simple::ImplicitOrderColumnMissing,
15
16
  inconsistent_enum_type: Simple::InconsistentEnumType,
16
17
  inconsistent_types: Simple::InconsistentTypes,
17
18
  length_validator_greater_limit: Simple::LengthValidatorGreaterLimit,
@@ -42,6 +42,7 @@ require 'database_consistency/writers/simple/redundant_case_insensitive_option'
42
42
  require 'database_consistency/writers/simple/three_state_boolean'
43
43
  require 'database_consistency/writers/simple/missing_association_class'
44
44
  require 'database_consistency/writers/simple/missing_table'
45
+ require 'database_consistency/writers/simple/implicit_order_column_missing'
45
46
  require 'database_consistency/writers/simple_writer'
46
47
 
47
48
  require 'database_consistency/writers/autofix/helpers/migration'
@@ -80,6 +81,7 @@ require 'database_consistency/checkers/column_checkers/length_constraint_checker
80
81
  require 'database_consistency/checkers/column_checkers/primary_key_type_checker'
81
82
  require 'database_consistency/checkers/column_checkers/enum_value_checker'
82
83
  require 'database_consistency/checkers/column_checkers/three_state_boolean_checker'
84
+ require 'database_consistency/checkers/column_checkers/implicit_ordering_checker.rb'
83
85
 
84
86
  require 'database_consistency/checkers/validator_checkers/validator_checker'
85
87
  require 'database_consistency/checkers/validator_checkers/missing_unique_index_checker'
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.7.17
4
+ version: 1.7.19
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evgeniy Demin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-07-16 00:00:00.000000000 Z
11
+ date: 2023-08-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -155,6 +155,7 @@ files:
155
155
  - lib/database_consistency/checkers/base_checker.rb
156
156
  - lib/database_consistency/checkers/column_checkers/column_checker.rb
157
157
  - lib/database_consistency/checkers/column_checkers/enum_value_checker.rb
158
+ - lib/database_consistency/checkers/column_checkers/implicit_ordering_checker.rb
158
159
  - lib/database_consistency/checkers/column_checkers/length_constraint_checker.rb
159
160
  - lib/database_consistency/checkers/column_checkers/null_constraint_checker.rb
160
161
  - lib/database_consistency/checkers/column_checkers/primary_key_type_checker.rb
@@ -217,6 +218,7 @@ files:
217
218
  - lib/database_consistency/writers/simple/enum_values_inconsistent_with_ar_enum.rb
218
219
  - lib/database_consistency/writers/simple/enum_values_inconsistent_with_inclusion.rb
219
220
  - lib/database_consistency/writers/simple/has_one_missing_unique_index.rb
221
+ - lib/database_consistency/writers/simple/implicit_order_column_missing.rb
220
222
  - lib/database_consistency/writers/simple/inconsistent_enum_type.rb
221
223
  - lib/database_consistency/writers/simple/inconsistent_types.rb
222
224
  - lib/database_consistency/writers/simple/length_validator_greater_limit.rb