database_consistency 1.1.14 → 1.2.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: 86037a5780e8f5f906a5232a9925b5e250177b6ebe653a81509167814dc87a2e
4
- data.tar.gz: 91cafa728a48dd765a2b66fc41c9a2a4f2952d72737472c6f1f88af28d9053b8
3
+ metadata.gz: 5774c6b4064a081e7a285013456b39fef68f39ec7dd536063c236d54abec3e84
4
+ data.tar.gz: 18ed6073af882e007ec6ea70a9a55fad8daa8fef755049a3070d9f68ce1377dd
5
5
  SHA512:
6
- metadata.gz: d6ac7dbf5de3cc1cbfaa7aef08e86aad7c88b2e5734283ef130ab60582945312faf87828b949d6f2f2e551b2770faca8498ad70e6438bcc6416977df20441aa3
7
- data.tar.gz: f31f3a0ad600d45e7fcfe6f9d9aad07a267dfc96de990e03f7e725e9644f09d95ce8b6f0c69de920676e7023a5cf2dc7058dce2506f0ead2f23ee543b2596e8f
6
+ metadata.gz: 575b90af564642b397b5a93d2c5a94a3b3b2fb8f6de12e40225361ebb96128df61a8d3e19fb4982986985c6bd1ae110539cc93f00688bab3b44205f7aa5f3716
7
+ data.tar.gz: 051360a87facb2062451b17c59ff596156b0211dfbf73d6e3345c1b45d470f45177c280d6dd6fb59602faa719a959534252dc4f9615378724d2a321d39c64394
@@ -24,17 +24,22 @@ end
24
24
  require 'optparse'
25
25
 
26
26
  config = [default_config]
27
+ options = {}
27
28
  opt_parser = OptionParser.new do |opts|
28
29
  opts.banner = <<-DESC
29
30
  Usage: database_consistency install - run installation
30
31
  database_consistency [options]
31
32
  DESC
32
33
 
33
- opts.on('-cFILE', '--config=FILE', 'Use additional configuration file') do |f|
34
+ opts.on('-cFILE', '--config=FILE', 'Use additional configuration file.') do |f|
34
35
  config << f
35
36
  end
36
37
 
37
- opts.on('-h', '--help', 'Prints this help') do
38
+ opts.on('-g', '--generate-todo', 'Generate TODO file with every failing check disabled. You can pass existing configurations so the generated file will have only new failures.') do |f|
39
+ options[:todo] = true
40
+ end
41
+
42
+ opts.on('-h', '--help', 'Prints this help.') do
38
43
  puts opts
39
44
  exit
40
45
  end
@@ -66,5 +71,5 @@ $LOAD_PATH.unshift(File.expand_path('lib', __dir__))
66
71
  require 'database_consistency'
67
72
 
68
73
  # Process checks
69
- code = DatabaseConsistency.run(config)
74
+ code = DatabaseConsistency.run(config, **options)
70
75
  exit code
@@ -7,6 +7,7 @@ module DatabaseConsistency
7
7
  attr_reader :model, :association
8
8
 
9
9
  def initialize(model, association)
10
+ super()
10
11
  @model = model
11
12
  @association = association
12
13
  end
@@ -19,7 +19,7 @@ module DatabaseConsistency
19
19
  # @param [Boolean] catch_errors
20
20
  #
21
21
  # @return [Hash, File, nil]
22
- def report(catch_errors = true)
22
+ def report(catch_errors: true)
23
23
  return unless preconditions
24
24
 
25
25
  @report ||= check
@@ -7,6 +7,7 @@ module DatabaseConsistency
7
7
  attr_reader :model, :column
8
8
 
9
9
  def initialize(model, column)
10
+ super()
10
11
  @model = model
11
12
  @column = column
12
13
  end
@@ -7,6 +7,7 @@ module DatabaseConsistency
7
7
  attr_reader :model, :index
8
8
 
