Community Documentation

Simple Drupal 6 to 7 User migration.

Last updated October 11, 2012. Created by danielson317 on October 11, 2012.
Log in to edit this page.

I spent a day searching for this and it seems like a rather common task. Here is how I did it. Just getting this page started so tweek as necessary.

settings.php

$databases['default']['default'] = array (
  'driver' => 'mysql',
  'database' => 'drupal7',
  'username' => 'root',
  'password' => 'root',
  'host' => '127.0.0.1',
  'port' => '3306',
  'prefix' => '',
);
$databases['migrate']['default'] = array (
  'driver' => 'mysql',
  'database' => 'drupal6',
  'username' => 'root',
  'password' => 'root',
  'host' => '127.0.0.1',
  'port' => '3306',
  'prefix' => '',
);

my_module.inc

<?php

/**
* User migration class for Demand Metric.
*/


class ArrojoEducationUserMigrate extends Migration {
  public function __construct() {
    parent::__construct(MigrateGroup::getInstance('ArrojoEducationUser'));

    // Select fields from the Drupal 6 user table.
    $query = Database::getConnection('default', 'migrate')
                        ->select('users', 'u')
                        ->fields('u', array('uid', 'name', 'pass', 'mail', 'created', 'access',
                                            'login', 'status', 'picture', 'init'));

    // This is supposed to get roles not sure how.
    $source_fields = array(
      'uid' => t('User ID'),
      'roles' => t('The set of roles assigned to a user.'),
    );

    // Set source and destination.
    $this->source = new MigrateSourceSQL($query, $source_fields);
    $this->destination = new MigrateDestinationUser(array('md5_passwords' => TRUE));

    // Set up database maping.
    $this->map = new MigrateSQLMap($this->machineName,
      array(
        'uid' => array(
          'type' => 'int',
          'unsigned' => TRUE,
          'not null' => TRUE,
          'description' => 'D6 Unique User ID',
          'alias' => 'u',
        )
      ),
      MigrateDestinationUser::getKeySchema()
    );

    // Add simple field mappings
    $this->addFieldMapping('name', 'name')->dedupe('users', 'name');
    $this->addFieldMapping('pass', 'pass');
    $this->addFieldMapping('mail', 'mail')->dedupe('users', 'mail');
    $this->addFieldMapping('language')->defaultValue('');
    $this->addFieldMapping('theme')->defaultValue('');
    $this->addFieldMapping('signature')->defaultValue('');
    $this->addFieldMapping('signature_format')->defaultValue('filtered_html');
    $this->addFieldMapping('created', 'created');
    $this->addFieldMapping('access', 'access');
    $this->addFieldMapping('login', 'login');
    $this->addFieldMapping('status', 'status');
    $this->addFieldMapping('picture')->defaultValue(0);
    $this->addFieldMapping('init', 'init');
    $this->addFieldMapping('timezone')->defaultValue(NULL);
    $this->addFieldMapping('roles', 'roles');
  }

  // Set up Roles.
  public function prepareRow($current_row) {

    // Additional query on the roles table.
    $source_id = $current_row->uid;
    $query = db_select('sourcetable.users_roles', 'r')
      ->fields('r', array('uid', 'rid'))
      ->condition('r.uid', $source_id, '=');
    $results = $query->execute();

    // Add the Authenticated role for everyone.
    $roles = array('2' => '2');
    foreach ($results as $row) {

      // Subscriber role should be transfered.
      if ($row->rid == 6) {
        $roles['5'] = '5';
      }
    }
    $current_row->roles = $roles;
    return TRUE;
    // return FALSE if you wish to skip a particular row
  }
}

Referenced this blog post a lot for this code: http://dtek.net/blog/drupal-6-to-drupal-7-via-migrate-2

Comments

Roles migration

Thanks for this post, it's very helpful.
I wonder how do you migrate your roles?
Do you migrate the roles using the Migrate API as well?
I am trying to migrate the role with Migrate API and having issue with mapping the users_roles from old Drupal 6 to new Drupal 7.
Any thoughts will be very appreciated.
Thanks.

Page status

No known problems

Log in to edit this page

About this page

Drupal version
Drupal 7.x
Level
Beginner
Audience
Programmers

Administration & Security Guide

Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.