Migrate users from drupal 6 to drupal 7

Hello, any drupal developer once is faced with a problem of data (users, nodes, terms) migration. I will tell you my experience of using migrate module.
In this article I will tell you about users migration from drupal 6 to drupal 7. First of all I should mention that migrate framework has nice api and it allows us even preserve users passwords. As I think any migration should starts with users because when we will migrate nodes they will have right author references. First of all we need to create module by adding folder with name my_migration adding my_migration.info and my_migration.module files. So lets start, code below will have a lot of commnets:

//my_migration.info file

name = "User Migrate"
 description = "Migration of data from Drupal 6 to Drupal 7"
 package = "Custom Migrate"
 core = 7.x
 dependencies[] = migrate
 dependencies[] = migrate_extras
 dependencies[] = field
 dependencies[] = file
 dependencies[] = text
 dependencies[] = path
 dependencies[] = taxonomy
 
 files[] = includes/users.inc

//my_migration.module file
// this file can be empty but we will define some constants
 2,
    'migrations' => array(
      'MfUserMigrate' => array('class_name' => 'MfUserMigrate'),
    ),
  );

  return $api;
}



//my_migration/users.inc

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('is_new')
      ->defaultValue(TRUE);
    $this->addFieldMapping('uid', 'id_to_preserve');
    $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.
  // massage the user roles before saving
  public function prepareRow($current_row) {
    $source_id = $current_row->uid;
    $query = Database::getConnection('default', CONNECT_NAME)
      ->select('users_roles', 'r')
      ->fields('r', array('uid', 'rid'))
      ->havingCondition('r.uid', $source_id, '=');
    $results = $query->execute();
    // add the anonymous role for everyone
    $roles = array('1' => '1');
    foreach ($results as $row) {
      // adjust the old to new role id's
      // Editors (rid 7) need role id 4
      if ($row->rid == 7) {
        $roles['4'] = '4';
      }
      elseif ($row->rid == 4) {
        // producer from 4 to 5
        $roles['5'] = '5';
      }
      elseif ($row->rid == 5) {
        // blogger from 5 to 6
        $roles['6'] = '6';
      }
      elseif ($row->rid == 8) {
        // freelancer 8 to 7
        $roles['7'] = '7';
      }
      else {
        // Collaboraters are role id 5 in both old and new dbs
        $roles[$row->rid] = $row->rid;
      }
    }
    $current_row->roles = $roles;
    return TRUE;
  }
} // close the ourUserMigration class