class Wyrm::Restore

Attributes

container[R]
dst_db[R]
pump[R]

Public Class Methods

[]( *args ) click to toggle source
# File lib/wyrm/restore.rb, line 24
def self.[]( *args )
  new(*args).call
end
new( container, dst_db, pump: nil, drop_tables: false ) click to toggle source
# File lib/wyrm/restore.rb, line 35
def initialize( container, dst_db, pump: nil, drop_tables: false )
  @container = Pathname.new container
  fail "#{@container} does not exist" unless @container.exist?

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

  options.drop_tables = drop_tables
end

Public Instance Methods

call() click to toggle source
# File lib/wyrm/restore.rb, line 28
def call
  drop_tables(table_names) if options.drop_tables
  create_tables
  restore_tables
  create_indexes
end
find_single( glob ) click to toggle source

sequel wants migrations numbered, but it's a bit of an annoyance for this.

# File lib/wyrm/restore.rb, line 56
def find_single( glob )
  candidates = Pathname.glob container + glob
  raise None, "No restore files found for #{glob}" if candidates.size == 0
  raise "too many #{candidates.inspect} for #{glob}" if candidates.size > 1
  candidates.first
end
fk_migration() click to toggle source
# File lib/wyrm/restore.rb, line 75
def fk_migration
  @fk_migration ||= find_single( '*foreign_keys.rb' ).read
rescue None
  ''
end
index_migration() click to toggle source
# File lib/wyrm/restore.rb, line 69
def index_migration
  @index_migration ||= find_single( '*indexes.rb' ).read
rescue None
  ''
end
open_bz2( table_name, &block ) click to toggle source

open a dbp.bz2 file and either yield or return an io of the uncompressed contents

# File lib/wyrm/restore.rb, line 98
def open_bz2( table_name, &block )
  table_file =
  case table_name
  when Symbol
    container + "#{table_name}.dbp.bz2"
  when Pathname
    table_name
  else
    raise "Don't know what to do with #{table_name.inspect}"
  end

  IO.popen "#{STREAM_DCMP} #{table_file}", &block
end
options() click to toggle source
# File lib/wyrm/restore.rb, line 49
def options
  @options ||= OpenStruct.new
end
reload_migrations() click to toggle source
# File lib/wyrm/restore.rb, line 81
def reload_migrations
  @fk_migration = nil
  @index_migration = nil
  @schema_migration = nil
end
restore_table( table_file ) click to toggle source

assume the table name is the base name of table_file pathname

# File lib/wyrm/restore.rb, line 88
def restore_table( table_file )
  logger.info "restoring from #{table_file}"
  pump.table_name = table_file.basename.sub_ext('').sub_ext('').to_s.to_sym
  open_bz2 table_file do |io|
    pump.io = io
    pump.restore filename: table_file
  end
end
restore_tables() click to toggle source
# File lib/wyrm/restore.rb, line 116
def restore_tables
  table_files.sort_by{|tf| tf.stat.size}.each{|table_file| restore_table table_file}
end
schema_migration() click to toggle source
# File lib/wyrm/restore.rb, line 63
def schema_migration
  @schema_migration ||= find_single( '*schema.rb' ).read
rescue None
  ''
end
table_files() click to toggle source
# File lib/wyrm/restore.rb, line 112
def table_files
  Pathname.glob container + '*.dbp.bz2'
end
table_names() click to toggle source
# File lib/wyrm/restore.rb, line 120
def table_names
  table_files.map do |path|
    path.basename.to_s.split(?.)[0...-2].last.to_sym
  end
end