9
9
  def initialize(model, index)
10
+ super()
10
11
  @model = model
11
12
  @index = index
12
13
  end
@@ -7,6 +7,7 @@ module DatabaseConsistency
7
7
  attr_reader :model, :attribute, :validator
8
8
 
9
9
  def initialize(model, attribute, validator)
10
+ super()
10
11
  @model = model
11
12
  @attribute = attribute
12
13
  @validator = validator
@@ -7,6 +7,7 @@ module DatabaseConsistency
7
7
  attr_reader :model, :attribute, :validators
8
8
 
9
9
  def initialize(model, attribute, validators)
10
+ super()
10
11
  @model = model
11
12
  @attribute = attribute
12
13
  @validators = validators.select(&method(:filter))
@@ -37,22 +37,30 @@ module DatabaseConsistency
37
37
  def enabled?(*path)
38
38
  current = configuration
39
39
 
40
+ value = global_enabling
41
+
40
42
  path.each do |key|
41
43
  current = current[key.to_s]
42
- return true unless current.is_a?(Hash)
44
+ return value unless current.is_a?(Hash)
43
45
 
44
46
  next if current['enabled'].nil?
45
47
 
46
- return false unless current['enabled']
48
+ value = current['enabled']
47
49
  end
48
50
 
49
- true
51
+ value
50
52
  end
51
53
 
52
54
  private
53
55
 
54
56
  attr_reader :configuration
55
57
 
58
+ def global_enabling
59
+ value = configuration.dig('DatabaseConsistencyCheckers', 'All', 'enabled')
60
+
61
+ value.nil? ? true : value
62
+ end
63
+
56
64
  def load_yaml_config_file(filepath)
57
65
  if YAML.respond_to?(:safe_load_file)
58
66
  YAML.safe_load_file(filepath, aliases: true)
@@ -14,7 +14,7 @@ module DatabaseConsistency
14
14
 
15
15
  # @param [String] type
16
16
  def initialize(type)
17
- @type = type
17
+ @type = type.downcase
18
18
  end
19
19
 
20
20
  # @return [String]
@@ -23,8 +23,10 @@ module DatabaseConsistency
23
23
 
24
24
  # Returns list of models to check
25
25
  def models
26
- ActiveRecord::Base.descendants.delete_if(&:abstract_class?).delete_if do |klass|
27
- !klass.connection.table_exists?(klass.table_name) || klass.name.include?('HABTM_')
26
+ ActiveRecord::Base.descendants.delete_if(&:abstract_class?).select do |klass|
27
+ klass.connection.table_exists?(klass.table_name) &&
28
+ !klass.name.include?('HABTM_') &&
29
+ project_klass?(klass)
28
30
  end
29
31
  end
30
32
 
@@ -35,6 +37,15 @@ module DatabaseConsistency
35
37
  end
36
38
  end
37
39
 
40
+ # @param klass [ActiveRecord::Base]
41
+ #
42
+ # @return [Boolean]
43
+ def project_klass?(klass)
44
+ return true unless Module.respond_to?(:const_source_location) && defined?(Bundler)
45
+
46
+ !Module.const_source_location(klass.to_s).first.to_s.include?(Bundler.bundle_path.to_s)
47
+ end
48
+
38
49
  # @return [Boolean]
39
50
  def check_inclusion?(array, element)
40
51
  array.include?(element.to_s) || array.include?(element.to_sym)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DatabaseConsistency
4
- VERSION = '1.1.14'
4
+ VERSION = '1.2.1'
5
5
  end
@@ -12,6 +12,12 @@ module DatabaseConsistency
12
12
  red: "\e[31m"
13
13
  }.freeze
14
14
 
15
+ COLOR_BY_STATUS = {
16
+ ok: :green,
17
+ warning: :yellow,
18
+ fail: :red
19
+ }.freeze
20
+
15
21
  def write
16
22
  results.each do |result|
