lib/wyrm/dump_schema.rb in wyrm-0.2.0 vs lib/wyrm/dump_schema.rb in wyrm-0.2.1

- old
+ new

@@ -4,10 +4,11 @@ # Dump a schema and compressed data from a db to a set of files # src_db = Sequel.connect "postgres://localhost:5454/lots" # ds = DumpSchema.new src_db, Pathname('/var/data/lots') # ds.dump_schema # ds.dump_tables +# TODO possibly use Gem::Package::TarWriter to write tar files class DumpSchema include PumpMaker def initialize( src_db, container = nil, pump: nil ) @src_db = maybe_deebe src_db @@ -29,58 +30,35 @@ def fk_migration @fk_migration ||= src_db.dump_foreign_key_migration(:same_db => same_db) end - def restore_migration - <<-EOF - require 'restore_migration' - Sequel.migration do - def db_pump - end - - up do - restore_tables - end - - down do - # from each table clear table - each_table do |table_name| - db_pump.restore table_name, io: io, db: db - end - end - end - EOF - end - def same_db false end def logger @logger ||= Logger.new STDERR end def dump_schema - (container + '001_schema.rb').open('w') do |io| + numbering = '000' + + (container + "#{numbering.next!}_schema.rb").open('w') do |io| io.write schema_migration end - (container + '002_populate_tables.rb').open('w') do |io| - io.write restore_migration - end - - (container + '003_indexes.rb').open('w') do |io| + (container + "#{numbering.next!}_indexes.rb").open('w') do |io| io.write index_migration end - (container + '004_foreign_keys.rb').open('w') do |io| + (container + "#{numbering.next!}_foreign_keys.rb").open('w') do |io| io.write fk_migration end end - def open_bz2( pathname ) + def write_through_bz2( pathname ) fio = pathname.open('w') # open subprocess in read-write mode zio = IO.popen( "pbzip2 -z", 'r+' ) copier = Thread.new do begin @@ -94,28 +72,29 @@ yield zio # signal the copier thread to stop zio.close_write logger.debug 'finished dumping' - # wait for copier thread to + + # wait for copier thread to finish copier.join logger.debug 'stream copy thread finished' ensure zio.close unless zio.closed? fio.close unless fio.closed? end - def dump_table( table_name ) + 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}" - open_bz2 filename do |zio| + write_through_bz2 filename do |zio| # generate the dump pump.io = zio pump.dump end rescue