database_consistency 0.7.9 → 0.8.4

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: 2d2fdde533c08cebcdc19ded3a2e2b94587304aef759b6a8bee72bddc2ec0ece
4
- data.tar.gz: 5df7c33b8a2cd32686dd3835514bab577eae10cb340709779bca20f942d6de76
3
+ metadata.gz: 0f88361846480fa9257f1a17799b54f60e9e44630b81f3e0612ce9a8bd2ee8ca
4
+ data.tar.gz: bbe851973d91592ab4156cb12da0dd0a3fc33d429c9c5521e3d4be9fc1c3f380
5
5
  SHA512:
6
- metadata.gz: c9de96885025f37decdaf6d8fc566dc9f35da9c9a91500c11504c0202b6e28f9d8fbcd5f8a742fc841b42419dbf7ccdd841066eb144391910948a5fbf637fc22
7
- data.tar.gz: 49088e8975bd3b2795ed97f00802ece5204c11b108bbbfa3eff10f52b1b176762094b8cb6fb414fca7dd1616e384974ca2adc847dee39b1902849bb8ee46c24e
6
+ metadata.gz: 13b9a733d87f4f840afc7e87d284ddea367cf6bbd7bacf10a496dea82d2cc782427eb3bcad4b1926f633dad79f36ccc7afbdc9463c2a68ccde5e07ce76a85019
7
+ data.tar.gz: fbc37a0a3764eea67df7733d4bebd40a5f1f3ddb1019998de08f6e71610350e4b4bf44f9896ea939de013b3ac5bc0630bbbaae8176189e095fbefde0420aee18
@@ -1,9 +1,25 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- base_dir = File.join(Dir.pwd, ARGV.first.to_s)
4
-
5
- unless File.realpath(base_dir).start_with?(Dir.pwd)
6
- puts "\nWarning! You are going out of current directory, ruby version may be wrong and some gems may be missing.\n"
3
+ if ARGV.include?('install')
4
+ require_relative '../lib/database_consistency/configuration'
5
+ config_filename = DatabaseConsistency::Configuration::CONFIGURATION_PATH
6
+ file_exists = File.exists?(config_filename)
7
+ rules = Pathname.new(__FILE__).dirname.join('..', 'lib', 'database_consistency', 'templates', 'rails_defaults.yml').read
8
+ if file_exists && File.foreach(config_filename).grep(Regexp.new(rules.lines.first.chomp)).any?
9
+ puts "#{config_filename} is already present"
10
+ else
11
+ File.open(config_filename, 'a') do |file|
12
+ file << "\n" * 2 if file_exists
13
+ file << rules
14
+ end
15
+ puts "#{config_filename} #{file_exists ? 'updated' : 'added'}"
16
+ end
17
+ exit 0
18
+ else
19
+ base_dir = File.join(Dir.pwd, ARGV.first.to_s)
20
+ unless File.realpath(base_dir).start_with?(Dir.pwd)
21
+ puts "\nWarning! You are going out of current directory, ruby version may be wrong and some gems may be missing.\n"
22
+ end
7
23
  end
8
24
 
9
25
  # Load Rails project
@@ -11,14 +11,19 @@ require 'database_consistency/writers/base_writer'
11
11
  require 'database_consistency/writers/simple_writer'
12
12
 
13
13
  require 'database_consistency/checkers/base_checker'
14
+
14
15
  require 'database_consistency/checkers/association_checkers/association_checker'
15
16
  require 'database_consistency/checkers/association_checkers/missing_index_checker'
17
+
16
18
  require 'database_consistency/checkers/column_checkers/column_checker'
17
19
  require 'database_consistency/checkers/column_checkers/null_constraint_checker'
18
20
  require 'database_consistency/checkers/column_checkers/length_constraint_checker'
21
+ require 'database_consistency/checkers/column_checkers/primary_key_type_checker'
22
+
19
23
  require 'database_consistency/checkers/validator_checkers/validator_checker'
20
24
  require 'database_consistency/checkers/validator_checkers/belongs_to_presence_checker'
21
25
  require 'database_consistency/checkers/validator_checkers/missing_unique_index_checker'
26
+
22
27
  require 'database_consistency/checkers/validators_fraction_checkers/validators_fraction_checker'
23
28
  require 'database_consistency/checkers/validators_fraction_checkers/column_presence_checker'
