class ExternalMigration::Schemas::SchemasMigration

Façade class make easy way to running batchs schemas migrations

File Format

name_migration:

:type: CUSTOM, SCHEMA 
from: URL to from schema or Custom Param
to: URL to destinate schema or Custom Param
transformer: class will be called to data transformations

Attributes

processor[RW]
schemas[RW]
transformer[RW]
verbose[RW]

Public Class Methods

new(file_schemas, schemas=nil) click to toggle source
# File lib/external_migration/schemas.rb, line 21
def initialize(file_schemas, schemas=nil)
  if schemas.nil?
    raise "Invalid File: should not null!" if file_schemas.nil?
    raise "Invalid File: should not exists!" if not File.exists?(file_schemas)
    @schemas = YAML::load(File.open(file_schemas))
  else
    @schemas = schemas
  end
  @verbose = true
end

Public Instance Methods

eval_class(class_str) click to toggle source
# File lib/external_migration/schemas.rb, line 65
def eval_class(class_str)
  begin
    class_found = eval class_str
    raise "its %s not a class" % class_str if !class_found.is_a?(Class) 
  rescue
    class_found = false
  end
end
migrate!() click to toggle source
# File lib/external_migration/schemas.rb, line 32
def migrate!
  ActiveRecord::Base.transaction do   
    @schemas.each do |key,schema|
      
      @migration_name = key
      @schema = schema
      
      msg = "Starting external migration: %s..." % @migration_name
      Rails.logger.info msg
      puts msg if verbose
      
      result = run_migration_job
      
      raise ActiveMigartionSchemasError.new("Failing Migrate Schemas: %s" % key) if not result
      
      msg = "Ending: %s." % @migration_name
      Rails.logger.info msg
      puts msg if verbose
    end
  end
end
migrate_custom() click to toggle source
# File lib/external_migration/schemas.rb, line 135
def migrate_custom
  raise "Transformer not assigned" if @transformer.nil?
  raise "Invalid Custom Migration Transformer" if not @transformer.respond_to?(:migrate!) 

  @transformer.migrate!
end
migrate_schema() click to toggle source
# File lib/external_migration/schemas.rb, line 124
def migrate_schema
  migration = ExternalMigration::Migration.new
  migration.name = @migration_name
  migration.schema_from = @schema[:from]
  migration.schema_to   = @schema[:to]
  migration.transformer = @transformer if not @transformer.nil?
  migration.processor   = @processor   if not @processor.nil?
  #rock!
  migration.migrate!
end
require_dependence(file_name) click to toggle source
# File lib/external_migration/schemas.rb, line 93
def require_dependence(file_name)

  Rails.logger.warn "Requiring file %s" % file_name

  search_dependency(file_name).each do |file|
    if File.exists? file
      Rails.logger.debug "Including file %s" % file
      require file
      break
    end
  end
end
run_migration_job() click to toggle source
# File lib/external_migration/schemas.rb, line 54
def run_migration_job
  transformer_from_schema()
  
  case @schema[:type]
    when :SCHEMA
      self.migrate_schema
    when :CUSTOM
      self.migrate_custom
  end
end
search_dependency(file_name) click to toggle source

@return Array with possible location of file

# File lib/external_migration/schemas.rb, line 108
def search_dependency(file_name)
  files = []
  files << Rails.root.join("db/external_migrate/" + file_name)

  #possibility paths
  unless File.exists?(files[0])
    Dir[file_name,
        File.expand_path("**/external_migrate/**/" + file_name),
        "../" + file_name,
        "../../" + file_name,
        File.expand_path("**/" + file_name)].each { |f| files << f }
  end

  files
end
transformer_class=(class_str) click to toggle source
# File lib/external_migration/schemas.rb, line 74
def transformer_class=(class_str)
  
  path = class_str.split("::")
  
  
  path.map!(&:underscore)
  file_name = path.join("/") + ".rb"

  class_found = eval_class(class_str)

  if class_found==false
    require_dependence(file_name)
    class_found = eval_class(class_str)
    raise "[%s] Invald informed Transformer: %s.  Schema: %s" % [@migration_name, class_str, @schema.to_yaml] if class_found == false 
  end

  @transformer = (eval class_str).new @schema
end
transformer_from_schema() click to toggle source
# File lib/external_migration/schemas.rb, line 142
def transformer_from_schema
  if @schema.include? :transformer
    self.transformer_class = @schema[:transformer]
  else
    @transformer = nil
  end
end