Sha256: 1fece7bb17e6550c76f08355089bdf0ea9f5072a37d510bd86c5b2b09c93c7e4

Contents?: true

Size: 1.72 KB

Versions: 10

Compression:

Stored size: 1.72 KB

Contents

#!/usr/bin/env ruby

require 'rubygems'
require 'highline'


class DumpLoader

  def initialize(argv)
    @argv = argv
    @verbose = !!@argv.delete('-v')
  end

  def dumps_dir
    require 'etc'
    user_dir = Etc.getpwuid.dir
    File.join(user_dir, 'dumps')
  end

  def db_console_command
    if File.exists?("script/dbconsole")
      "script/dbconsole -p"
    else
      "rails dbconsole -p"
    end
  end

  def source_dump(dump)
    require 'pty'
    output_buffer = StringIO.new
    PTY.spawn(db_console_command) do |output, input, pid|
      input.write("source #{dump};\nexit;\n")
      output_buffer.write output.read
    end

    output_and_errors = output_buffer.string.split("\n")
    output = output_and_errors.reject{ |line| line =~ /^ERROR / }
    errors = output_and_errors.select{ |line| line =~ /^ERROR / }

    [output, errors]
  end

  def choose_dump_file
    highline = HighLine.new

    available_dumps = Dir.glob("#{dumps_dir}/*.dump").sort
    selected_dump = highline.choose(*available_dumps) do |menu|
      menu.hidden('') { exit }
    end
  end

  def get_dump_file
    if ARGV[0] && File.exists?(ARGV[0])
      ARGV[0]
    else
      choose_dump_file
    end
  end


  def puts_info(msg = "")
    puts msg if @verbose
  end

  def execute!
    dump_to_load = get_dump_file

    puts_info
    puts_info "sourcing #{dump_to_load} into db ..."

    output, errors = source_dump(dump_to_load)

    puts_info
    puts_info output.join("\n")

    if errors.empty?
      puts_info "sourcing completed successfully."
      exit(0)
    else
      $stderr.puts "some errors occured while loading the dump #{File.basename(dump_to_load)}:"
      $stderr.puts errors.join("\n");
      exit(1)
    end
  end

end


DumpLoader.new(ARGV).execute!


Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
geordi-0.14.6 bin/load-dump
geordi-0.14.5 bin/load-dump
geordi-0.14.4 bin/load-dump
geordi-0.14.3 bin/load-dump
geordi-0.14.2 bin/load-dump
geordi-0.14.1 bin/load-dump
geordi-0.14.0 bin/load-dump
geordi-0.13.3 bin/load-dump
geordi-0.13.2 bin/load-dump
geordi-0.13.1 bin/load-dump