Sha256: a3d21de4908741dd43aa78e178f4881462cd5af38b6da143b214536c11ed97b6

Contents?: true

Size: 1.91 KB

Versions: 1

Compression:

Stored size: 1.91 KB

Contents

require 'logger'
require 'wyrm/db_pump'

# Load a schema from a set of dump files (from DumpSchema)
# and restore the table data
#  dst_db = Sequel.connect "postgres://localhost:5454/lots"
#  rs = RestoreSchema.new dst_db, Pathname('/var/data/lots')
#  rs.create
#  rs.restore_tables
class RestoreSchema
  def initialize( dst_db, container )
    @container = container
    @dst_db = dst_db
    @options = {:codec => :marshal}
    load_migrations @container
  end

  attr_reader :dst_db
  attr_reader :options
  attr_reader :container
  attr_reader :schema_migration, :index_migration, :fk_migration

  def logger
    @logger ||= Logger.new STDERR
  end

  def load_migrations( container )
    @schema_migration = (container + '001_schema.rb').read
    @index_migration = (container + '003_indexes.rb').read
    @fk_migration = (container + '004_foreign_keys.rb').read
  end

  # create indexes and foreign keys, and reset sequences
  def index
    logger.info "creating indexes"
    eval( index_migration ).apply dst_db, :up
    logger.info "creating foreign keys"
    eval( fk_migration ).apply dst_db, :up

    if dst_db.database_type == :postgres
      logger.info "reset primary key sequences"
      dst_db.tables.each{|t| dst_db.reset_primary_key_sequence(t)}
      logger.info "Primary key sequences reset successfully"
    end
  end

  # create the destination schema
  def create
    eval( schema_migration ).apply dst_db, :up
  end

  def restore_one_table( table_file )
    logger.info "restoring from #{table_file}"
    table_name = table_file.basename.sub_ext('').sub_ext('').to_s.to_sym
    # check if table has been restored already, and has the correct rows,
    # otherwise pass in a start row.
    DbPump.from_bz2 table_file, dst_db, table_name
  end

  def restore_tables
    table_files = Pathname.glob Pathname(container) + '*dbp.bz2'
    table_files.sort_by{|tf| tf.stat.size}.each{|table_file| restore_one_table table_file}
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
wyrm-0.1.4 lib/wyrm/restore_schema.rb