Class: ActiveMigration::Migration

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

Overview

Class Migration

Migrate data between data source and transforme to destination

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema_url = nil) ⇒ Migration

constructor



79
80
81
# File 'lib/active_migration/migration.rb', line 79

def initialize(schema_url=nil)
  self.load_schema(schema_url) unless schema_url.nil?
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



76
77
78
# File 'lib/active_migration/migration.rb', line 76

def name
  @name
end

#schema_fromObject

Returns the value of attribute schema_from.



76
77
78
# File 'lib/active_migration/migration.rb', line 76

def schema_from
  @schema_from
end

#schema_toObject

Returns the value of attribute schema_to.



76
77
78
# File 'lib/active_migration/migration.rb', line 76

def schema_to
  @schema_to
end

#transformerObject

Returns the value of attribute transformer.



76
77
78
# File 'lib/active_migration/migration.rb', line 76

def transformer
  @transformer
end

Instance Method Details

#begin_migrationObject



161
162
163
164
# File 'lib/active_migration/migration.rb', line 161

def begin_migration
  # TODO: make transactional
  res = @transformer.begin(@schema_from, @schema_to) unless @transformer.nil?
end

#end_migrationObject



167
168
169
# File 'lib/active_migration/migration.rb', line 167

def end_migration
  res = @transformer.end(@schema_from, @schema_to) unless @transformer.nil?
end

#load_schema(url) ⇒ Object

load yml schemas from and to



85
86
87
88
89
# File 'lib/active_migration/migration.rb', line 85

def load_schema(url)
  schema = YAML::load(File.open(url))
  self.schema_from = schema[:from]
  self.schema_to = schema[:to]
end

#load_schema_from(url) ⇒ Object

Deprecated.

loads yml file and convert to hash on schema_from



198
199
200
# File 'lib/active_migration/migration.rb', line 198

def load_schema_from(url)
  self.schema_from = YAML::load(File.open(url))
end

#load_schema_to(url) ⇒ Object

Deprecated.

load yml file and convert to hash on schema_to



205
206
207
# File 'lib/active_migration/migration.rb', line 205

def load_schema_to(url)
  self.schema_to = YAML::load(File.open(url))      
end

#migrate!Object

Running migration from configured files

ps> Default Behaviour Ignore First line - assumes head line



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/active_migration/migration.rb', line 97

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?
  
  ActiveRecord::Base.transaction do      
    begin_migration()

    # TODO: Make flexible configurable and more input formats
    if @schema_from[:format].to_s.to_sym == :XLS
        xls_migrate()
    end
  
    end_migration()
  end
  
  res = @transformer.end_transaction(@schema_from, @schema_to) unless @transformer.nil?
  
  return true
end

#raise_migrationObject



171
172
173
# File 'lib/active_migration/migration.rb', line 171

def raise_migration
  raise "failing migration %s.  Line: %d, Column: %d" % [@name, @line, @column]
end

#send_row_to_schema(row) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/active_migration/migration.rb', line 175

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)
      raise AciteMigrationInvalidRecordError.new "[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]
    end
  
    return res
  
  else
    raise "Not valid schema::TO format! %s" % @name  
  end
end

#xls_migrateObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/active_migration/migration.rb', line 121

def xls_migrate
  begin
    @xls = Spreadsheet.open @schema_from[:url]
    # TODO: make others workbook accessible by configuration
    sheet = @xls.worksheet 0

    @line = 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
      
      #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
      
      @line+=1
    end
  rescue Exception => e
    line = @line.nil? ? 0 : @line
    column = @column.nil? ? 0 : @column
    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