database_consistency 0.6.7 → 0.6.8

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: 70ba9a02302f513c147cd81271d10fbb361b8d8dcb5d578c8c564adb72f85092
4
- data.tar.gz: 1f1bb5d6af4ba890576f56196d40a3214aca8cbbca5fc854716db9c42f048dec
3
+ metadata.gz: 73fc6eabb9e2451dad6a2f238102f9eea3ee79ae7561c05649599e12301cacf3
4
+ data.tar.gz: f6251f161e8c5b2ce232c6632eb1176b9333d74b23c91a421ef21c9622ff85de
5
5
  SHA512:
6
- metadata.gz: 3d5a79bf854a548cde49ba478d0ce25b9db0653a7dc726850c0dd39856da33eaaa3c382a01b360a8b3fbba865b59871fd097e2547989da42a899e035dfe04d43
7
- data.tar.gz: 1bec8cabd5ee6380b74115856ed9956974048f43e0480e8e614f5d1391e1a14c03837b7b53b9df5ae92e5db50a666eb45850ea0a778a7cee9d993fa26201e148
6
+ metadata.gz: a5417be3a2afe6072884395e6fd54e3c9b4134f953d052d8c83b6cad8652c78f7db39c42a5b698cd6e1f6b7fab3cf8f0ae6316748e9eb1a816d684d57b6fbf17
7
+ data.tar.gz: c7844e3f18f15768b85e75c121328301b7f0d78e145c52485f1d27c577d35f531fffdbb078e6c33e2f35dca736849be18a11c4a52708a6092c954731ebdea033
@@ -22,10 +22,6 @@ Rails.application.eager_load! if defined?(Rails)
22
22
  $LOAD_PATH.unshift(File.expand_path('lib', __dir__))
23
23
  require 'database_consistency'
24
24
 
25
- # Welcome message
26
- puts 'Thank you for using the gem. Any contribution is welcome https://github.com/djezzzl/database_consistency!'
27
- puts "(c) Evgeniy Demin <lawliet.djez@gmail.com>\n\n"
28
-
29
25
  # Process checks
30
26
  result = DatabaseConsistency.run
31
27
  exit result
@@ -35,7 +35,7 @@ module DatabaseConsistency
35
35
 
36
36
  Writers::SimpleWriter.write(
37
37
  reports,
38
- ENV['LOG_LEVEL'] || 'INFO'
38
+ config: configuration
39
39
  )
40
40
 
41
41
  reports.empty? ? 0 : 1
@@ -34,9 +34,19 @@ module DatabaseConsistency
34
34
 
35
35
  def index
36
36
  @index ||= association.klass.connection.indexes(association.klass.table_name).find do |index|
37
- index.columns[0] == association.foreign_key.to_s
37
+ index_keys(index) == association_keys
38
38
  end
39
39
  end
40
+
41
+ def association_keys
42
+ @association_keys ||= [association.foreign_key, association.type].compact.map(&:to_s).sort
43
+ end
44
+
45
+ def index_keys(index)
46
+ return unless index.columns.is_a?(Array)
47
+
48
+ index.columns[0...association_keys.size].sort
49
+ end
40
50
  end
41
51
  end
42
52
  end
@@ -16,6 +16,18 @@ module DatabaseConsistency
16
16
  end
17
17
  end
18
18
 
19
+ def debug?
20
+ log_level.to_s.match?(/DEBUG/i)
21
+ end
22
+
23
+ def colored?
24
+ if ENV.key?('COLOR')
25
+ ENV['COLOR'].match?(/1|true|yes/)
26
+ else
27
+ settings && settings['color']
28
+ end
29
+ end
30
+
19
31
  # @return [Boolean]
20
32
  def enabled?(*path)
21
33
  current = configuration
@@ -35,5 +47,18 @@ module DatabaseConsistency
35
47
  private
36
48
 
37
49
  attr_reader :configuration
50
+
51
+ def settings
52
+ @settings ||= configuration['DatabaseConsistencySettings']
53
+ end
54
+
55
+ def log_level
56
+ @log_level ||=
57
+ if ENV.key?('LOG_LEVEL')
58
+ ENV['LOG_LEVEL']
59
+ else
60
+ settings && settings['log_level']
61
+ end
62
+ end
38
63
  end
39
64
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DatabaseConsistency
4
- VERSION = '0.6.7'
4
+ VERSION = '0.6.8'
5
5
  end
@@ -4,23 +4,19 @@ module DatabaseConsistency
4
4
  module Writers
5
5
  # The base class for writers
6
6
  class BaseWriter
7
- attr_reader :results, :log_level
7
+ attr_reader :results, :config
8
8
 
9
- def initialize(results, log_level)
9
+ def initialize(results, config: Configuration.new)
10
10
  @results = results
11
- @log_level = log_level
11
+ @config = config
12
12
  end
13
13
 
14
14
  def write?(status)
15
- status == :fail || debug?
15
+ status == :fail || config.debug?
16
16
  end
17
17
 
18
- def debug?
19
- log_level == 'DEBUG'
20
- end
21
-
22
- def self.write(results, log_level)
23
- new(results, log_level).write
18
+ def self.write(results, config: Configuration.new)
19
+ new(results, config: config).write
24
20
  end
25
21
  end
26
22
  end
@@ -5,22 +5,46 @@ module DatabaseConsistency
5
5
  module Writers
6
6
  # The simplest formatter
7
7
  class SimpleWriter < BaseWriter
8
+ COLORS = {
9
+ blue: "\e[34m",
10
+ yellow: "\e[33m",
11
+ green: "\e[32m",
12
+ red: "\e[31m"
13
+ }.freeze
14
+
8
15
  def write
9
- puts format unless format.empty?
16
+ results.each do |result|
17
+ next unless write?(result.status)
18
+
19
+ puts msg(result)
20
+ end
21
+ end
22
+
23
+ def msg(result)
24
+ msg = "#{status_text(result)} #{key_text(result)} #{result.message}"
25
+ msg += " (checker: #{result.checker_name})" if config.debug?
26
+ msg
10
27
  end
11
28
 
12
- def format
13
- results.map do |result|
14
- next unless write?(result[:status])
29
+ private
15
30
 
16
- line(result)
17
- end.tap(&:compact!).map(&:lstrip).delete_if(&:empty?).join("\n")
31
+ def key_text(result)
32
+ "#{colorize(result.table_or_model_name, :blue)} #{colorize(result.column_or_attribute_name, :yellow)}"
18
33
  end
19
34
 
20
- def line(result)
21
- s = "#{result.status} #{result.table_or_model_name} #{result.column_or_attribute_name} #{result.message}"
22
- s += " (checker: #{result.checker_name})" if debug?
23
- s
35
+ def status_text(result)
36
+ color = case result.status
37
+ when :ok then :green
38
+ when :fail then :red
39
+ end
40
+
41
+ colorize(result.status, color)
42
+ end
43
+
44
+ def colorize(text, color)
45
+ return text unless config.colored? && color
46
+
47
+ "#{COLORS[color]}#{text}\e[0m"
24
48
  end
25
49
  end
26
50
  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.6.7
4
+ version: 0.6.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evgeniy Demin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-05-01 00:00:00.000000000 Z
11
+ date: 2019-05-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord