We Build Websites

Data Migration - part 1
By Amitai Burstein (@amitaibu) // 06 Jan 2011

Moving an old site to Drupal?

The following article will discuss the right way to do this using the module Migrate (version2). Migrate V2 uses Drush (Drupal Shell) to execute the migration functions, if your are not familiar with Drush I suggest looking into this very powerful tool.

Here is a list of the main challenges I have faced during the process :

  1. Mapping old information to the new structure.
  2. Mapping the old values to match the new fields.
  3. Moving the references between the old objects into Drupal.

Install the following modules :

  1. drush
  2. Migration 6.x
  3. activate Migrate ,Migrate UI
drush dl migrate
drush en migrate migrate_ui

In this example we will import Users from the old site , take a look at the old Users table:


For the migrate module to recognize classes defined by your module, you must define hook_migrate_api() in your .module file:

function example_migrate_api() {
  $api = array(
	'api' => 2,
  return $api;

Define the migration class, each migration class should end with the suffix Migration .

class OldUsersMigration extends Migration

  public function __construct() {
    $this->description = t('Migrate users from the source database');

define the source (the old data object) :

This part is used in order to map the primary key of the old USERS table. We ll also use this mapping when we want to import objects that were references to users (in our case , users). This mean that Migrate will take care of coordinating the old UID to the new one.

  $this->map = new MigrateSQLMap($this->machineName,
      'user_id'  => array(
        'type' => 'int',
        'not null' => TRUE,

Now, we want to set a query to bring our source records (from the old DB), we use the db_select function as described bellow:

   $query = db_select('car_users', 'old_users')
     ->fields('old_users' ,array(
      'email' ,
      'user_id' ,
      'date_of_birth' ,
      'last_login_date' ,
  $this->source = new MigrateSourceSQL($query);

Define the destination , for example : user , taxonomy , node.

  $this->destination = new MigrateDestinationUser(); // Drupal user

Its time to start the field mapping , here we will map all the fileds that we want to import from the old DB to the new field name in drupal for example :

password : the old site password field. pass: the Drupal password field.

  $this->addFieldMapping('pass', 'password');
  $this->addFieldMapping('mail', 'email');
  $this->addFieldMapping('name', 'username');
  $this->addFieldMapping('login', 'last_login_date');
  $this->addFieldMapping('created', 'date_added');
  $this->addFieldMapping('signature', 'moto');
  $this->addFieldMapping('access', 'last_login_date');

In order to complete the mapping process, we need to mark all the destination fields that are NOT mapped as DNM (do not map) :

    //all the fields we don’t wanna map in the destination!!!

Druring the mapping you can use the ‘migrate ui’ to see your migration map status:

open the page /basepath/admin/content/migrate

Now we can executing the migration using Drush, the excute functions allows us a process of trial and error using the command, drush migrate-import and drush migrate-rollback repeadly.

To import the first 10 users :

drush mi oldUsers –-itemlimit=10

To roll back to the previous state :

drush mr oldUsers –-itemlimit=10

To see your data migration status use:

drush ms oldUsers

In the next chapter we will discuss importing cars of the users with an emphasis on the transfer relationships between tables, files, etc. ..