lib/pg_dumper.rb in pg_dumper-0.0.2 vs lib/pg_dumper.rb in pg_dumper-0.1.0

- old
+ new

@@ -1,76 +1,145 @@ +require 'pg_dumper/vendor/escape' +require 'tempfile' +require 'open3' + class PgDumper require 'pg_dumper/railtie' if defined?(Rails) - + attr_reader :database - attr_reader :args - + attr_reader :output + def initialize database @database = database @args = [] @options = {} end - + def run(mode = :silent) - @binary ||= find_executable - - raise "ERROR: pg_dump executable not found" unless @binary.present? + raise "ERROR: pg_dump executable not found" unless binary options = {} - + case mode when :silent - options[:out] = "/dev/null" + options[:out] = "/dev/null" end - system @binary, *args, database, options + + execute command, options end - + + def command + Escape.shell_command([binary, *args, database]).to_s + end + def schema_only! add_args "-s" end - - def recreate! + + def create! + add_args "-C" + end + + def clean! add_args "-c" end - + def data_only! add_args "-a", '--disable-triggers' add_args '-T', 'schema_migrations' end - + def compress! level=9 add_args '-Z', level if level.present? end - + def verbose! + @stderr = nil add_args "-v" end - + def pretty! add_args '--column-inserts' end - + + def silent! + # FIXME: this is not windows friendly + # try to use same solution as Bundler::NULL + @stderr = "/dev/null" + end + def connection= opts add_args '-p', opts['port'] if opts['port'].present? add_args '-h', opts['host'] if opts['host'].present? add_args '-U', opts['username'] if opts['host'].present? end - + def output= filename - add_args "-f", filename + @output = filename end - + + def output? + !!@output + end + + def output + File.path(@output) + end + + def tempfile + @tempfile ||= new_tempfile + end + + def args + if output? + @args.dup.push('-f', output) + else + @args + end + end + private + + def binary + @binary ||= find_executable + end + + def execute(cmd, options) + puts [cmd, options].inspect + if output? + system(cmd, options) + else + stdout, status = Open3.capture2(cmd, options) + stdout + end + end + def find_executable [ENV['PG_DUMP'], %x{which pg_dump}.strip].each do |pg| - return pg if pg.present? && File.exists?(pg) + return pg if pg && File.exists?(pg) end nil end - + def add_args(*args) @args.push *args.map!(&:to_s) @args.uniq! end - - -end \ No newline at end of file + + def stdout + @stdout || $stdout + end + + def stderr + @stderr || $stderr + end + + def new_tempfile + tempfile = Tempfile.new('pg_dumper') + at_exit { + tempfile.close + tempfile.unlink + } + tempfile + end + +end