Class: Migraine::Migration

Inherits:
Map
  • Object
show all
Includes:
Generator
Defined in:
lib/migraine/migration.rb

Overview

This is a special case of a ‘Migraine::Map` as it specifies source and destination databases and provides methods to, for example, run the migration. It also overrides Migraine::Map#set_source_and_destination_from(s_and_d) to accept surce and destination in a more elegant way.

Instance Attribute Summary collapse

Attributes inherited from Map

#destination, #maps, #source

Instance Method Summary collapse

Methods included from Generator

#database_schema_for, #database_schema_for_mysql, #generate

Methods inherited from Map

#initialize, #map

Constructor Details

This class inherits a constructor from Migraine::Map

Instance Attribute Details

#prefix(new_prefix = nil) ⇒ Object

DSL convenience method for skipping the assignment operator when specifying prefix.

Parameters:

  • Prefix (String)

    to use in generations.



55
56
57
# File 'lib/migraine/migration.rb', line 55

def prefix
  @prefix
end

Instance Method Details

#runObject

Runs the migration using the mappings that have been set up. Walks through the nested Map objects, copying database records from source to destination.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/migraine/migration.rb', line 15

def run
  connect
  src = @source_connection
  dest = @destination_connection

  log "BEGINNING MIGRATION\n" +
      "-------------------"

  # Iterate through table mappings
  maps.each do |table|
    log "MIGRATING TABLE #{table.source}"

    # Fetch all rows from source table
    rows = src[table.source.to_sym].all
    
    # Iterate through the records and migrate each one
    rows.each do |row|
      new_record = {}

      # Identify the columns we need to grab data from
      table.maps.each do |column|
        new_record.merge!(
          column.destination.to_sym => row[column.source.to_sym]
        )
      end

      # Insert new record to destination table
      log " -> Inserting record into #{table.destination}"
      dest[table.destination.to_sym].insert(new_record)
    end
  end

  log "-------------------\n" +
      "MIGRATION COMPLETED"
end