16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/schema_sherlock/commands/analyze_command.rb', line 16
def analyze(model_name = nil)
load_rails_environment
if options[:min_usage]
original_threshold = SchemaSherlock.configuration.min_usage_threshold
SchemaSherlock.configuration.min_usage_threshold = options[:min_usage]
end
models = model_name ? [find_model(model_name)] : all_models
puts "Analyzing #{models.length} model(s)..."
@binary_index = nil
if options[:use_index] && defined?(Rails) && Rails.root
puts "Loading binary index..."
@binary_index = BinaryIndex.load_or_build(Rails.root.to_s)
if @binary_index
puts " Index loaded with #{@binary_index[:files].size} files indexed"
end
end
puts "Preloading database metadata..."
cache_stats = SchemaCache.preload_all_metadata
puts " Cached: #{cache_stats[:tables_cached]} tables, #{cache_stats[:columns_cached]} column sets, #{cache_stats[:indexes_cached]} index sets"
if SchemaSherlock.configuration.min_usage_threshold && SchemaSherlock.configuration.min_usage_threshold > 0
if @binary_index
puts "Using binary index for file analysis (skipping file cache preload)"
else
puts "Preloading file cache..."
file_stats = FileCache.preload_all_files
puts " Cached: #{file_stats[:files_scanned]} files (#{(file_stats[:total_size] / 1024.0 / 1024.0).round(2)} MB), #{file_stats[:files_failed]} failed"
end
end
results = {}
models.each do |model|
puts " Analyzing #{model.name}..."
UsageTracker.binary_index = @binary_index
analysis = analyze_model(model)
if has_issues?(analysis)
results[model.name] = analysis
end
end
display_results(results, models.length)
save_results(results) if options[:output]
rescue SchemaSherlock::Error => e
say e.message, :red
exit 1
ensure
SchemaCache.clear_cache
FileCache.clear_cache
if options[:min_usage] && defined?(original_threshold)
SchemaSherlock.configuration.min_usage_threshold = original_threshold
end
end
|