17
23
  next unless write?(result.status)
@@ -31,11 +37,7 @@ module DatabaseConsistency
31
37
  end
32
38
 
33
39
  def status_text(result)
34
- color = case result.status
35
- when :ok then :green
36
- when :warning then :yellow
37
- when :fail then :red
38
- end
40
+ color = COLOR_BY_STATUS[result.status]
39
41
 
40
42
  colorize(result.status, color)
41
43
  end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DatabaseConsistency
4
+ # The module contains formatters
5
+ module Writers
6
+ # The writer that generates to-do file
7
+ class TodoWriter < BaseWriter
8
+ def write
9
+ hash = results.each_with_object({}) do |result, hash|
10
+ next unless write?(result.status)
11
+
12
+ assign_result(hash, result)
13
+ end
14
+
15
+ File.write(file_name, hash.to_yaml)
16
+ end
17
+
18
+ def write?(status)
19
+ status == :fail
20
+ end
21
+
22
+ private
23
+
24
+ def assign_result(hash, result)
25
+ hash[result.table_or_model_name] ||= {}
26
+ hash[result.table_or_model_name][result.column_or_attribute_name] ||= {}
27
+ hash[result.table_or_model_name][result.column_or_attribute_name][result.checker_name] = {'enabled' => false}
28
+ end
29
+
30
+ def file_name
31
+ [nil, *(1..100)].each do |number|
32
+ name = generate_file_name(number)
33
+
34
+ return name unless File.exists?(name)
35
+ end
36
+ end
37
+
38
+ def generate_file_name(number = nil)
39
+ ".database_consistency.todo#{number}.yml"
40
+ end
41
+ end
42
+ end
43
+ end
@@ -10,6 +10,7 @@ require 'database_consistency/errors'
10
10
 
11
11
  require 'database_consistency/writers/base_writer'
12
12
  require 'database_consistency/writers/simple_writer'
13
+ require 'database_consistency/writers/todo_writer'
13
14
 
14
15
  require 'database_consistency/databases/factory'
15
16
  require 'database_consistency/databases/types/base'
@@ -48,16 +49,19 @@ require 'database_consistency/processors/indexes_processor'
48
49
  # The root module
49
50
  module DatabaseConsistency
50
51
  class << self
51
- def run(*args)
52
+ def run(*args, **opts)
52
53
  configuration = Configuration.new(*args)
53
54
  reports = Processors.reports(configuration)
54
55
 
55
- Writers::SimpleWriter.write(
56
- reports,
57
- config: configuration
58
- )
56
+ if opts[:todo]
57
+ Writers::TodoWriter.write(reports, config: configuration)
59
58
 
60
- reports.any? { |report| report.status == :fail } || !RescueError.empty? ? 1 : 0
59
+ 0
60
+ else
61
+ Writers::SimpleWriter.write(reports, config: configuration)
62
+
63
+ reports.any? { |report| report.status == :fail } || !RescueError.empty? ? 1 : 0
64
+ end
61
65
  end
62
66
  end
63
67
  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: 1.1.14
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evgeniy Demin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-04-28 00:00:00.000000000 Z
11
+ date: 2022-09-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -180,6 +180,7 @@ files:
180
180
  - lib/database_consistency/version.rb
181
181
  - lib/database_consistency/writers/base_writer.rb
182
182
  - lib/database_consistency/writers/simple_writer.rb
183
+ - lib/database_consistency/writers/todo_writer.rb
183
184
  homepage: https://github.com/djezzzl/database_consistency
184
185
  licenses:
185
186
  - MIT
@@ -199,7 +200,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
199
200
  - !ruby/object:Gem::Version
200
201
  version: '0'
201
202
  requirements: []
202
- rubygems_version: 3.0.3.1
203
+ rubygems_version: 3.3.21
203
204
  signing_key:
204
205
  specification_version: 4
205
206
  summary: Provide an easy way to check the consistency of the database constraints