Class: SchemaFerry::Target::RidgepoleRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/schema_ferry/target/ridgepole_runner.rb

Overview

Runs ridgepole as a subprocess rather than via Ridgepole::Client: the client calls ActiveRecord::Base.establish_connection and patches AR in-process, which would hijack a host Rails app's database connection. The tempfile exists because the CLI only reads the schema from -f FILE.

Instance Method Summary collapse

Constructor Details

#initialize(target_url) ⇒ RidgepoleRunner

Returns a new instance of RidgepoleRunner.



13
14
15
# File 'lib/schema_ferry/target/ridgepole_runner.rb', line 13

def initialize(target_url)
  @target_url = target_url
end

Instance Method Details

#run(schema_content, dry_run: false) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/schema_ferry/target/ridgepole_runner.rb', line 17

def run(schema_content, dry_run: false)
  bin = find_ridgepole!

  Tempfile.create(["schema_ferry_", ".rb"]) do |f|
    f.write(schema_content)
    f.flush

    cmd = [bin, "--apply", "-c", @target_url, "-f", f.path]
    cmd << "--dry-run" if dry_run

    stdout, stderr, status = Open3.capture3(*cmd)
    raise RidgepoleError, [stdout, stderr].reject { |s| s.strip.empty? }.join("\n") unless status.success?

    stdout
  end
end