24
29
 
@@ -21,8 +21,14 @@ module DatabaseConsistency
21
21
  # We skip check when:
22
22
  # - column hasn't limit constraint
23
23
  # - column insn't string nor text
24
+ # - column is array (PostgreSQL only)
24
25
  def preconditions
25
- !column.limit.nil? && %i[string text].include?(column.type)
26
+ !column.limit.nil? && %i[string text].include?(column.type) && !postgresql_array?
27
+ end
28
+
29
+ # @return [Boolean] true if it is an array (PostgreSQL only)
30
+ def postgresql_array?
31
+ column.respond_to?(:array) && column.array
26
32
  end
27
33
 
28
34
  # Table of possible statuses
@@ -25,7 +25,7 @@ module DatabaseConsistency
25
25
  # | provided | ok |
26
26
  # | missing | fail |
27
27
  #
28
- # We consider PresenceValidation, InclusionValidation, ExclusionValidation with nil,
28
+ # We consider PresenceValidation, InclusionValidation, ExclusionValidation, NumericalityValidator with nil,
29
29
  # or BelongsTo association using this column
30
30
  def check
31
31
  if valid?
@@ -38,6 +38,7 @@ module DatabaseConsistency
38
38
  def valid?
39
39
  validator?(ActiveModel::Validations::PresenceValidator) ||
40
40
  validator?(ActiveModel::Validations::InclusionValidator) ||
41
+ numericality_validator_without_allow_nil? ||
41
42
  nil_exclusion_validator? ||
42
43
  belongs_to_association?
43
44
  end
@@ -57,6 +58,13 @@ module DatabaseConsistency
57
58
  end
58
59
  end
59
60
 
61
+ def numericality_validator_without_allow_nil?
62
+ model.validators.grep(ActiveModel::Validations::NumericalityValidator).any? do |validator|
63
+ Helper.check_inclusion?(validator.attributes, column.name) &&
64
+ !validator.options[:allow_nil]
65
+ end
66
+ end
67
+
60
68
  def validator?(validator_class)
61
69
  model.validators.grep(validator_class).any? do |validator|
62
70
  Helper.check_inclusion?(validator.attributes, column.name)
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DatabaseConsistency
4
+ module Checkers
5
+ # This class checks missing presence validator
6
+ class PrimaryKeyTypeChecker < ColumnChecker
7
+ # Message templates
8
+ VALIDATOR_MISSING = 'column has int/serial type but recommended to have bigint/bigserial/uuid'
9
+
10
+ private
11
+
12
+ VALID_TYPES = %w[bigserial bigint uuid].freeze
13
+ SQLITE_ADAPTER_NAME = 'SQLite'
14
+
15
+ # We skip check when:
16
+ # - column is not a primary key
17
+ # - database is SQLite3
18
+ def preconditions
19
+ primary_field? && !sqlite?
20
+ end
21
+
22
+ # Table of possible statuses
23
+ # | bigint/bigserial/uuid | status |
24
+ # | --------------------- | ------ |
25
+ # | yes | ok |
26
+ # | no | fail |
27
+ def check
28
+ if valid?
29
+ report_template(:ok)
30
+ else
31
+ report_template(:fail, VALIDATOR_MISSING)
32
+ end
33
+ end
34
+
35
+ # @return [Boolean]
36
+ def valid?
37
+ VALID_TYPES.any? do |type|
38
+ column.sql_type.to_s.match?(type)
39
+ end
40
+ end
41
+
42
+ # @return [Boolean]
43
+ def primary_field?
44
+ column.name.to_s == model.primary_key.to_s
45
+ end
46
+
47
+ # @return [Boolean]
48
+ def sqlite?
49
+ model.connection.adapter_name == SQLITE_ADAPTER_NAME
50
+ end
51
+ end
52
+ end
53
+ end
@@ -48,7 +48,13 @@ module DatabaseConsistency
48
48
  end
49
49
 
50
50
  def index_columns
51
- @index_columns ||= ([wrapped_attribute_name] + Array.wrap(validator.options[:scope])).map(&:to_s)
51
+ @index_columns ||= ([wrapped_attribute_name] + scope_columns).map(&:to_s)
52
+ end
53
+
54
+ def scope_columns
55
+ @scope_columns ||= Array.wrap(validator.options[:scope]).map do |scope_item|
56
+ model._reflect_on_association(scope_item)&.foreign_key || scope_item
57
+ end
52
58
  end
