Skip to content

Commit 9fa0ac3

Browse files
committedNov 21, 2018
Runs for specified models only
1 parent 5f484e7 commit 9fa0ac3

File tree

6 files changed

+96
-37
lines changed

6 files changed

+96
-37
lines changed
 

‎.rubocop.yml

+3
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,6 @@ Metrics/LineLength:
1616
Metrics/BlockLength:
1717
Exclude:
1818
- 'spec/**/*'
19+
20+
RSpec/MultipleExpectations:
21+
Max: 3

‎README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,14 @@ Accordance/TablePresence: Stat::Hourly has no underlying table hourly_stats
3131

3232
## Options
3333

34-
Check only specified database rules:
34+
Check only specified database rules with `--only` options:
3535
```
3636
bundle exec data_integrity --only HasMany/ForeignKey,BelongsTo/ForeignKey
3737
```
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+
```
3842

3943
## Supported Issues
4044

‎lib/active_record/data_integrity/cli.rb

+7-16
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
# frozen_string_literal: true
22

33
require 'thor'
4-
require_relative 'options'
4+
require_relative 'config'
55

66
module ActiveRecord
77
module DataIntegrity
88
# CLI application class
99
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'
1311
method_option :only,
1412
type: :string,
1513
desc: 'List of the rules to check, separated with comma' \
1614
', 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+
1918
require_rails
2019

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|
2322
cop_class.new(model).call
2423
end
2524
end
@@ -38,14 +37,6 @@ def version
3837

3938
private
4039

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-
4940
def require_rails
5041
# Rails load ugly hack :)
5142
require File.expand_path('config/environment', Dir.pwd)
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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

‎lib/active_record/data_integrity/options.rb

-20
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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

Comments
 (0)
Failed to load comments.