Class: EventEngine::SchemaDriftGuard

Inherits:
Object
  • Object
show all
Defined in:
lib/event_engine/schema_drift_guard.rb

Defined Under Namespace

Classes: DriftError

Class Method Summary collapse

Class Method Details

.check!(schema_path:, definitions:) ⇒ Object

Raises:



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/event_engine/schema_drift_guard.rb', line 7

def self.check!(schema_path:, definitions:)
  raise DriftError, "Schema file does not exist: #{schema_path}" unless File.exist?(schema_path)

  committed = File.read(schema_path)
  regenerated = dump_to_string(definitions)

  return true if committed == regenerated

  raise DriftError, <<~MSG
    EventEngine schema drift detected.

    The DSL definitions do not match #{schema_path}.

    #{SchemaDiff.new(expected: committed, actual: regenerated)}
    Run:
      bin/rails event_engine:schema:dump

    And commit the updated schema file.
  MSG
end

.dump_to_string(definitions) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/event_engine/schema_drift_guard.rb', line 28

def self.dump_to_string(definitions)
  Tempfile.create("event_schema") do |file|
    EventEngine::EventSchemaDumper.dump!(
      definitions: definitions,
      path: file.path
    )
    File.read(file.path)
  end
end