database_consistency 0.7.8 → 0.8.3

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: d3b93b80d81b10da730dbaf8cc523325150103fcae0f18ea4092baa03d8fb8ba
4
- data.tar.gz: 67a8a38c922d359f11699b703a340489a866e573b8714f1848bfee7900ecb280
3
+ metadata.gz: 5015849cbfceb5d46a68ab0c3f9ba0e620fffbf013c16af563d3aa8fd16b140f
4
+ data.tar.gz: dd379822c590457db98fd9f23fe99f19868063cf994e804eec11fe61b7311105
5
5
  SHA512:
6
- metadata.gz: 187968d380947ce829f765bac635720b959346c53b3ef81a99eadb1555395767d457fcbb5fd325010656b418867d1b916170cf0bc5cf265e69c9228e61d1a01e
7
- data.tar.gz: 724d34742874b0b1f6e864dabe98624702409a26d39467292d64490d0757e4e4f7bfe428d0b6e9bf629ccac99abaaf1ba623cf674e1fd00215df5f39c200a512
6
+ metadata.gz: 3ec317d549f0852e6396db14f7263904448a3dda2c2515f83a174946a9d8138163f909b078a235a41c5d36daa01bbc38cd61c03f23921d3ad8704c7742e7fcec
7
+ data.tar.gz: 8c41bdf53040283417a3d616e9cf9b13a34138ba17edf65b84df1506e630e758d88995a618ea2b111a8d45884c49bf8b4e5f9b2c38e16fbfdd379328260aa0f1
@@ -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
 
@@ -25,7 +25,8 @@ module DatabaseConsistency
25
25
  # | provided | ok |
26
26
  # | missing | fail |
27
27
  #
28
- # We consider PresenceValidation, InclusionValidation or BelongsTo association using this column
28
+ # We consider PresenceValidation, InclusionValidation, ExclusionValidation, NumericalityValidator with nil,
29
+ # or BelongsTo association using this column
29
30
  def check
30
31
  if valid?
31
32
  report_template(:ok)
@@ -37,6 +38,8 @@ module DatabaseConsistency
37
38
  def valid?
38
39
  validator?(ActiveModel::Validations::PresenceValidator) ||
39
40
  validator?(ActiveModel::Validations::InclusionValidator) ||
41
+ numericality_validator_without_allow_nil? ||
42
+ nil_exclusion_validator? ||
40
43
  belongs_to_association?
41
44
  end
42
45
 
@@ -48,6 +51,20 @@ module DatabaseConsistency
48
51
  model.record_timestamps? && %w[created_at updated_at].include?(column.name)
49
52
  end
50
53
 
54
+ def nil_exclusion_validator?
55
+ model.validators.grep(ActiveModel::Validations::ExclusionValidator).any? do |validator|
56
+ Helper.check_inclusion?(validator.attributes, column.name) &&
57
+ validator.options[:in].include?(nil)
58
+ end
59
+ end
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
+
51
68
  def validator?(validator_class)
52
69
  model.validators.grep(validator_class).any? do |validator|
53
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.8'
4
+ VERSION = '0.8.3'
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.8
4
+ version: 0.8.3
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-02 00:00:00.000000000 Z
11
+ date: 2020-06-24 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.