Class: Exwiw::SchemaCheck

Inherits:
Object
  • Object
show all
Defined in:
lib/exwiw/schema_check.rb

Overview

Reports how the committed schema config differs from what the application would generate now, plus the columns still waiting for a masking decision. Regenerating into a copy rather than over the config directory lets it run on a working tree it must not modify (CI). ActiveRecord only.

Constant Summary collapse

CATEGORIES =
%w[
  added_tables added_columns removed_tables removed_columns changed_tables needs_mask_decision
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(models:, schema_dir:) ⇒ SchemaCheck

Returns a new instance of SchemaCheck.



23
24
25
26
# File 'lib/exwiw/schema_check.rb', line 23

def initialize(models:, schema_dir:)
  @models = models
  @schema_dir = schema_dir
end

Class Method Details

.clean?(report) ⇒ Boolean

Whether the report requires someone to act: the config is out of date, or a column's masking has not been decided yet.

Returns:

  • (Boolean)


44
45
46
# File 'lib/exwiw/schema_check.rb', line 44

def self.clean?(report)
  CATEGORIES.all? { |category| report.fetch(category, []).empty? }
end

.from_rails_application(schema_dir:) ⇒ Object



18
19
20
21
# File 'lib/exwiw/schema_check.rb', line 18

def self.from_rails_application(schema_dir:)
  Rails.application.eager_load!
  new(models: ActiveRecord::Base.descendants, schema_dir: schema_dir)
end

Instance Method Details

#runObject

The report as a plain Hash. Every list is sorted, so a given state always produces the same output (it ends up in a CI comment).



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/exwiw/schema_check.rb', line 30

def run
  committed = read_configs(@schema_dir)
  regenerated = Dir.mktmpdir do |tmp_dir|
    regenerate_into(tmp_dir)
    read_configs(tmp_dir)
  end

  report = diff(committed, regenerated)
  report["needs_mask_decision"] = flagged_columns(committed)
  report
end