class ExternalMigration::Migration

Migrate data between data source and transforme to destination

Attributes

name[RW]
processor[RW]
schema_from[RW]
schema_to[RW]
transformer[RW]

Public Class Methods

new(schema_url=nil) click to toggle source

constructor

# File lib/external_migration/migration.rb, line 78
def initialize(schema_url=nil)
  self.load_schema(schema_url) unless schema_url.nil?
end

Public Instance Methods

begin_migration() click to toggle source
# File lib/external_migration/migration.rb, line 192
def begin_migration
  # TODO: make transactional
  res = @transformer.begin(@schema_from, @schema_to) unless @transformer.nil?
end
end_migration() click to toggle source
# File lib/external_migration/migration.rb, line 198
def end_migration
  res = @transformer.end(@schema_from, @schema_to) unless @transformer.nil?
end
load_schema(url) click to toggle source

load yml schemas from and to

# File lib/external_migration/migration.rb, line 83
def load_schema(url)
  schema = YAML::load(File.open(url)).keys_to_sym
  self.schema_from = schema[:from]
  self.schema_to = schema[:to]
end
load_schema_from(url) click to toggle source

loads yml file and convert to hash on schema_from @deprecated

# File lib/external_migration/migration.rb, line 232
def load_schema_from(url)
  self.schema_from = YAML::load(File.open(url))
end
load_schema_to(url) click to toggle source

load yml file and convert to hash on schema_to @deprecated

# File lib/external_migration/migration.rb, line 239
def load_schema_to(url)
  self.schema_to = YAML::load(File.open(url))      
end
migrate!() click to toggle source

Running migration from configured files

ps> Default Behaviour Ignore First line - assumes head line

# File lib/external_migration/migration.rb, line 95
def migrate!

  raise "schema_from needs" if @schema_from.nil?
  raise "schema_to needs" if @schema_to.nil?
  
  res = @transformer.begin_transaction(@schema_from, @schema_to) unless @transformer.nil?
  
  @line = 0
  
  ActiveRecord::Base.transaction do      
    begin_migration()

    # TODO: Make flexible configurable and more input formats
    case @schema_from[:format].to_s.to_sym 
    when :XLS
      xls_migrate()
    when :TXT_FIXED
      decoder = ExternalMigration::TextFixed.new(@schema_from)
      decoder.migration = self
      decoder.migrate!
    end
  
    end_migration()
  end
  
  res = @transformer.end_transaction(@schema_from, @schema_to) unless @transformer.nil?
  
  return true
end
migrate_row!(row_to) click to toggle source
# File lib/external_migration/migration.rb, line 125
def migrate_row!(row_to)
  @line += 1
  begin
    if @processor.nil?
      #transform row to @schema_to
      res = true
      res = @transformer.transform(row_to) unless @transformer.nil?
    
      if (res!=:ignore)
        res = res==true && send_row_to_schema(row_to)
        raise_migration if (res==false)
      
        @transformer.after_row_saved(row_to, @last_object) unless @transformer.nil?
      end
    else
      @processor.migrate_row row_to
    end
  rescue Exception => e
    line = @line.nil? ? 0 : @line
    column = @column.nil? ? 0 : @column
    
    obj = @last_object || (e.respond_to?(:record) && (e.record)) || nil
    
    if !obj.nil?
      raise ActiveMigartionDataSourceError.new obj.errors.to_yaml.to_s.concat("Failing import excel source format from %s. %d:%d [ignored head]. " % [@schema_from[:url], column, line]).concat(e.message).concat("\n----"+e.backtrace.to_yaml)         
    else
      raise ActiveMigartionDataSourceError.new ("Failing import excel source format from %s. %d:%d [ignored head]. " % [@schema_from[:url], column, line]).concat(e.message).concat("\n----"+e.backtrace.to_yaml)
    end
  end

end
raise_migration() click to toggle source
# File lib/external_migration/migration.rb, line 202
def raise_migration
  raise "failing migration %s.  Line: %d, Column: %d" % [@name, @line, @column]
end
send_row_to_schema(row) click to toggle source
# File lib/external_migration/migration.rb, line 206
def send_row_to_schema(row)
  
  if @schema_to[:format].to_sym == :ACTIVE_RECORD
    
    # TODO: optimize on initialize migration
    class_schema_to = eval @schema_to[:url]
    
    @last_object = class_schema_to.new(row)
    res = @last_object.save
  
    if (!res)
      msg = "[Schema:%s] Error on send to ACTIVE_RECORD %s. \n%s \nrow: \n%s" % [@name, @schema_to[:url], @last_object.errors.to_yaml, row.to_yaml]
      Rails.logger.error msg
      raise AciteMigrationInvalidRecordError.new msg
    end
  
    return res
  
  else
    raise "Not valid schema::TO format! %s" % @name  
  end
end
xls_migrate() click to toggle source
# File lib/external_migration/migration.rb, line 158
def xls_migrate
  begin
    @xls = Spreadsheet.open @schema_from[:url]
    # TODO: make others workbook accessible by configuration
    sheet = @xls.worksheet 0

    # ignore head line
    sheet.each 1 do |row|
      @column = 0
      row_to = { }
      
      #read schema columns and types
      @schema_from[:columns].each do |schema_column, schema_type|
        row_to.merge!(schema_column.to_sym => row[@column])
        @column+=1
      end
      
      self.migrate_row! row_to
    end
  rescue Exception => e
    line = @line.nil? ? 0 : @line
    column = @column.nil? ? 0 : @column
    
    obj = @last_object || (e.respond_to?(:record) && (e.record)) || nil
    
    if !obj.nil?
      raise ActiveMigartionDataSourceError.new obj.errors.to_yaml.to_s.concat("Failing import excel source format from %s. %d:%d [ignored head]. " % [@schema_from[:url], column, line]).concat(e.message).concat("\n----"+e.backtrace.to_yaml)         
    else
      raise ActiveMigartionDataSourceError.new ("Failing import excel source format from %s. %d:%d [ignored head]. " % [@schema_from[:url], column, line]).concat(e.message).concat("\n----"+e.backtrace.to_yaml)
    end
  end
end