53
59
 
54
60
  def sorted_index_columns
@@ -4,7 +4,7 @@ module DatabaseConsistency
4
4
  module Checkers
5
5
  # This class checks if presence validator has non-null constraint in the database
6
6
  class ColumnPresenceChecker < ValidatorsFractionChecker
7
- WEAK_OPTIONS = %i[allow_nil allow_blank if unless].freeze
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
10
  POSSIBLE_NULL = 'column is required but there is possible null value insert'
@@ -6,7 +6,8 @@ module DatabaseConsistency
6
6
  class ColumnsProcessor < BaseProcessor
7
7
  CHECKERS = [
8
8
  Checkers::NullConstraintChecker,
9
- Checkers::LengthConstraintChecker
9
+ Checkers::LengthConstraintChecker,
10
+ Checkers::PrimaryKeyTypeChecker
10
11
  ].freeze
11
12
 
12
13
  private
@@ -0,0 +1,7 @@
1
+ # Ignore false positive from Rails' ActionText and ActiveStorage
2
+ ActionText::RichText:
3
+ enabled: false
4
+ ActiveStorage::Attachment:
5
+ enabled: false
6
+ ActiveStorage::Blob:
7
+ enabled: false
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DatabaseConsistency
4
- VERSION = '0.7.9'
4
+ VERSION = '0.8.4'
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.7.9
4
+ version: 0.8.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evgeniy Demin
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-02-20 00:00:00.000000000 Z
11
+ date: 2020-07-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -70,16 +70,16 @@ dependencies:
70
70
  name: rake
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - "~>"
73
+ - - ">="
74
74
  - !ruby/object:Gem::Version
75
- version: '10.0'
75
+ version: 12.3.3
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - "~>"
80
+ - - ">="
81
81
  - !ruby/object:Gem::Version
82
- version: '10.0'
82
+ version: 12.3.3
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: rspec
85
85
  requirement: !ruby/object:Gem::Requirement
@@ -122,7 +122,7 @@ dependencies:
122
122
  - - "~>"
123
123
  - !ruby/object:Gem::Version
124
124
  version: '1.3'
125
- description:
125
+ description:
126
126
  email:
127
127
  - lawliet.djez@gmail.com
128
128
  executables:
@@ -138,6 +138,7 @@ files:
138
138
  - lib/database_consistency/checkers/column_checkers/column_checker.rb
139
139
  - lib/database_consistency/checkers/column_checkers/length_constraint_checker.rb
140
140
  - lib/database_consistency/checkers/column_checkers/null_constraint_checker.rb
141
+ - lib/database_consistency/checkers/column_checkers/primary_key_type_checker.rb
141
142
  - lib/database_consistency/checkers/validator_checkers/belongs_to_presence_checker.rb
142
143
  - lib/database_consistency/checkers/validator_checkers/missing_unique_index_checker.rb
143
144
  - lib/database_consistency/checkers/validator_checkers/validator_checker.rb
@@ -151,6 +152,7 @@ files:
151
152
  - lib/database_consistency/processors/validators_fractions_processor.rb
152
153
  - lib/database_consistency/processors/validators_processor.rb
153
154
  - lib/database_consistency/rescue_error.rb
155
+ - lib/database_consistency/templates/rails_defaults.yml
154
156
  - lib/database_consistency/version.rb
155
157
  - lib/database_consistency/writers/base_writer.rb
156
158
  - lib/database_consistency/writers/simple_writer.rb
@@ -158,7 +160,7 @@ homepage: https://github.com/djezzzl/database_consistency
158
160
  licenses:
159
161
  - MIT
160
162
  metadata: {}
161
- post_install_message:
163
+ post_install_message:
162
164
  rdoc_options: []
163
165
  require_paths:
164
166
  - lib
@@ -173,9 +175,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
173
175
  - !ruby/object:Gem::Version
174
176
  version: '0'
175
177
  requirements: []
176
- rubyforge_project:
177
- rubygems_version: 2.7.9
178
- signing_key:
178
+ rubygems_version: 3.1.2
179
+ signing_key:
179
180
  specification_version: 4
180
181
  summary: Provide an easy way to check the consistency of the database constraints
181
182
  with the application validations.