How to migrate database users from Drupal 6 into Drupal 7 Part II

We want to convert users database from Drupal 6 into Drupal 7 by simple php script. This is part number II of "How to convert database from Drupal 6 into Drupal7". On previous, part I , telling about How to convert nodes databases Drupal 6 into Drupal 7. Now, after succesfully moving nodes, we go to moving users. In this steps, i don't guarantee about password will still works or not after moved into Drupal 7. Probably, is difficult to make same hash of password of each users when moving from Drupal 6 into Drupal 7. But, you can suggest your user to change their password using "forgot password" option. It's will solve problem also keep communicate with users.

Basically, there are several fields in table users that new in Drupal 7 compared with Drupal 6. Some fields that we can set by default are theme, picture and data. Just modify this code if you have some custom configuration. Remember that picture in Drupal 7 is interger format. Just in case, if you want to put link of user images, you can insert in signature (varchar 255) and it's can be loaded in comment template files.

<?php
/**
* Migrating Users
* By Yodiaditya - Yoodey.com - Drupal 7 How to, Tutorial and Guide
*/
function migrate_users(){
   
db_set_active('old');
   
$result = db_query("SELECT * FROM {users} WHERE status = '1' AND uid > 1 ORDER BY uid ASC");
   
db_set_active('default');

    foreach(

$result as $record ) {
       
$nid = db_insert('users') // Table name no longer needs {}
                       
->fields(array(
                           
'uid' => $record->uid,
                           
'name' => $record->name,
                           
'pass' => $record->pass,
                           
'mail' => $record->mail,
                           
'theme' => $record->theme,
                           
'signature' => $record->signature,
                           
'signature_format' => $record->signature_format,
                           
'created' => $record->created,
                           
'access' => $record->access,
                           
'login' => $record->login, // 0 for never login
                           
'status' => '1',
                           
'timezone' =>$record->timezone,
                           
'language' => 'und',
                           
'picture' => '0',
                           
'init' => '0',
                           
'data' =>''
                       
))
                        ->
execute();
    }

    return

'finish migrate users!';
}
?>



Better you know about create multiple database connection in Drupal 7 for known usage of db_set_active().
Also, $result = db_query("SELECT * FROM {users} WHERE status = '1' AND uid > 1 ORDER BY uid ASC"); this will query that not administator users. So, your Drupal 7 admin still safe.

Later, i will discuss part III - how to moving URL ALIAS tables from Drupal 6 into Drupal 7.



You should follow me here

Comments

Add new comment