6 files changed +96
-37
lines changed Original file line number Diff line number Diff line change @@ -16,3 +16,6 @@ Metrics/LineLength:
16
16
Metrics/BlockLength :
17
17
Exclude :
18
18
- ' spec/**/*'
19
+
20
+ RSpec/MultipleExpectations :
21
+ Max : 3
Original file line number Diff line number Diff line change @@ -31,10 +31,14 @@ Accordance/TablePresence: Stat::Hourly has no underlying table hourly_stats
31
31
32
32
## Options
33
33
34
- Check only specified database rules:
34
+ Check only specified database rules with ` --only ` options :
35
35
```
36
36
bundle exec data_integrity --only HasMany/ForeignKey,BelongsTo/ForeignKey
37
37
```
38
+ Check only specified model with the list of the model full with:
39
+ ``` sh
40
+ bundle exec data_integrity --only HasMany/ForeignKye Billing::Account User
41
+ ```
38
42
39
43
## Supported Issues
40
44
Original file line number Diff line number Diff line change 1
1
# frozen_string_literal: true
2
2
3
3
require 'thor'
4
- require_relative 'options '
4
+ require_relative 'config '
5
5
6
6
module ActiveRecord
7
7
module DataIntegrity
8
8
# CLI application class
9
9
class CLI < Thor
10
- attr_reader :options
11
-
12
- desc 'check [OPTIONS]' , 'Runs the data integrity check'
10
+ desc 'check [OPTIONS] [MODEL_NAMES]' , 'Runs the data integrity check'
13
11
method_option :only ,
14
12
type : :string ,
15
13
desc : 'List of the rules to check, separated with comma' \
16
14
', e.g. --only BelongsTo/ForeignKey,Accordance/PrimaryKey'
17
- def check ( _args = ARGV )
18
- @options = Options . new ( options )
15
+ def check ( *model_names )
16
+ config = Config . new ( options . merge ( model_names : model_names ) )
17
+
19
18
require_rails
20
19
21
- results = cops . map do |cop_class |
22
- ActiveRecord :: Base . descendants . each do |model |
20
+ results = config . cops . map do |cop_class |
21
+ config . models . each do |model |
23
22
cop_class . new ( model ) . call
24
23
end
25
24
end
@@ -38,14 +37,6 @@ def version
38
37
39
38
private
40
39
41
- def cops
42
- @cops ||= begin
43
- ActiveRecord ::DataIntegrity ::Cop . descendants . select do |cop |
44
- options . only . empty? || cop . cop_name . in? ( options . only )
45
- end
46
- end
47
- end
48
-
49
40
def require_rails
50
41
# Rails load ugly hack :)
51
42
require File . expand_path ( 'config/environment' , Dir . pwd )
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ require 'optparse'
4
+
5
+ module ActiveRecord
6
+ module DataIntegrity
7
+ # Config parser
8
+ class Config
9
+ attr_reader :only , :model_names
10
+
11
+ def initialize ( options )
12
+ @only = options . only? ? options . only . to_s . split ( ',' ) : [ ]
13
+ @model_names = options . model_names || [ ]
14
+ end
15
+
16
+ def cops
17
+ @cops ||= begin
18
+ ActiveRecord ::DataIntegrity ::Cop . descendants . select do |cop |
19
+ only . empty? || cop . cop_name . in? ( only )
20
+ end
21
+ end
22
+ end
23
+
24
+ def models
25
+ @models ||= begin
26
+ ActiveRecord ::Base . descendants . select do |model |
27
+ model_names . empty? || model . name . in? ( model_names )
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
Load Diff This file was deleted.
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec . describe ActiveRecord ::DataIntegrity ::Config do
6
+ let ( :options ) { Thor ::CoreExt ::HashWithIndifferentAccess . new }
7
+ let ( :config ) { described_class . new ( options ) }
8
+
9
+ before { ActiveRecord ::Base . connection }
10
+
11
+ describe '#cops' do
12
+ context 'when option only is passed' do
13
+ before { options [ :only ] = 'HasMany/ForeignKey' }
14
+
15
+ it do
16
+ expect ( config . cops ) . to include ( ActiveRecord ::DataIntegrity ::HasMany ::ForeignKey )
17
+ expect ( config . cops ) . not_to include ( ActiveRecord ::DataIntegrity ::BelongsTo ::ForeignKey )
18
+ end
19
+ end
20
+
21
+ context 'when option only is not passed' do
22
+ it do
23
+ expect ( config . cops ) . to include ( ActiveRecord ::DataIntegrity ::HasMany ::ForeignKey )
24
+ expect ( config . cops ) . to include ( ActiveRecord ::DataIntegrity ::BelongsTo ::ForeignKey )
25
+ end
26
+ end
27
+ end
28
+
29
+ describe '#models' do
30
+ before { BelongsTo ::User && BelongsTo ::UserSetting }
31
+
32
+ context 'when option model_names is passed' do
33
+ before { options [ :model_names ] = [ 'BelongsTo::User' ] }
34
+
35
+ it do
36
+ expect ( config . models ) . to include ( BelongsTo ::User )
37
+ expect ( config . models ) . not_to include ( BelongsTo ::UserSetting )
38
+ end
39
+ end
40
+
41
+ context 'when option model_names is not passed' do
42
+ it do
43
+ expect ( config . models ) . to include ( BelongsTo ::User )
44
+ expect ( config . models ) . to include ( BelongsTo ::UserSetting )
45
+ end
46
+ end
47
+ end
48
+ end
0 commit comments