Sha256: 660353a5dd1f731ab77993de5d9716220f5da6a877e37a0ff7da714526c1bf70

Contents?: true

Size: 1.67 KB

Versions: 93

Compression:

Stored size: 1.67 KB

Contents

module Rumination
  module Pg
    class Restore
      attr_reader :args

      def initialize *args
        require "open3"
        @args = args.dup.freeze
      end

      def self.call *args
        new(*args).call
      end

      def call
        command = "pg_restore #{args.join(" ")}"
        puts command
        Open3.popen3 ENV, command do |stdin, stdout, stderr, thread|
          out_lines = stdout.readlines
          err_lines = stderr.readlines
          save_stream :stdout, out_lines
          save_stream :stderr, err_lines
          puts_log out_lines + err_lines, indent: 2
          analyse_stderr! err_lines
        end
      end

      private

      def puts_log lines, indent: 0
        lines = lines.dup
        lines[3..-4] = ["", "... snip snip ...", ""] unless lines.count < 8
        lines.each do |line|
          puts line.indent(indent)
        end
      end

      def analyse_stderr! lines
        text = lines.join("\n")
        error_count = text.scan(/ERROR/).count
        more_errors_than_ignored = error_count > 0 && !expected_errors?(text, error_count)
        other_errors = error_matchers.any?{|m| m === text}
        if more_errors_than_ignored || other_errors
          raise RuntimeError, "pg_restore seems to have failed"
        end
      end

      def save_stream name, lines
        File.open("log/pg_restore.#{name}.log","w") do |io|
          io.puts lines
        end
      end

      def save_streams out_lines, err_lines
      end

      def error_matchers
        [
          /could not open input file/
        ]
      end

      def expected_errors? text, count
        text =~ /WARNING: errors ignored on restore: #{count}/
      end
    end
  end
end

Version data entries

93 entries across 93 versions & 1 rubygems

Version Path
rumination-0.17.6 lib/rumination/pg/restore.rb
rumination-0.17.5 lib/rumination/pg/restore.rb
rumination-0.17.4 lib/rumination/pg/restore.rb
rumination-0.17.3 lib/rumination/pg/restore.rb
rumination-0.17.2 lib/rumination/pg/restore.rb
rumination-0.17.1 lib/rumination/pg/restore.rb
rumination-0.17 lib/rumination/pg/restore.rb
rumination-0.16.6 lib/rumination/pg/restore.rb
rumination-0.16.5 lib/rumination/pg/restore.rb
rumination-0.16.4 lib/rumination/pg/restore.rb
rumination-0.16.3 lib/rumination/pg/restore.rb
rumination-0.16.2 lib/rumination/pg/restore.rb
rumination-0.16.1 lib/rumination/pg/restore.rb
rumination-0.16 lib/rumination/pg/restore.rb
rumination-0.15.1 lib/rumination/pg/restore.rb
rumination-0.15 lib/rumination/pg/restore.rb
rumination-0.14.11 lib/rumination/pg/restore.rb
rumination-0.14.10 lib/rumination/pg/restore.rb
rumination-0.14.9 lib/rumination/pg/restore.rb
rumination-0.14.8 lib/rumination/pg/restore.rb