Skip to main content

Preserving Role ID (rid) in a Drupal 6 to Drupal 7 Role Migration

I was working on setting up a role migration from Drupal 6 to Drupal 7. After some time, I noticed that the role id's were not being preserved, which was going to cause a problem for me when I went to take the migration to the live server. I initially tried using the addFieldMapping function to link the source rid to the destination rid (similar to preserving the nid for nodes or the uid for users). However, this failed to save any of the roles during migration. In the end, I used the complete() function of a custom class to manually update the role table to assign the source ID to the destination's role table. The whole class is below:

  1. /**
  2.  * Class to extend the role migration.
  3.  */
  4. class MyRoleMigration extends DrupalRole6Migration {
  5.   public function __construct(array $arguments) {
  6.     parent::__construct($arguments);   
  7.      // This option does not work
  8.      // $this->addFieldMapping('rid', 'rid')->description(t('Preserve the D6 RID')); 
  9.      // Set the is_new to force the RID to remain.
  10.      // $this->addFieldMapping('is_new')->defaultValue(1);
  11.   }
  12.  
  13.   public function complete($entity, $row) {
  14.     // Update the saved rid to the original rid from D6.
  15.     $num_updated = db_update('role')
  16.       ->fields(array(
  17.         'rid' => $row->rid,
  18.       ))
  19.       ->condition('rid', $entity->rid, '=')
  20.       ->execute();
  21.     if ($num_updated) {
  22.       // If the role table was updated, change the entity->rid to the source
  23.       // so the rollback options works properly.
  24.       $entity->rid = $row->rid; 
  25.     }
  26.   }
  27. }


Comments