Class: SchemaFerry::IO::PostgresWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/schema_ferry/io/postgres_writer.rb

Overview

Runs ridgepole as a subprocess rather than via Ridgepole::Client: Ridgepole::Client calls ActiveRecord::Base.establish_connection and patches AR in-process, which would hijack a host Rails app's database connection.

Instance Method Summary collapse

Constructor Details

#initialize(target_url) ⇒ PostgresWriter

Returns a new instance of PostgresWriter.



13
14
15
# File 'lib/schema_ferry/io/postgres_writer.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
33
34
35
36
37
# File 'lib/schema_ferry/io/postgres_writer.rb', line 17

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

  # ridgepole only reads the schema from -f FILE.
  Tempfile.create(["schema_ferry_", ".rb"]) do |f|
    f.write(schema_content)
    f.flush

    cmd = [bin, "--apply", "-c", @target_url, "-f", f.path]
    if dry_run
      cmd << "--dry-run"
    else
      cmd += ["--pre-query", "BEGIN", "--post-query", "COMMIT"]
    end

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

    stdout
  end
end