class Wyrm::Dump

Attributes

container[R]
pump[R]
src_db[R]

Public Class Methods

[]( *args ) click to toggle source
# File lib/wyrm/dump.rb, line 19
def self.[]( *args )
  new(*args).call
end
new( src_db, container = nil, pump: nil ) click to toggle source
# File lib/wyrm/dump.rb, line 29
def initialize( src_db, container = nil, pump: nil )
  @container = Pathname.new container || '.'
  raise "#{@container} does not exist" unless @container.exist?

  @src_db = maybe_deebe src_db
  @pump = make_pump( @src_db, pump )

  @src_db.extension :schema_dumper
end

Public Instance Methods

call() click to toggle source
# File lib/wyrm/dump.rb, line 23
def call
  dump_schema
  dump_tables
  dump_indexes
end
dump_indexes() click to toggle source
# File lib/wyrm/dump.rb, line 62
def dump_indexes
  (container + "#{numbering.next!}_indexes.rb").open('w') do |io|
    io.write index_migration
  end

  (container + "#{numbering.next!}_foreign_keys.rb").open('w') do |io|
    io.write fk_migration
  end
end
dump_schema() click to toggle source
# File lib/wyrm/dump.rb, line 56
def dump_schema
  (container + "#{numbering.next!}_schema.rb").open('w') do |io|
    io.write schema_migration
  end
end
dump_table( table_name, &io_block ) click to toggle source
# File lib/wyrm/dump.rb, line 109
def dump_table( table_name, &io_block )
  pump.table_name = table_name
  if pump.table_dataset.empty?
    logger.info "No records in #{table_name}"
    return
  end

  filename = container + "#{table_name}.dbp.bz2"
  logger.info "dumping #{table_name} to #{filename}"

  write_through_bz2 filename do |zio|
    # generate the dump
    pump.io = zio
    pump.dump
  end
rescue
  logger.error "failed dumping #{table_name}: #{$!.message}"
end
dump_table_schemas( *tables ) click to toggle source
# File lib/wyrm/dump.rb, line 47
def dump_table_schemas( *tables )
  (container + "#{numbering.next!}_schema.rb").open('w') do |io|
    tables.each do |table|
      logger.debug "schema for #{table}"
      io.puts table_migration table
    end
  end
end
dump_tables() click to toggle source
# File lib/wyrm/dump.rb, line 128
def dump_tables
  src_db.tables.each do |table_name|
    dump_table table_name
  end
end
numbering() click to toggle source
# File lib/wyrm/dump.rb, line 43
def numbering
  @numbering ||= '000'
end
same_db() click to toggle source
# File lib/wyrm/dump.rb, line 41
def same_db; false end
write_through_bz2( pathname ) { |zio| ... } click to toggle source
# File lib/wyrm/dump.rb, line 72
def write_through_bz2( pathname )
  fio = pathname.open('w')
  # open subprocess in read-write mode
  zio = IO.popen( STREAM_COMP, 'r+' )
  copier = Thread.new do
    begin
      IO.copy_stream zio, fio
      logger.debug "finished stream copy"
    ensure
      fio.close
    end
  end

  # block receiving zio will write to it.
  yield zio

  # signal the copier thread to stop
  logger.debug 'flushing'
  if RUBY_ENGINE == 'jruby'
    # seems to be required for jruby, at least 9.1.2.0
    logger.debug 'jruby flushing'
    zio.flush
    logger.debug 'jruby close'
    zio.close
  else
    zio.close_write
  end
  logger.debug 'finished dumping'

  # wait for copier thread to finish
  copier.join
  logger.debug 'stream copy thread finished'
ensure
  zio.close if zio && !zio.closed?
  fio.close if fio && !fio.closed?
end