Sha256: 6a07563a67689175422bcdd777c793826a32a597ca098fcedc433fc12bb9b659

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 KB

Contents

class Ridgepole::ExternalSqlExecuter
  def initialize(script, logger)
    @script = script
    @logger = logger
  end

  def execute(sql)
    cmd = Shellwords.join([@script, sql, JSON.dump(ActiveRecord::Base.connection_config)])
    @logger.info("Execute #{@script}")
    script_basename = File.basename(@script)

    Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr|
      stdin.close_write

      begin
        loop do
          IO.select([stdout, stderr]).flatten.compact.each do |io|
            io.each do |line|
              next if line.nil?
              line.strip!

              if io == stderr
                @logger.warn("[WARNING] #{script_basename}: #{line}")
              else
                @logger.info("#{script_basename}: #{line}")
              end
            end
          end

          if stdout.eof? and stderr.eof?
            break
          end
        end
      rescue EOFError
      end

      unless wait_thr.value.success?
        raise "`#{@script}` execution failed"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ridgepole-0.6.5.beta5 lib/ridgepole/external_sql_executer.rb