Class: ActiveMigration::Schemas::SchemasMigration

Inherits:
Object
  • Object
show all
Defined in:
lib/active_migration/schemas.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_schemas) ⇒ SchemasMigration

Returns a new instance of SchemasMigration.



21
22
23
24
25
# File 'lib/active_migration/schemas.rb', line 21

def initialize(file_schemas)
  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))
end

Instance Attribute Details

#schemasObject

Returns the value of attribute schemas.



19
20
21
# File 'lib/active_migration/schemas.rb', line 19

def schemas
  @schemas
end

#transformerObject

Returns the value of attribute transformer.



19
20
21
# File 'lib/active_migration/schemas.rb', line 19

def transformer
  @transformer
end

Instance Method Details

#eval_class(class_str) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/active_migration/schemas.rb', line 50

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!Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/active_migration/schemas.rb', line 27

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
      
      result = run_migration_job
      
      raise ActiveMigartionSchemasError.new("Failing Migrate Schemas: %s" % key) if not result
      
      msg = "Ending: %s." % key
      Rails.logger.info msg
      puts msg
    end
  end
end

#migrate_customObject



125
126
127
128
129
130
# File 'lib/active_migration/schemas.rb', line 125

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_schemaObject



115
116
117
118
119
120
121
122
123
# File 'lib/active_migration/schemas.rb', line 115

def migrate_schema
  migration = ActiveMigration::Migration.new
  migration.name = @migration_name
  migration.schema_from = @schema[:from]
  migration.schema_to   = @schema[:to]
  migration.transformer = @transformer if not @transformer.nil?
  #rock!
  migration.migrate!
end

#require_dependence(file_name) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/active_migration/schemas.rb', line 73

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_jobObject



104
105
106
107
108
109
110
111
112
113
# File 'lib/active_migration/schemas.rb', line 104

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) ⇒ Object

Returns Array with possible location of file.

Returns:

  • Array with possible location of file



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/active_migration/schemas.rb', line 88

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) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/active_migration/schemas.rb', line 59

def transformer_class=(class_str)
  file_name = class_str.underscore + ".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_schemaObject



132
133
134
135
136
137
138
# File 'lib/active_migration/schemas.rb', line 132

def transformer_from_schema
  if @schema.include? :transformer
    self.transformer_class = @schema[:transformer]
  else
    @transformer = nil
  end
end